I'm new to Arduino and I need help, tweaking my code. I need the servo to turn 180 to 0 on the initial press. Then when pressed again, I need the servo to go from 0 back to 180. So far all I have is the servo to go from 180 to 0 on the first press but wont go back on another press or at all (we have to manually wind it back and press again). Im also using an arduino uno
#include <Servo.h>
Servo servo1;
const int BUTTON = 4;
int val = 0;
int old_val = 0;
int state = 0;
void setup() {
servo1.attach(7);
pinMode(BUTTON, INPUT);
digitalWrite(4, HIGH);
}
void loop() {
val - digitalRead(BUTTON);
if ((val == HIGH) && (old_val == LOW)){
state = 1 - state;
}
old_val = val;
if (state == 1) {
servo1.write(0);
} else {
servo1.write(180);
}
}