I used the sweep function to control a servo with a rocker switch. Push the switch left, servo swept left; push right, servo swept right. My problem is the user would like to have more control of the servo position rather than having the servo make the full sweep. Push switch left momentarily, release and have servo hold position until switch is pressed again, for example. How is this possible?
Code?
Schematic?
#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
}
Servo test code you can try.
//zoomkat servo button test 12-29-2011
#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);
}
}