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
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