#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Define the pin numbers for the LEDs and buttons
const int GoLED = 15; //pin number for Green LED
const int FullLED = 2; //pin number for the Red LED
const int GObutton = 5; //pin number for the green push button
const int OUTbutton = 18; //pin number for the red push button
const int RESETbutton = 19; //pin number for black push button
//These variables store the current number of occupants and the reset flag
int x = 0;
int RESET = 0;
//Interrupt service routine to handle the RESET button press
void IRAM_ATTR Restart(){
RESET = true;
}
void setup(){
//initialize the LCD and turn on the backlight
lcd.init();
lcd.backlight();
delay(10);
//Initialize serial communication for debugging at 115200 baud
Serial.begin(115200);
Serial.print("PROGRAM READY");
delay(10);
//Set pin modes for the LEDs and buttons
pinMode(GObutton, INPUT);
pinMode(OUTbutton, INPUT);
pinMode(RESETbutton, INPUT);
pinMode(GoLED, OUTPUT);
pinMode(FullLED, OUTPUT);
//Attach an interrupt to the RESET button that will call the "Restart" function when the button is pressed
attachInterrupt(RESETbutton, Restart, RISING);
//Print "OCCUPANT/S" on the second line of the LCD
lcd.setCursor(6,1);
lcd.print("OCCUPANT/S");
delay(10);
}
void loop(){
//if the GO button is pressed, increment the number of occupants
if (digitalRead(GObutton)==HIGH){
x++;
delay(300);
}
//if the OUT button is pressed, decrement the number of occupants
else if (digitalRead(OUTbutton)==HIGH){
x--;
delay(300);
}
//if the number of occupants is less than 10, turn on the GoLED and off the FullLED
if(x < 10){
digitalWrite(GoLED, HIGH);
digitalWrite(FullLED, LOW);
lcd.setCursor(4, 0);
lcd.print("WELCOME");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(x);
delay(100);
}
//if the number of occupants is 10 or more, turn on the FullLED and off the GoLED
else if (x >= 10){
digitalWrite(FullLED, HIGH);
digitalWrite(GoLED, LOW);
lcd.setCursor(4, 0);
lcd.print(" CLOSED");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(x);
x = 10;
delay(100);
}
//if the reset flag is set, reset the number of occupants to 0 and display the welcome message
if (RESET){
x = 0;
digitalWrite(GoLED, HIGH);
lcd.setCursor(4, 0);
lcd.print("WELCOME");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(x);
RESET = false;
delay(100);
}
}