Servo Control with 3 position dpdt Momentary Switch

Can someone please help me, I need to know how to wire this switch

http://www.servocity.com/html/dpdt_20-amp_momentary_flip_swi.html#.Ux3BcPlkSSo

to work with this code:

// Oscar's Project
//
// There are 2 input buttons (turn left and right), when button is pressed, the servo turns and corresponding LED is lit up.

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 90; // variable to store the servo position
const int maxDeg = 160;
const int minDeg = 5;

const int leftPin = 3;
const int rightPin = 2;

const int led1Pin = 6; // indicator
const int led2Pin = 5; // indicator

const int outputPin = 9; // pwm function will be disabled on pin 9 and 10 if using servo

int leftPressed = 0;
int rightPressed = 0;

void setup()
{
myservo.attach(outputPin); // attaches the servo on pin 9 to the servo object
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
}

void loop()
{
leftPressed = digitalRead(leftPin);
rightPressed = digitalRead(rightPin);

if(leftPressed){
if(pos < maxDeg) pos += 3;
myservo.write(pos); // tell servo to go to position in variable 'pos'
digitalWrite(led1Pin,HIGH);
}
else
digitalWrite(led1Pin,LOW);

if(rightPressed){
if(pos > minDeg) pos -= 3;
myservo.write(pos); // tell servo to go to position in variable 'pos'
digitalWrite(led2Pin,HIGH);
}
else
digitalWrite(led2Pin,LOW);

delay(15); // waits 15ms for the servo to reach the position

}

any help is greatly appreciated!!

Thanks,
Peter

That sketch uses pins 2 and 3 as inputs leftPin and rightPin. It does a digitalRead on the pins to determine if they are HIGH or LOW. The servo is turned in the direction of whichever pin is HIGH.

I THINK that switch is going to put the upper left pin (looking at the picture on the link) pos sometimes and the lower left pin pos sometimes depending on switch position. I would make one of them pin 2 and one of them pin 3. Might need pull down resistors to elminate float.

That is a 12v switch. 5v only to the Arduino pins.

That switch is overkill but you can connect it so it behaves as two momentary switches and then connect those to inputs will pullups enabled in the usual way.

yea..it is an overkill....but its going on a motorcycle..

so basically...im am feeding 12 v into arduino VIN..then feeding the switch with the 5v out from arduino.

Now, where im getting stuck is the actual wiring of the switch...

it has 6 poles...the center ones would be ground (off) position..or positive?...

Looking at the picture in your link the switch connects the centre tags to the tags at one side or the other depending how the switch lever is moved. It doesn't matter to the switch which way the power flows through it.

Whether you connect the centre tag to Arduino ground or 5v (and the side tags to pins 2 and 3) depends on whether you want the signal on pins 2 and 3 to be high or low when the switch is NOT pressed.

...R

voodoo28:
it has 6 poles...the center ones would be ground (off) position..or positive?...

Yes, the center poles are power. Sorry, should have said that in my answer. I mistakenly thought you were confused as to how to wire the other poles to the Arduino signal pins.

ok so if i make the center power....do i need to cross the other poles for the inputs?...and the switch would not need a ground??

Sorry for all the confusion....being a newbie SUCKS!

I would connect the center poles to power and ground. I THINK the left top and left bottom poles are what you want to read as Pin 2 and Pin 3.

I would try connecting the upper left to Arduino 2 and to a resistor to ground. Arduino ahead of the resistor. When that line goes HIGH Arduino should see it. Same with lower left, to Arduino 3, and to a resistor and ground.

The switch and the Arduino would have a common ground.

as you can use internal pull up, use the center and connect with gnd 0V
the top to input2
and bottom to input3
the servo will move in about 0.5 seconds from middle to outer position.
you will need to put it manual back or use a timeout: after 30 seconds automatic go to 90 again.

Below is some servo/button test code you might try. As to the switch you probably need to only one on-off-on side with the center tab going to the arduino ground and the end pins each going to an arduino input pin. Radio Shack has that type switch for ~$5, but it may not be as well built.

//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

Given the switch characteristics described, I would connect a common (center) terminal to ground and connect the corresponding end terminals to digital inputs will pull-ups enabled. In this way the center position would read as both inputs high, and each end position would pull the corresponding input low.

PeterH:
Given the switch characteristics described, I would connect a common (center) terminal to ground and connect the corresponding end terminals to digital inputs will pull-ups enabled. In this way the center position would read as both inputs high, and each end position would pull the corresponding input low.

Would I then have to modify the code?

zoomkat:
Below is some servo/button test code you might try. As to the switch you probably need to only one on-off-on side with the center tab going to the arduino ground and the end pins each going to an arduino input pin. Radio Shack has that type switch for ~$5, but it may not be as well built.

//zoomkat servo button test 12-29-2011

// Powering a servo from the arduino usually DOES NOT WORK.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }   
 
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

yes..thats exactly where i got the switch..