Sunday, February 28, 2010

Task 28 - program to turn a LED on whenever the light is blocked to your LED

int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading;
int ledPin = 7;
// the analog reading from the analog resistor divider

void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}

void loop(void) {
photocellReading = analogRead(photocellPin);

Serial.print("Analog reading = ");
Serial.print(photocellReading); // the raw analog reading

// We'll have a few threshholds, qualitatively determined
if (photocellReading < 10) {
Serial.println(" - Dark");
digitalWrite(ledPin, HIGH); // LED on
} else if (photocellReading < 200) {
Serial.println(" - Dim");
digitalWrite(ledPin, HIGH); // LED on
} else if (photocellReading < 500) {
Serial.println(" - Light");
digitalWrite(ledPin, LOW); // LED off
} else if (photocellReading < 800) {
Serial.println(" - Bright");
digitalWrite(ledPin, LOW); // LED off
} else {
Serial.println(" - Very bright");
digitalWrite(ledPin, LOW); // LED off
}
delay(1000);
}

No comments:

Post a Comment