Start Switch for Woody Bot |
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 |
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 |
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() { setuphere} and 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. |
pinMode(A0, INPUT_PULLUP);
// make pin an input with internal 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