Tuesday, September 16, 2008

One Sensor, Sensing

This here is a light sensor, compliments of radio shack, giving me some feedback. Way to go, little sensor!


Here's the Arduino all wired up and ready to go

And here is a readout showing it's receiving

With light

Without light!

And here's the code:

int analogReading; //variable int size because adc number potentially as big as 1024
int potPin = 0; // Analog input pin that the potentiometer is attached to
int potValue = 0; // value read from the pot
int led = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
void setup()
{
beginSerial(9600); //set up communication back to pc
//don't forget to press "serial monitor button"
}

void loop()
{
analogReading = analogRead(0); //can only use pins 0-5
Serial.println(analogReading); //talk back to pc

potValue = analogRead(potPin); // read the pot value
analogWrite(led, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
Serial.println(potValue); // print the pot value back to the debugger pane
delay(10); // wait 10 milliseconds before the next loop

}

No comments: