12 March 2010

Task 26 - ladyada software & breadboard





/*Photocell simple testing sketch.

Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
Connect LED from pin 9 through a resistor to ground
For more information see www.ladyada.net/learn/sensors/cds.html */

int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 9; // connect Red LED to pin 09(PWM pin)
int LEDbrightness; //
void setup(void)
{
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}

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

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

// LED gets brighter the darker it is at the sensor
// that means we have to -invert- the reading from 0-1023 back to 1023-0
photocellReading = 1023 - photocellReading;
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness);

delay(100);
}


3 comments:

  1. Using PWM pin 9 instead of pin 11 in the original.

    I've got my LED project on the other side of the
    breadboard!

    ReplyDelete
  2. On the ladayada BB, i didn't have much luck with my LED-resistor network - tried putting a 22k resistor where hers has 220-1k, & i got some brightening when i 'darkened' the input of the CDS cell, but not much..

    ReplyDelete
  3. ..ummm, what kind of 'improvements'?
    Fancy-pants kinds, or just stuff that makes it work better..?

    ReplyDelete