Dark sensor
This is really a test of a light sensor, or Light Dependent Resistor (LDR). I got one of these with my prototyping kit from Adafruit and figured I should learn how to use it. There is actually a much more useful article on Libelium that shows how to hook it up more clearly and even gives schematics.
The 5v is dropped a bit with a 1k resistor. The LDR is dropped into ground with the other end sitting between the dropped 5v and one of the analog ports. As voltage varies through the LDR the analog port sees the difference and reports it.
In my code whatever is found on the LDR/analog port (0-1023) is translated into a number of LEDs to light. The lower the value - or less resistance to ground - presented by the LDR shows up as a lower value on the analog port. This means less LEDs lit. The higher the values and the more LEDs are lit.
I built several versions of this setup, but the one pictured is the last version. It uses 9 LEDs I took from a Christmas light string I got from Gerten's. The LEDs aren't fancy, but I've got a lot of them, so I figured on the last incarnation of this thing I'd use as many as I could comfortably fit on the proto-shield.
/*
* LightSensor4.pde
* Controls 9 LEDw which get turned on or off in relation
* to values read from the LDR. None on for darkest up to
* all on for brightest.
*/
// Light Dependent Resistor (LDR) - or analog pin we'll listen to
int LDR = 0;
// various levels of analog read - total range is 1023 (dark) to zero (light)
// int level[] = { 1024, 896, 768, 640, 512, 384, 256, 128 };
int level[] = { 1000, 900, 800, 700, 600, 500, 400, 300, 200 };
// establish an array for our LED pins
int ledpin[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// other working variable schtuff
int leds = 9;
int count = 0;
int val = 0;
int set = 0;
/*
* The "run once" setup method
*/
void setup()
{
// set up to communication with serial
Serial.begin(9600);
// set this pin (analog/light sensor) to input
pinMode(LDR, INPUT);
// set all the LED pins to output
for (count = 0; count < leds; count++)
{
pinMode(ledpin[count], OUTPUT);
// and turn on those LEDs
//digitalWrite(ledpin[count], HIGH);
}
// say that we're done if anyone's listening
Serial.println("Setup has run");
}
/*
* Main processing method
*/
void loop()
{
// fetch a value from the light sensor
val = analogRead(LDR);
// show us what analog/LDR val is
Serial.print("val: ");
Serial.println(val, DEC);
// initially set LEDs to light to max # possible
set = leds;
// look through range of levels
for (count = 0; count < leds; count++)
{
// if value less than a given level, drop # of LEDs to light by 1
if (val < level[count]) { set--; }
}
// show us what we think the number of LEDs to set is
Serial.print("set: ");
Serial.println(set, DEC);
// if there's anything to set
if (set > 0)
{
// loop up to only the number to set
for (count = 0; count < set; count++)
{
// and turn on those LEDs
digitalWrite(ledpin[count], HIGH);
}
}
// loop through the remaining LEDs
for (count = (leds - 1); count >= set; count--)
{
// and turn 'em off
digitalWrite(ledpin[count], LOW);
}
*/
}
Comments
Post a Comment