I have the CdS light sensor connected to analog pin 0 with a 1k ohm resistor. I have a series of five LED's connected to digital pins 13, 12, 11, 10, and 9. They are all grounded through a 1k resistor.
I am trying to get the LEDs to light up sequentially with either less light or more light. For example, I would like that to light up one LED (say, pin 13) when the value is from 0-55. When the value is between 50 and 100, I would like to light up two LED's (pins 13 and 12). And so on and so forth.
However, when I upload the code it lights all of the LED's. No change in the amount of light reaching the sensor affects the LED output. I have tried for over an hour on this single problem (after chasing down missing semicolons and what-not for another hour), and I can't figure out what I'm doing wrong. I feel like this is a pretty valuable thing to learn how to code because it wouldn't be hard to adapt it to work as a proximity sensor or optical trigger.
Thank you all for the help!
-Jason
Code: Select all
/*
*CdS Light Sensor Bar Graph
*
*August 1, 2012
*
*This sketch uses a CdS light sensor and if/else statements to turn on
*a LED or LEDs at a predetermined values.
*/
int redLED1 = 13; // Red LED connected to a digital pins
int redLED2 = 12;
int redLED3 = 11;
int redLED4 = 10;
int redLED5 = 9;
int varResis = 0; // Variable, light resistor with an integer value of 0
void setup ()
{
Serial.begin(9600); // Begins a debugging serial stream at 9600 baud
pinMode(redLED1, OUTPUT); // Sets the pin the red LED is connected to as an output
pinMode(redLED2, OUTPUT);
pinMode(redLED3, OUTPUT);
pinMode(redLED4, OUTPUT);
pinMode(redLED5, OUTPUT);
}
void loop ()
{
Serial.println(analogRead(varResis)); // Prints in the analog value of the variable resistor
delay(100); // Delays print in of serial info by 1/10s
varResis = analogRead(0); // Reads variable as an analog, 0-1023 value
if(varResis <= 50 ) // If the value is equal to or below 50, pin 13 is given +5v and all of the other pins are given none.
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
}
else if(varResis > 50 && varResis <=100 ) // If the value is between 50 and 100, pins 13 and 12 are given +5v and the other pins are given none.
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
}
else if(varResis >100 && varResis <= 150 )
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
}
else if(varResis > 150 && varResis <= 200)
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
}
else (varResis > 200 && varResis <= 1023 );
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
}
}
http://mail-attachment.googleuserconten ... 6981G19oA8
http://mail-attachment.googleuserconten ... o&sadssc=1