// https://forum.arduino.cc/t/my-code-for-is-not-working/1074619
//
#include<Servo.h> //include the servo library
int BUTTON1 = 5; //pin of the first button
int BUTTON2 = 4; //pin of the second button
int BUTTON3 = 3; //pin of the third button
int BUTTON4 = 2; //pin of the fourth button
int servoPin = 9; // Servo pin
Servo servo; //create a servo object
int pos = 0; //initial position of the servo
void setup() {
// put your setup code here, to run once:
//pin used by the servo
servo.attach(9);
pinMode(BUTTON1, INPUT_PULLUP); //define first button as input pullup
pinMode(BUTTON2, INPUT_PULLUP); //define second button as input pullup
pinMode(BUTTON3, INPUT_PULLUP); //define third button as input pullup
pinMode(BUTTON4, INPUT_PULLUP); //define fourth button as input pullup
}
void loop() {
if ((digitalRead(BUTTON2) == LOW) && (digitalRead(BUTTON3) == LOW)) { //if Value read of the button ==LOW:
pos++; //increases the value of the "pos" variable each time the push button of the left is pressed
// Keep the angle 'pos' from 0 to 180.
pos = constrain(pos, 0, 180);
delay(5); //5 milliseconds of delay
servo.write(pos); //servo goes to variable pos
}
if ((digitalRead(BUTTON1) == LOW) && (digitalRead(BUTTON4) == LOW)) { //if Value read of the button ==LOW:
pos--; //decreases the value of the "pos" variable each time the push button of the right is pressed
// Keep the angle 'pos' from 0 to 180.
pos = constrain(pos, 0, 180);
delay(5); //5 milliseconds of delay
servo.write(pos); //servo goes to variable pos
}
}