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
Bracket
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
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 wooodystime.blogspot.com for more info.
//
//Kevin Woodyard - Str8shotPhoto.com
//2013
 
AF_DCMotor motor(1, MOTOR12_64KHZ); 
// create motor #1, 64KHz pwm 
 
AF_DCMotor motor2(2, MOTOR12_64KHZ); 
// create motor #2, 64KHz pwm 

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


Good Luck and Keep Wasting Time.

Comments