#include <Servo.h>
Servo myservo;
byte leftPin = 8;
byte servoPin = 13;
byte switchValue = LOW;
int servoStep = 1; // int because we need negative values
byte pos = 90; // servo initial position
byte delayPeriod = 20;
void setup() {
myservo.attach (13);
pinMode(leftPin, INPUT_PULLUP);
pinMode(servoPin, OUTPUT);
}
void loop() {
readSwitch();
updatePosition();
moveServo();
}
void readSwitch() {
if(digitalRead(leftPin) == HIGH) { // unpressed button will be HIGH
switchValue = LOW;
}
else {
switchValue = HIGH;
}
}
void updatePosition() {
if (switchValue == LOW) {
pos = pos + servoStep;
}
if( pos < 20) { // some servos can't go all the way from 0 to 180
// adjust to suit your servo
pos = 20;
servoStep = 1;
}
if( pos > 160) {
pos = 160;
servoStep = -1;
}
}
void moveServo() {
myservo.write(pos);
delay(delayPeriod);
}
hey i changed it and it works thanks !!!