Tuesday, June 29, 2010

Hacking a Light Toy

So after much frustration with my first attempt I decided to hack a toy instead of making something from scratch. The toy, shown below, just had a push button before, but I wanted to make it come on when the lights went off.

IMG_3334

IMG_3335

After breaking open the toy I popped out a piece from the back and slipped the wires out of the new hole.

IMG_3344

Before adding a sensor I wanted to make sure I could hack it and ran it with a simple blink code.
After doing this I was able to manipulate the function by making it strobe on and off. Success!

IMG_3383

Now it's time for the photo resistor.

IMG_3385

I removed the button to replace it with the photo resistor.

IMG_3388

Set the resistor with hot glue.

IMG_3389

Solder some wires to it.

IMG_3390

Pull out the wires.

IMG_5576

Wire it up to the arduino.

IMG_5578

Lights on...

IMG_5583

Lights off...

The code I used is very simple. It's a combination of the example blink and button.

Here's the code:

/*
photo resistor as a Button

Turns on and off anything connected to digital
pin 13, when pressing a pushbutton attached to pin 2.


The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.


created 2005
by DojoDave
modified 17 Jun 2009
by Tom Igoe

http://www.arduino.cc/en/Tutorial/Button

modified june 2010
by Richard Meacham
http://interactivesquared.blogspot.com/
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 2000 ; // variable for reading the pushbutton status.

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH); // set the LED on
delay(500); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(250); // wait for a second
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

And the wiring diagram:

button

No comments:

Post a Comment