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