#include <Servo.h>
int servoPin = 3;
Servo Servo1;
const int button = 7;
const int led = 8;
int ledState = 0;
void setup() {
Servo1.attach(servoPin);
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
if (digitalRead(button) == HIGH) {
if (ledState == 0) {
ledState = 1;
digitalWrite(led, HIGH);
delay(2000);
Servo1.write(0);
delay(1000);
Servo1.write(100);
delay(1000);
}
}
else {
ledState = 0;
digitalWrite(led, LOW);
Servo1.write(0);
}
}
did you try this?
bool check = true;
void loop() {
if (digitalRead(button) == HIGH && check) {
check = false;
if (ledState == 0) {
ledState = 1;
digitalWrite(led, HIGH);
delay(2000);
Servo1.write(0);
delay(1000);
Servo1.write(100);
delay(1000);
}
else {
ledState = 0;
digitalWrite(led, LOW);
Servo1.write(0);
}
}
if (digitalRead(button) == LOW) {
check = true;
}