Tuesday, December 28, 2010

Arduino LED Sensing

/*
@Kun Duan
Simple LED sensing
Two LED required:
ledPin connects the output LED
analogPin connects the input LED as a light sensor
Note that the input LED MUST have its +/- legs inversed
*/

int ledPin = 13; // LED '13 pin as output
int analogPin = 0;
int value = LOW;
int val = 0;
long previousMillis = 0;
long interval = 120;

void setup()
{
pinMode(ledPin, OUTPUT); // set pin mode as OUTPUT
Serial.begin(9600); // set baud rate as 9600 bps
}

void loop()
{
if (millis() - previousMillis > interval) // run program every "interval" msec
{
previousMillis = millis();
val = analogRead(analogPin); // read analog input
if(val < 10) // set output led value as HIGH
value = HIGH;
else
{
value = LOW;
}
digitalWrite(ledPin, value); // write value to led pin
Serial.print("DETECT:"); // send back analog input through serial port Serial.println(val);
}
}


%%%%%%%%%%%%%%%%%%%%%
Below is a demo video
%%%%%%%%%%%%%%%%%%%%%