int button = A0; // Pin of the button
#include <Servo.h> // Include the servo library
Servo servo; // Create a servo object
bool isRotating = false; // State to track if the servo is rotating
void setup() {
servo.attach(3); // Pin used by the servo
pinMode(button, INPUT_PULLUP); // Define button as input with internal pull-up resistor
}
void loop() {
// Check if the button is pressed
if (digitalRead(button) == LOW) { // Button is pressed (LOW due to INPUT_PULLUP)
if (!isRotating) {
// If the servo is not already rotating, start rotating it
isRotating = true;
// Start rotating in one direction (clockwise or counterclockwise)
servo.write(0); // Rotate in one direction (clockwise)
delay(3000); // Rotate for 3 seconds (you can adjust this value for how long the servo should rotate)
// Stop the servo after the rotation (simulates a full 360-degree rotation)
servo.write(90); // Stop the servo (neutral position)
delay(500); // Small delay to ensure the servo has stopped
// After the full rotation, reset the flag
isRotating = false;
}
}
}