#include <LiquidCrystal.h> // Include the LCD library
const int sensorPin = 2; // PIR Motion Sensor
const int buzzerPin = 3; // Buzzer
const int lcdRS = 4; // LCD RS pin
const int lcdEN = 5; // LCD EN pin
const int lcdD4 = 6; // LCD D4 pin
const int lcdD5 = 7; // LCD D5 pin
const int lcdD6 = 8; // LCD D6 pin
const int lcdD7 = 9; // LCD D7 pin
LiquidCrystal lcd(lcdRS, lcdEN, lcdD4, lcdD5, lcdD6, lcdD7); // Initialize the LCD
int cowCount = 8;
int expectedHerdSize = 10; // Set the expected herd size
void setup() {
pinMode(sensorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.clear();
}
void loop() {
int sensorState = digitalRead(sensorPin);
if (sensorState == HIGH) {
cowCount++;
digitalWrite(buzzerPin, HIGH);
delay(100); // Short buzzer beep when a cow is detected
digitalWrite(buzzerPin, LOW);
delay(5000); // Adjust delay to avoid multiple counts from the same cow
}
// Display current cow count and expected herd size
lcd.setCursor(0, 0);
lcd.print("Count: ");
lcd.print(cowCount);
lcd.setCursor(0, 1);
lcd.print("Herd: ");
lcd.print(expectedHerdSize);
// Check if the cow count is less, more, or equal to the expected herd size
if (cowCount < expectedHerdSize) {
lcd.setCursor(10, 0); // Move to next available position
lcd.print("Missing");
// Sound alarm for missing cows
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
}
else if (cowCount > expectedHerdSize) {
lcd.setCursor(10, 0); // Move to next available position
lcd.print("Extra");
// Sound alarm for extra cows
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
}
else {
lcd.setCursor(10, 0); // Move to next available position
lcd.print("Correct");
}
delay(2000); // Delay before refreshing the LCD
}