Wednesday, May 12, 2010

Arduino / Hacked Toy Internet Alert Circuit

Overview
Zach Kaplan recently invited Roth Mobot to the Chicago offices of Inventables to lead their monthly Lab session wherein guests speak about the various recent crossovers of technology and creativity. At first we were asked to conduct a hands-on toy hacking session with Inventables' employees, but about a week before our arrival we were given the circuit bending challenge of creating an "interface between the Internet and a common circuit bent toy."

Basic Data Flow
We decided to create a circuit that would activate a toy whenever someone logged into Roth Mobot's web site. We designed a simple and elegant solution using an Arduino, a homemade Vactrol, a common electronic toy, and three simple scripts written in different programming languages. We'd like to thank William Swyter for lending us his Arduino, and Factory Smoke for his Vactrol suggestion, which stopped us from creating overly-complicated custom circuitry with transistors and diodes, and made the programming a piece of cake. The result was an elegant circuit that electrically insulates the toy from the Arduino (and the computer) by "optically coupling" them with light.
 
Vactrol Construction
We've included a diagram of how to build a Vactrol. All you need is a bright LED, and a Cadmium Sulfide photo resistor. We used black epoxy for our casing.

Arduino / Vactrol Hook Up
We plugged the Vactrol's LED's ground lead into the Arduino's ground socket, and it's positive lead into the Arduino's number 13 socket. We used socket number 13 because it has a built in resistor.

Vactrol / Hacked Toy Hook Up
We then connected the two leads from the Vactrol's photo resistor to two leads coming out of a hacked police truck toy. The beauty of this approach is that it doesn't matter what the voltage of the toy is or whether it's "voltage triggered" or "ground triggered."

Video of Circuit in Action
Now when someone logs into our web site the circuit bent police truck toy lights up and says, "Slow down, pull over!" in a demonic voice.


Sample PHP  Code 
We embedded this common basic hit counter in our home page
<?php  $count_my_page = ("hitcounter.txt"); 
$hits = file($count_my_page); 
$hit = trim($hits[0]); 
$hit++; 
$fp = fopen($count_my_page , "w"); 
fputs($fp , "$hit"); 
fclose($fp); 
echo $hit; 
?>;
 
Sample Python Code 
This is the Python script that runs on the computer attached to the Arduino and the hacked toy. It requires the PySerial library extension to be installed. The only part of the code we had to change was the Arduio's serial port and the location of the TXT file being generated by the PHP script on our server. (highlighted below). 
# fetch txt file

import time
import urllib
import serial

# USB serial connection to Arduino

ser = serial.Serial(
   'com4', 115200)
myUrl = 'http://www.YOURWEBSITE.COM/hitcounter.txt'

last_counter = urllib.urlopen(myUrl).read()
while (True):
  counter = urllib.urlopen(myUrl).read()
  delta = int(counter) - int(last_counter)
  print "counter: %s, delta: %s" % (counter, delta)
  ser.write(chr(ord(chr(delta))))
  last_counter = counter
  time.sleep(10)

Sample Arduino Code 
This code was originally used to activate a servo motor. We Frankensteined it with some other code to light the Vactrol's LED. 
int ledPin = 13; 
int minPulse = 0; 
int maxPulse = 10; 
int pulse = 0; 
int rings = 0; 
long nextMillis = 0; 
long lastPulse = 0; 
int refreshTime = 200; 
int state = 0; 
 
void setup() {
  pinMode(ledPin, OUTPUT); 
  pulse = minPulse; 
  Serial.begin(115200);
}

#define PULSE_ON       0
#define WAIT_PULSE_ON  1
#define PULSE_OFF      2
#define WAIT_PULSE_OFF 3
 
void loop() {
  
  if (Serial.available() > 0) {
    rings += Serial.read();
    Serial.println(rings);
  }
   
  if (rings > 0) {
      digitalWrite(ledPin, HIGH);  // LED on
      delay(200);                  // wait
      digitalWrite(ledPin, LOW);   // LED off
  }
  if (rings = 0) {
      digitalWrite(ledPin, LOW);   // LED off
  }
  }