Ambient Brightness Bar Graph / Proximity Sensor

by JRucks on Wed Aug 01, 2012 9:57 pm

I'm a beginner to Arduino and programming in general, so forgive me if some of my questions seem obvious. I'm using an Arduino Uno with the budget starter pack to make an ambient light sensor that, based on the analog output of the CdS light sensor, lights a certain number of LEDs.

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); 
   }
   
  }
 
 


Pictures:
http://mail-attachment.googleusercontent.com/attachment/u/0/?ui=2&ik=32c22af6ed&view=att&th=138e5415fc4ad8c2&attid=0.2&disp=inline&safe=1&zw&saduie=AG9B_P9G039tlGfUtVcPO0ZJGORa&sadet=1343876136564&sads=iAp5cWDDz8HRqv8dk6981G19oA8
http://mail-attachment.googleusercontent.com/attachment/u/0/?ui=2&ik=32c22af6ed&view=att&th=138e5415fc4ad8c2&attid=0.1&disp=inline&safe=1&zw&saduie=AG9B_P9G039tlGfUtVcPO0ZJGORa&sadet=1343876132653&sads=aB7NmOaqmVfNPxe_SjKxsJCOFuo&sadssc=1
JRucks
 
Posts: 4
Joined: Sat Jul 14, 2012 7:20 pm

Re: Ambient Brightness Bar Graph / Proximity Sensor

by adafruit_support_bill on Thu Aug 02, 2012 4:42 am

To post photos, use the "upload attachment" tab below the edit window.

This line is wrong.
Code: Select all
Serial.println(analogRead(varResis));    // Prints in the analog value of the variable resistor

varResis is the variable you are using to store the reading later in the loop. It should be:
Code: Select all
Serial.println(analogRead(0));    // Prints in the analog value of the variable resistor


Fix that line and post the output of the serial monitor.

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.

Each led should be connected to ground through a separate resistor.
User avatar
adafruit_support_bill
 
Posts: 25601
Joined: Sat Feb 07, 2009 9:11 am

Re: Ambient Brightness Bar Graph / Proximity Sensor

by JRucks on Thu Aug 02, 2012 7:38 pm

Thanks for the help. I fixed both things you suggested, and although it seems like the serial monitor is more consistent than before, it hasn't fixed the LED problem. I'm not sure why it isn't working. How do I post the output from the serial monitor?

Also, why do I need to connect an individual resistor on each of the LEDs?
JRucks
 
Posts: 4
Joined: Sat Jul 14, 2012 7:20 pm

Re: Ambient Brightness Bar Graph / Proximity Sensor

by adafruit_support_bill on Thu Aug 02, 2012 8:14 pm

How do I post the output from the serial monitor?

Open the serial monitor (the button on the far-right side of the IDE toolbar). Then select the output, and cut & paste to the forum message.

why do I need to connect an individual resistor on each of the LEDs?

Variations in individual led forward voltages can result in some leds carrying higher currents if they are wired in parallel. Separate resistors limit the current to each led separately.
User avatar
adafruit_support_bill
 
Posts: 25601
Joined: Sat Feb 07, 2009 9:11 am