Sunday, November 10, 2013

Adding a Start Switch to Woody Bot

Start Switch for Woody Bot
If you have done anything with an Arduino based sketch you know that when it is done uploading or the second you apply power it starts running whatever it is programmed to do. This is fine except when your dealing with a robot that will run off the desk and crash to the floor. My solution with the help of some great people in the Arduino Forum was to put in a momentary push button switch that very basically holds the program until you push  the button. At which point it will then run the program until you remove power.

The Hardware.
For a switch I used some I already had laying around. But you can pick these up very cheap at Radio Shack. It is a very small Momentary push button Switch that is Normally Open and rated at 0.5A Radio Shack Part Number 275-1547. You get 4 switches for $3.99. Not too bad.

Bracket with holes drilled
Countersunk for the Switch
Add caption
I needed a way to mount the switch to the Woody Bot. I played will aluminum and an 1/8 piece of Plexiglas, both proved to be too flimsy to work. I finally decided on a piece of 1/4" black Lexan I had a scrap of. I made a piece 1 1/2" x 5/8" and drilled a 1/4" hole in one end and a 3/16" hole in the other. I then counter sunk a 7/16" hole using a spade bit to allow the switch to be recessed enough for the nut to be put on. I removed all the protective paper and mounted the switch on the bracket. I mounted the bracket off of one of the upper corners of the Arduino base Using the existing screw.

Bracket mounted on Arduino base
Wiring is pretty straight forward. First you must find a free pin on the Arduino. The problem is that the Adafruit Motorshield in not stack-able friendly and terminates all of its pins on top. Inside the Adafruit AFMotor library there is a schematic file that I found that shows that Pins A0 to A5 are NOT used by the Motorshield. So decided on pin A0 to ground for my switch. Fortunately there us a small breakout board on the shield to help you out.

Wires soldered to A0 and Grd
Pins soldered from underneath
I sacrificed a couple of female to female jumper wires by soldering one end to the open pins on the breakout board. On the board I soldered the pins from the bottom, it just works better. I might add some jumper blocks to these later for easier wiring. I routed the wires underneath through the same hole as my power wires, cut off and stripped the ends and soldered them to the switch.

The Code
If your like me your not taking as much time as you should and learning how the Arduino code actually works as wellas you should. But that's ok, we can still figure it out with the help of other folks who did take the time ;-). So the first thing we need to know is that the sketch for an arduino is in two sections. The first section is void setup() { setuphereand the second section is void loop() {Work here}.
Completed start switch wiring

void setup is basically where you tell the arduino what your going to hook up to it, how to set things up to run and set starting variables. It does a lot more but for now that explanation will suffice.

Start Switch Done.
void loop is where the work is actually done. This is where you tell the Arduino what to monitor, power, run, turn on and off etc. So with that in mind to prevent a Loop from starting we have to add our code to the Void Setup towards the top. The first bit of code that we add is in

pinMode(A0, INPUT_PULLUP);
// make pin an input with internal pullup

What this does is set up pin A0 as an input waiting for something to change its state. Using INPUT_PULLUP you utilize an internal resistor rather than having to add a resistor to the circuit externally. The Inital State of the Pullup is HIGH and providing 5v to the input. With a switch wired from Pin A0 to Grd, when you press the normally open switch and close it you set the pin to LOW causing a change in state that will satisfy the following line and start looping your sketch.

Place the following bit of code at the very bottom of the Void Setup Section before its last }.

while (digitalRead(A0) == HIGH){ // do nothing }

This line will Read Pin A0 when the void setup section runs if it is still HIGH then it stops the program and waits for a change in state. When you depress your switch it changes its state and the Void Loop will start running until your remove power.

Anything you put between the {  } the Arduino will do. So we could  possibly turn on an LED or print a serial statement saying were waiting for the button push. I will experiment more on that later though.

Here is the complete test code with the Switch bits added in. I have tested it and it works just as expected. Next I want to figure out how to us ethe same switch to stop the  loop and return it to the start.

#include <AFMotor.h> 
//Must have Adafruit MotorShield Library installed. 
//Check out for more info.
//Kevin Woodyard -
AF_DCMotor motor(1, MOTOR12_64KHZ); 
// create motor #1, 64KHz pwm 
AF_DCMotor motor2(2, MOTOR12_64KHZ); 
// create motor #2, 64KHz pwm 

void setup() { 
 // set up Serial library at 9600 bps 
 Serial.println("Woody Bot test!"); 
 pinMode(A0, INPUT_PULLUP); // make pin an input with internal pullup
 // set the speed to 200/255 
 while (digitalRead(A0) == HIGH){
// do  nothing
void loop() { 
 Serial.print("Move forward");;; 
 // turn both wheels forward, should go in a straight line.
 //adjust individual motor speed to correct for any curving in travel
 Serial.print("Spin to the left");;;
 // Spin to the left for 2 seconds by counter rotating the motors
 Serial.print("Spin to the right");;;
 // Spin to the right using the oposite rotations.
 // Release the motors and wait 2 seconds before starting again.
// REPEAT OF first part of SKETCH with spins reversed
Serial.print("Move Forward 2");;; 
 // Turn on both wheels going forward 
  Serial.print("Spin to the Right");;;
 // Spin to the right for 2 seconds by counter rotating the motors
 Serial.print("Spin to the Left");;;
// Spin to the Left using the oposite rotations.
 // stopped 

Good Luck and Keep Wasting Time.

No comments: