Generation

generate functionTue, 18 Apr 2023

#include <Servo.h> // Pin definitions const int pirPin = 2; // PIR sensor connected to pin 2 const int buttonPin = 4; // Button connected to pin 4 const int servoPin = 9; // Servo connected to pin 9 const int tiltPin = A0; // Tilt sensor connected to analog pin A0 // Global variables Servo myServo; int tiltVal; void setup() { pinMode(pirPin, INPUT); pinMode(buttonPin, INPUT_PULLUP); pinMode(tiltPin, INPUT); myServo.attach(servoPin); myServo.write(0); // Servo starts in closed position } void loop() { // Check for car arrival while (digitalRead(pirPin) == LOW) { delay(100); // Wait for car to arrive } // Car has arrived, wait for button press while (digitalRead(buttonPin) == HIGH) { delay(100); // Wait for button press } // Button has been pressed, open the gate myServo.write(90); // Gate opens delay(3000); // Wait for car to pass myServo.write(0); // Gate closes // Check tilt sensor for gate position tiltVal = analogRead(tiltPin); if (tiltVal < 500 && digitalRead(pirPin) == LOW) { myServo.write(0); // Gate is closed } else { myServo.write(90); // Gate is open } }

int main() { }

Want to kickstart your project?Use the new AI Studio to create your code