Making Furniture Interactive

September 5, 2007

jenn gooch’s lamp w/ light sensor

Filed under: Exercise 0: Make A Lamp,Jenn Gooch — jenngooch @ 11:51 am

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.

board setup

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

LED/light sensor lamp

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
}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: