Hi people I’ve combined these codes to try and get a Bargraph controlled by a UV sensor but… not sure if its my faulty wiring job, but its not behaving correctly. getting silly readings from the UV sensor for starters.
float Vsig;
// these constants won’t change:
const int analogPin = A0; // the pin that the UV sensor is attached to
const int ledCount = 5; // the number of LEDs in the bar graph
int ledPins = {
11, 10, 9, 8, 5
}; // an array of pin numbers to which LEDs are attached
void setup() {
Serial.begin(9600);
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
int sensorValue;
long sum=0;
for(int i=0;i<1023;i++)
{
sensorValue=analogRead(A0);
sum=sensorValue+sum;
delay(2);
}
sum = sum >> 10;
Vsig = sum*4980.0/1023.0; // Vsig is the value of voltage measured from the SIG pin of the Grove interface
Serial.print("The voltage value: “);
Serial.print(Vsig);
Serial.print(” mV – ");
if (Vsig < 50) {
Serial.print("UV Index: 0 “); Serial.println(” Exposure level - NONE (You’re probably at home!) ");
}
if (Vsig > 50 && Vsig < 227) {
Serial.print("UV Index: 1 “); Serial.println(” Exposure level - LOW (You’re probably at home!) ");
}
if (Vsig > 227 && Vsig < 318) {
Serial.print("UV Index: 2 “); Serial.println(” Exposure level - LOW (You can go outside and have fun!) ");
}
if (Vsig > 318 && Vsig < 408) {
Serial.print("UV Index: 3 “); Serial.println(” Exposure level - MODERATE (Sun starts to annoy you) ");
}
if (Vsig > 408 && Vsig < 503) {
Serial.print("UV Index: 4 “); Serial.println(” Exposure level - MODERATE (Sun starts to annoy you) ");
}
if (Vsig > 503 && Vsig < 606) {
Serial.print("UV Index: 5 “); Serial.println(” Exposure level - MODERATE (Sun starts to annoy you) ");
}
if (Vsig > 606 && Vsig < 696) {
Serial.print("UV Index: 6 “); Serial.println(” Exposure level - HIGH (Get out from the sunlight! get out now!) ");
}
if (Vsig > 696 && Vsig < 795) {
Serial.print("UV Index: 7 “); Serial.println(” Exposure level - HIGH (Get out from the sunlight! get out now!) ");
}
if (Vsig > 795 && Vsig < 881) {
Serial.print("UV Index: 8 “); Serial.println(” Exposure level - VERY HIGH (Get out from the sunlight! get out now!) ");
}
if (Vsig > 881 && Vsig < 976) {
Serial.print("UV Index: 9 “); Serial.println(” Exposure level - VERY HIGH (If you value your health, don’t go outside, just stay at home!) ");
}
if (Vsig > 976 && Vsig < 1079) {
Serial.print("UV Index: 10 “); Serial.println(” Exposure level - VERY HIGH (If you value your health, don’t go outside, just stay at home!) ");
}
if (Vsig > 1079 && Vsig < 1170) {
Serial.print("UV Index: 11 “); Serial.println(” Exposure level - EXTREME (If you value your health, don’t go outside, just stay at home!) ");
}
if (Vsig > 1170) {
Serial.print("UV Index: 11+ “); Serial.println(” Exposure level - EXTREME (You will probably die in 3, 2, 1… Just JOKING, don’t be scared…) - intensity of sunlight is really at maximum ");
}
// read the UV sensor:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element’s index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}