Tuesday, April 29, 2014

Let there be LIGHT! Sensor input experiment

Chris and Skyler here, and boy do we have a goodie for y'all! We've been spending some time playing around with LEDs and we wanted to up the game by adding a photosensor input.  Through exposing our sensor to a flashlight our LED would turn off (how energy efficient! ) and if we moved the light away, the LED would light up again. The video below demos the action and you can see our code below,





int lightPin = 0;  //define a pin for Photo resistor
int ledPin=12;     //define a pin for LED

void setup()
{
    Serial.begin(9600);  //Begin serial communcation
    pinMode( ledPin, OUTPUT );
}

void loop()
{
    Serial.println(analogRead(lightPin)); //Write the value of the photoresistor to the serial monitor.
    analogWrite(ledPin, analogRead(lightPin)/4);  //send the value to the ledPin. Depending on value of resistor
                                                //you have  to divide the value. for example,
                                                //with a 10k resistor divide the value by 2, for 100k resistor divide by 4.
   delay(10); //short delay for faster response to light.
}

No comments:

Post a Comment