#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Constants for button pins
const int BUTTON1_PIN = 0; // Change these to match Wokwi pin assignments
const int BUTTON2_PIN = 13;
const int BUTTON3_PIN = 14;
// Constants for LED pins
const int LED_PIN = 12; // LED for indicating the mode
const int SPRAY_LED_PIN = 26; // Additional LED for spraying status
// LCD and button variables
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address, number of columns, number of rows
bool button2Pressed = false;
bool button3Pressed = false;
// Timing constants
const int STAFF_MODE_COUNTDOWN = 5; // seconds
const int VISITOR_MODE_COUNTDOWN = 4; // seconds
const int STAFF_SPRAYING_TIME = 5; // seconds
const int VISITOR_SPRAYING_TIME = 4; // seconds
void setup() {
// Initialize LCD
lcd.init(); // Use init() instead of begin() for some LiquidCrystal_I2C libraries
lcd.backlight(); // Turn on the backlight
// Initialize button pins
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
pinMode(BUTTON3_PIN, INPUT_PULLUP);
// Initialize LED pins
pinMode(LED_PIN, OUTPUT);
pinMode(SPRAY_LED_PIN, OUTPUT);
// Display initial message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please select your");
lcd.setCursor(0, 1);
lcd.print("mode");
}
void loop() {
if (digitalRead(BUTTON1_PIN) == LOW) {
delay(500); // Debounce
while (digitalRead(BUTTON1_PIN) == LOW); // Wait for button release
startSelection();
}
if (button2Pressed) {
handleMode(STAFF_MODE_COUNTDOWN, STAFF_SPRAYING_TIME);
button2Pressed = false;
resetSelection();
}
if (button3Pressed) {
handleMode(VISITOR_MODE_COUNTDOWN, VISITOR_SPRAYING_TIME);
button3Pressed = false;
resetSelection();
}
}
void startSelection() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select mode:");
while (true) {
if (digitalRead(BUTTON2_PIN) == LOW) {
delay(500); // Debounce
while (digitalRead(BUTTON2_PIN) == LOW); // Wait for button release
button2Pressed = true;
break;
}
if (digitalRead(BUTTON3_PIN) == LOW) {
delay(500); // Debounce
while (digitalRead(BUTTON3_PIN) == LOW); // Wait for button release
button3Pressed = true;
break;
}
}
}
void handleMode(int countdownSeconds, int sprayingTime) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mode selected");
digitalWrite(LED_PIN, HIGH); // Turn on mode LED
delay(2000); // Display "Staff selected" or "Visitor selected" for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please stand under");
lcd.setCursor(0, 1);
lcd.print("the spray");
delay(2000); // Display for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Spray will start in");
for (int i = countdownSeconds; i > 0; i--) {
lcd.setCursor(0, 1);
lcd.print(i);
lcd.print(" seconds");
delay(1000); // Wait for 1 second
}
// Show spraying message and control second LED
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Spraying...");
for (int i = sprayingTime; i > 0; i--) {
lcd.setCursor(0, 1);
lcd.print("Time left: ");
lcd.print(i);
lcd.print(" sec");
// Light up the second LED based on time left
digitalWrite(SPRAY_LED_PIN, HIGH); // Turn on second LED
delay(1000); // Wait for 1 second
digitalWrite(SPRAY_LED_PIN, LOW); // Turn off second LED briefly
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please proceed");
delay(2000); // Display for 2 seconds
digitalWrite(LED_PIN, LOW); // Turn off mode LED
}
void resetSelection() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please select your");
lcd.setCursor(0, 1);
lcd.print("mode");
}