// Include the LiquidCrystal_I2C library
#include <LiquidCrystal_I2C.h>
// Include the RadioHead library
#include <RH_ASK.h>
// Define the receiver pin
#define RX_PIN 11
// Create an ASK object
RH_ASK rf_driver(2000, RX_PIN);
// Define the bus codes
const char* bus1 = "11000010";
const char* bus2 = "11001110";
const char* bus3 = "11001000";
// Create a LiquidCrystal_I2C object
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define a variable to store the previous received code
char prev_code[9];
// Define a variable to store the time of the last received code
unsigned long last_time;
// Define a function to compare two strings
bool compareStrings(const char* str1, const char* str2) {
for (int i = 0; i < 8; i++) {
if (str1[i] != str2[i]) {
return false;
}
}
return true;
}
void setup() {
// Initialize the serial port
Serial.begin(9600);
// Initialize the ASK object
if (!rf_driver.init()) {
Serial.println("init failed");
}
// Initialize the LCD
lcd.init();
lcd.backlight();
// Initialize the prev_code to empty
strcpy(prev_code, "");
// Initialize the last_time to zero
last_time = 0;
}
void loop() {
// Define a buffer to store the received data
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
// Define a variable to store the length of the received data
uint8_t buflen = sizeof(buf);
// Check if there is any data available
if (rf_driver.recv(buf, &buflen)) {
// Convert the data to a string
char code[9];
for (int i = 0; i < 8; i++) {
code[i] = buf[i] + '0';
}
code[8] = '\0';
// Print the received code
Serial.println(code);
// Check if the code matches any of the bus codes
if (compareStrings(code, bus1)) {
// Display "bus1 arrived"
lcd.clear();
lcd.print("bus1 arrived");
}
else if (compareStrings(code, bus2)) {
// Display "bus2 arrived"
lcd.clear();
lcd.print("bus2 arrived");
}
else if (compareStrings(code, bus3)) {
// Display "bus3 arrived"
lcd.clear();
lcd.print("bus3 arrived");
}
else {
// Display "unknown code"
lcd.clear();
lcd.print("unknown code");
}
// Update the prev_code and the last_time
strcpy(prev_code, code);
last_time = millis();
}
else {
// Check if the last_time is not zero and more than 2 seconds have passed
if (last_time != 0 && millis() - last_time > 2000) {
// Check if the prev_code matches any of the bus codes
if (compareStrings(prev_code, bus1)) {
// Display "bus1 passed"
lcd.clear();
lcd.print("bus1 passed");
}
else if (compareStrings(prev_code, bus2)) {
// Display "bus2 passed"
lcd.clear();
lcd.print("bus2 passed");
}
else if (compareStrings(prev_code, bus3)) {
// Display "bus3 passed"
lcd.clear();
lcd.print("bus3 passed");
}
// Reset the prev_code and the last_time
strcpy(prev_code, "");
last_time = 0;
}
}
}