/*
* This Arduino Nano code was developed by newbiely.com
*
* This Arduino Nano code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-servo-motor
*/
#include <Servo.h>
const int BUTTON_PIN = 2;
const int SERVO_PIN1 = 9;
const int SERVO_PIN2 = 10;
Servo servo1;
Servo servo2;
int angle = 0;
int currentButtonState;
int motion = 0;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
servo1.attach(SERVO_PIN1);
servo2.attach(SERVO_PIN2);
servo1.write(angle);
servo2.write(angle);
currentButtonState = digitalRead(BUTTON_PIN);
}
void loop() {
currentButtonState = digitalRead(BUTTON_PIN);
if (currentButtonState == LOW)
{
Serial.println("The button is pressed");
for (int i = 0; i < 3; i++)
{
if (angle == 0)
{
angle = 90;
servo1.write(angle);
servo2.write(angle);
delay(1000);
}
if (angle == 90)
{
angle = 0;
servo1.write(angle);
servo2.write(angle);
delay(1000);
}
}
}
motion = 0; // reset motion to 0 for next button press
}