Hello, I'm using arduino nano and I wish to have a push button that when it is pressed at the first time, it will rotate 90 degrees. And for the 2nd time, it will go back to 0 degrees. I'm having trouble with this.
#include <Servo.h>
int servoPin = 3;
int button = 4;
int buttonread = 0;
int status = 0;
Servo Servo1;
void setup() {
Servo1.attach(servoPin);
pinMode(button, INPUT);
}
void loop{
buttonread = digitalRead(button);
if(buttonread != 0){
if(status == 0){
rotate();
delay(500);
}else{
rotate1();
delay(500);
}
}
void rotate(){
Servo1.attach(servoPin);
Servo1.write(90);
delay(500);
Servo1.detach();
status = 90;
delay(1000);
}
void rotate1(){
Servo1.attach(servoPin);
Servo1.write(0);
delay(500);
Servo1.detach();
status = 0;
delay(1000);
}