const int pirPin = 2; // PIR sensor output pin
const int relayPin = 3; // Relay module control pin
const int feedDuration = 5000; // Feed dispensing time in milliseconds (5 seconds)
void setup() {
pinMode(pirPin, INPUT); // Set PIR sensor pin as input
pinMode(relayPin, OUTPUT); // Set relay control pin as output
digitalWrite(relayPin, LOW); // Ensure relay is off initially
Serial.begin(9600); // Initialize serial communication for debugging
Serial.println("Bird Feeder System Ready.");
}
void loop() {
int motionDetected = digitalRead(pirPin); // Read PIR sensor state
if (motionDetected == HIGH) { // If motion is detected
Serial.println("Motion detected! Dispensing feed...");
digitalWrite(relayPin, HIGH); // Activate relay (turn on feeder)
delay(feedDuration); // Dispense feed for set duration
digitalWrite(relayPin, LOW); // Deactivate relay (turn off feeder)
Serial.println("Dispensing complete.");
// Wait until motion stops to avoid multiple triggers
while (digitalRead(pirPin) == HIGH) {
delay(100); // Small delay to reduce CPU usage
}
Serial.println("Waiting for next motion...");
}
}