Getting back into hobby electronics after 2 decades, I am amazed and thrilled too see the new powerful toy in town - the mircocontroller. After some research, I decided to get an Atmel based Arduino board to experiment with. Since Arduino is not easily available in India, I got a Freeduino board from Bhasha Technologies.
As a first "newbie" project with the Arduino, I decided to try and hack my son's remote controlled toy train. The remote has five buttons on it - forward/reverse, stop, horn and a "speed up" button. I wanted to control the first four buttons from my computer. The idea is that a program running on the computer would output key strokes to the serial port. The code running on the Arduino board would read these characters from the serial port,and activate the right switch on the remote control.
To activate the switches, I used an opto-isolator, which isolates the remote's electrical circuit from that of the Arduino board. I used an MCT2E available here for about Rs. 8. To the left is a simple circuit that shows how this works.
This image shows the switches inside the remote.
Here, I have soldered wires to the contact points on to the switches to an external connector.
This image shows how the optocouplers are connected to the Arduino board.
Processing Code running on the Computer
The Arduino board is connected to my computer, via a USB cable, which creates a virtual serial port (COM6 on mine). To send serial data based on a key press, I used the Processing Language. (See code below.)
Code that runs on the Arduino Board
/**
* Train
*
* Writes pressed key value to serial port.
*
*/
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(200, 200);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
print(portName);
myPort = new Serial(this, portName, 9600);
}
void draw() {
background(255);
if(keyPressed) {
print(key);
myPort.write(key);
}
}
Arduino uses a language called Wiring. The code below reads the serial port, and activates digital pins 13, 12, 10 or 9, which are connected to 4 optocouplers, which in turn "press" the remote's buttons.
/*
* Train
* by MV
*
* Pins 13,12,10,9 mapped to controls on toy train
*
* http://
*/
int stopPin = 13;
int hornPin = 12;
int forwardPin = 11;
int reversePin = 10;
void setup() {
pinMode(stopPin, OUTPUT);
pinMode(hornPin, OUTPUT);
pinMode(forwardPin, OUTPUT);
pinMode(reversePin, OUTPUT);
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
char val = 0;
if (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if(val == 's') {
press(stopPin);
}
else if(val == 'h') {
press(hornPin);
}
else if(val == 'f') {
press(forwardPin);
}
else if(val == 'r') {
press(reversePin);
}
else {
delay(100); // Wait 100 milliseconds for next reading
}
}
void press(int pin)
{
digitalWrite(pin, HIGH);
delay(200);
digitalWrite(pin, LOW);
}
Conclusion
The high point of my project was pressing 'h' on my computer and hearing the train whistle go "Toot Toot"!
Hope that was interesting, and hope it gives Arduino newbies (like myself) some circuit bending ideas.
Relevant Links
http://arduino.cc/
http://www.processing.org/
4 comments:
Very nice... love it. Thank you for sharing.
Thanks!
Do you import electronic components from foreign retailers? Please tell about the customs duty incurred.
Good Job there.
I too do stuff like that and share some of your interest. Have a look at my blog: http://m8051.blogspot.com
Post a Comment