I wanted to play a little with Arduino’s on board analog inputs after the joy of dealing with A/D converters previously. I built a simple lamp with 3 LEDs, packing tape (I moved this weekend – it was around) and a light sensor. I played around with some borrowed code to create the following, which includes a simple serial print function (this streams numbers into the black box at the bottom of your code in Arduino that corresponds to the info coming in from the sensor – great for troubleshooting) so that I could monitor the numbers coming in from the sensor.
Since I have next week’s assignment covered I’ll work towards a more creative housing material that is less, well, sticky.
NOTE: be careful running serial all the time – it’s a good way to freeze-up or crash. Best to monitor then stop it.

Materials:
LEDs
light sensor
resistors
wire
heat shrink tubing
(packing tape)

Code:
/* Analog Read to LED
* ——————
* copyleft 2005 David Cuartielles (modified by Jennifer Gooch)
*
*/
int sensorPin = 0; // select the input pin for the
// potentiometer
int ledPin1 = 11; // select the pin for LED1
int ledPin2 = 12; // select the pin for LED2
int ledPin3 = 13; // select the pin for LED3
int val = 0; // variable to store the value coming
// from the sensor
void setup() {
pinMode(ledPin1, OUTPUT); // ledPin is as an OUTPUT
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
val = analogRead(sensorPin); // read the value from
// the sensor
Serial.print(val, DEC); // prints value from sensor
Serial.print(” “); // space in value
if (val > 0)
{
digitalWrite(ledPin1, LOW); // turn the LED on
digitalWrite(ledPin2, LOW); // turn the LED on
digitalWrite(ledPin3, LOW); // turn the LED on
}
if (val > 250)
{
digitalWrite(ledPin1, LOW); // turn the LED on
digitalWrite(ledPin2, LOW); // turn the LED on
digitalWrite(ledPin3, HIGH); // turn the LED off
}
if (val > 500)
{
digitalWrite(ledPin1, LOW); // turn the LED on
digitalWrite(ledPin2, HIGH); // turn the LED off
digitalWrite(ledPin3, HIGH); // turn the LED off
}
if (val > 750)
{
digitalWrite(ledPin1, HIGH); // turn the LED off
digitalWrite(ledPin2, HIGH); // turn the LED off
digitalWrite(ledPin3, HIGH); // turn the LED off
}
delay(100); // delay program for 100 ms
}