#include <Servo.h> // import the Servo library
Servo servo; // create servo object to control a servo
#define SECONDS 5 // seconds servo will be at 180 degrees
#define SLOWER 15 // value to slow servo movement
int buttonPin = 2; // declare a button pin
int servoPin = 3; // declare the servo pin
int ledPin1 = 4; // add an LED on pin 4
int ledPin2 =12; // add an LED on pin 12
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
servo.attach(servoPin); // attach the servo instance to the servo PWM pin
servo.write(0); // rotate the servo to 90 degrees.
}
void loop() {
digitalWrite(ledPin2, HIGH); // turn LED ON
if (!digitalRead(buttonPin)) { // is the button pressed?
delay(50); // this is a quck way to debounce the button ringing noise
if (digitalRead(buttonPin)) { // is the button released?
digitalWrite(ledPin2, LOW); // turn LED OFF
for (int i = 0; i < 180; i++) {
servo.write(i); // rotate the servo to 180 degrees
delay(SLOWER);
}
digitalWrite(ledPin1, HIGH); // turn LED ON
for (int i = 0; i < SECONDS; i++) {
Serial.print(5 - i);
delay(1000); // one second
}
digitalWrite(ledPin1, LOW); // turn LED OFF
Serial.println("0");
for (int i = 0; i < 180; i++) {
servo.write(180 - i); // rotate the servo to 0 degrees
delay(SLOWER);
}
}
}
}
5 SECONDS
HOME
PUSH