//CASTRO, AUBREY A.
//NW-301
//SHWINT - Mid Individual Assignment 3
//Servo with Button to Start/Stop the movement
#include <Servo.h>
Servo myServo;
int buttonPin = 2;
bool servoActive = false;
bool lastButtonState = HIGH;
void setup() {
Serial.begin(9600);
myServo.attach(9);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
bool currentButtonState = digitalRead(buttonPin);
if (lastButtonState == HIGH && currentButtonState == LOW) {
servoActive = !servoActive;
delay(50);
}
lastButtonState = currentButtonState;
if (servoActive) {
for (int i = 0; i <= 180; i++) {
myServo.write(i);
Serial.print("Servo current angle: ");
Serial.println(i);
delay(15);
if (digitalRead(buttonPin) == LOW) {
servoActive = false;
break;
}
}
for (int i = 180; i >= 0; i--) {
myServo.write(i);
delay(15);
if (digitalRead(buttonPin) == LOW) {
servoActive = false;
break;
}
}
}
}