简体   繁体   中英

How to make a servo push a button with Arduino?


I'm trying to replicate the toy "uselsess box" with Arduino but in a different version.
I have a button, a led and a servo motor.
When I click the button the led lights up and after 2 seconds I want the servo motor to rotate and push that button again to turn off the led.
The problem is that when I press the button the led lights up and at the same time the servo motor moves, in this way when it clicks the button the led does not turn off.
Here's the code:
 #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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM