/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-servo-motor
*/
#include <Servo.h>
// constants won't change
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin
Servo servo; // create servo object to control a servo
// variables will change:
int angle = 90; // the current angle of servo motor
String ang = "0";
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
boolean isOpening;
boolean isClosing;
boolean isMoving;
void setup() {
Serial.begin(9600); // initialize serial
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(angle);
currentButtonState = digitalRead(BUTTON_PIN);
}
void loop() {
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.print("START ");
// change angle of servo motor
if(angle == 0){
angle = 90;
ang = String(angle);
Serial.println(" servo rotated to " + ang + "deg");
}
else
if(angle == 90){
angle = 0;
ang = String(angle);
Serial.println(" servo rotated to " + ang + "deg");
}
// control servo motor arccoding to the angle
servo.write(angle);
}
}