#include <Servo.h>
// Define the pin numbers
const int servoPin = 7;
const int buttonPin = 3;
// Create a servo object
Servo myServo;
// Variable to store the current state of the button
int buttonState = 0;
// Variable to store the previous state of the button
int prevButtonState = 0;
void setup() {
// Attach the servo to the pin
myServo.attach(servoPin);
// Set the button pin as input
Serial.begin(9600);
myServo.write(90);
pinMode(buttonPin, INPUT);
}
// Check if the button is pressed
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// Check for a state change
if (buttonState != prevButtonState) {
if (buttonState == LOW) {
// Turn the servo to 180 degrees
myServo.write(180);
Serial.println("Button pressed. Servo turning to 180 degrees.");
Serial.println("Button pressed. Gate Open.");
delay(4000);
}
// Update the previous button state
prevButtonState = buttonState;
}
if (buttonState == HIGH)
{
Serial.println("Gate Closed");
myServo.write(90);
delay(200);
}
// Update the previous button state
}