#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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.