#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define IR1_PIN 2
#define IR2_PIN 3
#define IR3_PIN 4
#define IR4_PIN 5
bool ir1_detected = false;
bool ir2_detected = false;
bool ir3_detected = false;
bool ir4_detected = false;
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(IR1_PIN, INPUT_PULLUP);
pinMode(IR2_PIN, INPUT_PULLUP);
pinMode(IR3_PIN, INPUT_PULLUP);
pinMode(IR4_PIN, INPUT_PULLUP);
}
void loop()
{
ir1_detected = digitalRead(IR1_PIN) == LOW;
ir2_detected = digitalRead(IR2_PIN) == LOW;
ir3_detected = digitalRead(IR3_PIN) == LOW;
ir4_detected = digitalRead(IR4_PIN) == LOW;
if (ir1_detected && ir2_detected)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Train Arrived");
delay(2000);
}
if (ir3_detected && ir4_detected)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Train Departed");
delay(2000);
}
if (ir1_detected && ir3_detected)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Possible Train Collision!");
delay(2000);
}
if (ir4_detected && !ir3_detected)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Possible Train Collision!");
delay(2000);
}
}