How to make a servo push a button with Arduino?

  arduino, arduino-c++, c++, servo

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);
   }
 }

Source: Windows Questions C++

LEAVE A COMMENT