Sunday, March 7, 2010

Task 28




This code is from the http://www.ladyada.net/learn/sensors/cds.html website. Depending on the amount of light read in by the LDR, the LED will glow. If the LDR reads in very little or no light, the LED will increase in brightness.
At this stage this does not light up very much so I will need to edit it some more.

int photocellPin = 0;
int photocellReading;
int LEDpin = 11;
int LEDbrightness;
void setup(void) {

Serial.begin(9600);
}

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

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

photocellReading = 1023 - photocellReading;

LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness);

delay(100);
}

No comments:

Post a Comment