i did that (connect the other side of the button to ground) , did not work , maybe i did it wrong but the code works
thanks
#include <Servo.h>
Servo myservo;
byte rightPin = 9;
byte leftPin = 8;
byte servoPin = 13;
byte switchValue = LOW;
int servoStep = 5; // int because we need negative values change value to servo move faster
byte pos = 90; // servo initial position
byte delayPeriod = 20;
void setup() {
myservo.attach (13);
pinMode(rightPin, INPUT_PULLUP); // other side of the button need to be connected to ground
pinMode(leftPin, INPUT_PULLUP); // other side of the button need to be connected to ground
pinMode(servoPin, OUTPUT);
}
void loop() {
readSwitch();
updatePosition();
moveServo();
readSwitch2();
updatePosition2();
moveServo2();
restart();
}
void restart() {
if (digitalRead(rightPin) == LOW && digitalRead(leftPin) == LOW)
{
pos = 90; // restart position
}
}
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 = 5; // change value to servo move faster
}
if( pos > 160) {
pos = 160;
servoStep = -5; // change value to servo move faster
}
}
void moveServo() {
myservo.write(pos);
delay(delayPeriod);
}
void readSwitch2() {
if(digitalRead(rightPin) == HIGH) { // unpressed button will be HIGH
switchValue = LOW;
}
else {
switchValue = HIGH;
}
}
void updatePosition2() {
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 = -5; // change value to servo move faster
}
if( pos > 160) {
pos = 160;
servoStep = +5; // change value to servo move faster
}
}
void moveServo2() {
myservo.write(pos);
delay(delayPeriod);
}
i changed the code again and i added a void restart that when neither of the buttons is pressed the servo goes to position 90
but it does that with no delay, and if i add a delay in void restart it delays all the commands, any idea how i can fix that ?
and that if i press a button in the middle of the restart delay to stop the restart from happening?
thanks