Sunday, February 28, 2010

Analogue Inputs giving Serial Outputs (Tasks 24, 26)

http://www.ladyada.net/learn/sensors/cds.html
This site is a great resource for info on LDRs and the following program is taken from this site.

The code is starting to gain some more practical purposes. With this code (and circuit diagram) you can read the resistance of an LDR (Light De pendant Resistor) and output a value. We use the analogue pins along the bottom edge of the board to read a value between 0 and 1024 from the LDR. That value can then be used to determine the light levels in the room:

int reading;

void setup() {
Serial.begin(9600);
}

void loop() {
reading = analogRead(0);

Serial.print("Analog reading = ");
Serial.print(reading);

if (reading < 10) {Serial.println (" - Dark");}
else if (reading < 200) {Serial.println (" - Dim");}
else if (reading < 500) {Serial.println (" - Light");}
else if (reading < 800) {Serial.println (" - Bright");}
else {Serial.println (" - Very Bright");}
delay(1000);
}


(this image was copied directly from Peter's Blog http://embeddednotices.blogspot.com/)

In the well lit room D207, the highest value I was able to cause was 724, and the lowest was 34.

No comments:

Post a Comment