#include <Servo.h>
const int button = 3;
const int servo1 = 5;
Servo servo;
int angle = 0; //sets the constant angle of the servo
bool buttonState = false;
bool recButtonState = false;
void setup() {
servo.attach(servo1);
pinMode(button, INPUT);
servo.write(angle);
}
void loop() {
buttonState = digitalRead(button);
if (buttonState != recButtonState) {
if (buttonState == HIGH) { // the following code will execute when the button is pressed
angle = (angle == 0) ? 90 : 0; //the ? is a conditional operator it will change the value to 90 if the angle is 0 if not it will remain 0.
servo.write(angle);
delay(50);
}
delay(50);
}
recButtonState = buttonState;
}