#include <Servo.h>
Servo gateServo; // Create a servo object to control the gate
int buttonPin = 2; // Pin connected to the push button
int isOpen = false; // Flag to track the gate state
void setup() {
gateServo.attach(10); // Attach the servo to pin 9
pinMode(buttonPin, INPUT_PULLUP); // Set up the push button pin
Serial.begin(9600);
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
if (!isOpen) {
openGate();
isOpen = true;
} else {
closeGate();
isOpen = false;
}
delay(1000); // Debounce delay
}
}
void openGate() {
Serial.println("Opening gate");
gateServo.write(90); // Assuming 90 degrees opens the gate
delay(100000); // Allow time for the gate to open completely (adjust as needed)
}
void closeGate() {
Serial.println("Closing gate");
gateServo.write(0); // Assuming 0 degrees closes the gate
delay(100000); // Allow time for the gate to close completely (adjust as needed)
}