Order your own BreadBox kit
A five position switch is like a joystick. You can push it to all four sides and you can push down in the center. Each of these motions triggers a switch that can be read by the Arduino. In this project we will wire up the five position switch to the Arduino and use it to control the BreadBox servos.
In addition to the BreadBox kit you will need:
Soldering iron, nippers, needle nose pliers
Prepare Header - We will only need five of the six positions on
the header. Use the soldering iron to tin five of the pins sticking
out of the header.
Attach 5 Wires To Header - Use the needle nose
pliers to make a small loop in the tinned end of 5 of the cut wires.
Take one of the wires and slip the loop over one of the tinned pins
on the header. Use the pliers to squeeze the loop tight. With the
soldering iron solder this wire to the pin. This is where that
pre-tinning makes the job much easier.
Attach 5 Wires To Joystick - Solder the five
wires to the MIDDLE five contacts on the switch. We want to have the
following contacts wired to the header: Up, Down, Left, Right,
Center.
Connect the Cables - If you have an assembled
BreadBox, then remove the cables from the analog inputs. Place the
six connector header on the sensor shield so that it spans the S
input for five of the analog inputs. With the software below you can use the joystick to make the servos move slowly clockwise or counter clockwise (the left and right buttons); slowly spread open or close (the up and down buttons); return to the 90 degree position (center button).
Here's a short movie of the whole thing running.
/*
* five position thumb control servo test jig
* Left / Right will move servos left and right
* Up / Down will move servos 3,5,6 one way and 9,10,11 the other
* Center press will move them all to 0
*
* (c) 2013 Jim Schrempp shrimpware.com
*/
#include <Servo.h>
#include <Serial.h>
// input pins for switches
const int c_up = 0;
const int c_middle = 1;
const int c_left = 2;
const int c_down = 3;
const int c_right = 4;
Servo servo3;
Servo servo5;
Servo servo6;
Servo servo9;
Servo servo10;
Servo servo11;
const int c_servoHomePosition = 90;
int servoLeftPosition = c_servoHomePosition; // Initial position is the home position
int servoRightPosition = c_servoHomePosition;
void setup() {
servo3.attach(3);
servo5.attach(5);
servo6.attach(6);
servo9.attach(9);
servo10.attach(10);
servo11.attach(11);
Serial.begin (9600);
while (!Serial) {
// wait for the Serial interface to become ready
}
Serial.println ("Initalized");
}
void LeftServosToPosition (int newPos) {
servo3.write(newPos) ;
servo5.write(newPos);
servo6.write(newPos);
}
void RightServosToPosition (int newPos) {
servo9.write(newPos);
servo10.write(newPos);
servo11.write(newPos);
}
void loop() {
// Read each switch. If the switch is Low (which is ON) then adjust the
// appropriate servo positions
int readValueL = analogRead(c_left);
if (readValueL < 500) {
servoLeftPosition = servoLeftPosition - 1;
servoRightPosition = servoRightPosition - 1;
}
int readValueR = analogRead (c_right);
if (readValueR < 500) {
servoLeftPosition = servoLeftPosition + 1;
servoRightPosition = servoRightPosition + 1;
}
int readValueU = analogRead (c_up);
if (readValueU < 500) {
servoLeftPosition = servoLeftPosition + 1;
servoRightPosition = servoRightPosition - 1;
}
int readValueD = analogRead (c_down);
if (readValueD < 500) {
servoLeftPosition = servoLeftPosition - 1;
servoRightPosition = servoRightPosition + 1;
}
int readValueM = analogRead (c_middle);
if (readValueM < 500) {
servoLeftPosition = c_servoHomePosition;
servoRightPosition = c_servoHomePosition;
}
// Make sure the servo positions haven't gone too far
if (servoLeftPosition < 0) servoLeftPosition = 0;
if (servoLeftPosition > 180) servoLeftPosition = 180;
if (servoRightPosition < 0) servoRightPosition = 0;
if (servoRightPosition > 180) servoRightPosition = 180;
// Move the servos to position
LeftServosToPosition ( servoLeftPosition );
RightServosToPosition ( servoRightPosition );
// This bit is to make sure that we only write to the
// serial port once a second. Without this kind of check
// the code would write to the serial port every time
// the main loop is executed, and that will overrun
// the output buffer very quickly.
static long lastWriteTime = 0;
long currentTime = millis();
if (currentTime - lastWriteTime > 1000) {
lastWriteTime = currentTime;
Serial.println ("left, right, up, down, middle, left position, right position ");
Serial.print(readValueL); Serial.print(", ");
Serial.print(readValueR); Serial.print(", ");
Serial.print(readValueU); Serial.print(", ");
Serial.print(readValueD); Serial.print(", ");
Serial.print(readValueM); Serial.print(", ");
Serial.print(servoLeftPosition); Serial.print(", ");
Serial.print(servoRightPosition);
Serial.println ("");
}
delay(15);
}
(c) 2013Shrimpware LLC All Rights Reserved