Problem with UV sensor
-
After some research i've found out that my UVM-30A is outputting 0-1V (as stated in the datasheet). The analog input on my nano pro however, expect 0-5V to calculate the 0-1170 analog value that is used for the UV calculation in the sketch (see: http://www.mysensors.org/build/uv)
Right now i'm multiplying the input times 5 to correct the issue. But I was wondering whether it is the sketch or my nano pro who is wrong here..
-
I was scratching my head over this problem last year, but found a solution that works for me.....Try this.....
sensorVal = analogRead(sensorPin); //The pin the UV sensor is connected to
sensorVal = constrain(sensorVal, 0, 1180);
sensorValue = map (sensorVal, 0, 246, 0, 1180); //sensorValue is the number you
//are looking forThen I did a look up for the UV index as follows......
if (sensValue >= 0 && sensValue <= 226)
uvVal = 0;
else if (sensValue >= 227 && sensValue <= 317)
uvVal = 1;
else if (sensValue >= 318 && sensValue <= 407)
uvVal = 2;
else if (sensValue >= 408 && sensValue <= 502)
uvVal = 3;
else if (sensValue >= 503 && sensValue <= 605)
uvVal = 4;
else if (sensValue >= 606 && sensValue <= 695)
uvVal = 5;
else if (sensValue >= 696 && sensValue <= 794)
uvVal = 6;
else if (sensValue >= 795 && sensValue <= 880)
uvVal = 7;
else if (sensorValue >= 881 && sensorValue <= 975)
uvVal = 8;
else if (sensValue >= 976 && sensValue <= 1078)
uvVal = 9;
else if (sensValue >=1079 && sensValue <=1169)
uvVal = 10;
else if (sensValue >= 1170)
uvVal = 11;And now I get the values I expect!
Look at the map function on the arduino page to see how it works!Skywatch
-
@skywatch said:
if (sensValue >= 0 && sensValue <= 226)
uvVal = 0;
else if (sensValue >= 227 && sensValue <= 317)
uvVal = 1;
else if (sensValue >= 318 && sensValue <= 407)
uvVal = 2;
else if (sensValue >= 408 && sensValue <= 502)
uvVal = 3;
else if (sensValue >= 503 && sensValue <= 605)
uvVal = 4;
else if (sensValue >= 606 && sensValue <= 695)
uvVal = 5;
else if (sensValue >= 696 && sensValue <= 794)
uvVal = 6;
else if (sensValue >= 795 && sensValue <= 880)
uvVal = 7;
else if (sensorValue >= 881 && sensorValue <= 975)
uvVal = 8;
else if (sensValue >= 976 && sensValue <= 1078)
uvVal = 9;
else if (sensValue >=1079 && sensValue <=1169)
uvVal = 10;
else if (sensValue >= 1170)
uvVal = 11;trivial, but this can be simplified like this:
if (sensValue <= 226) uvVal = 0; else if (sensValue <= 317) uvVal = 1; else if (sensValue <= 407) uvVal = 2; else if (sensValue <= 502) uvVal = 3; else if (sensValue <= 605) uvVal = 4; else if (sensValue <= 695) uvVal = 5; else if (sensValue <= 794) uvVal = 6; else if (sensValue <= 880) uvVal = 7; else if (sensorValue <= 975) uvVal = 8; else if (sensValue <= 1078) uvVal = 9; else if (sensValue <=1169) uvVal = 10; else uvVal = 11;
-
Or even simpler...
int levels[11] = {226, 317, 407, ....}; int uVal = 0; for (;uVal<=11;uVal++) { if (uVal==11 || sensValue <= levels[uVal]) break; }
(untested)
-
@Bulldog....
Yeah I know! - it was 19 months ago I did that and I was trying out new ideas. This was obviously the last one as it is the last version (currently)
som är för smart för mig