#include <ArduinoQueue.h>
#define DOORCLOSED 0
#define SEAT1 4
#define SEAT2 7
#define SEAT3 10
#define SEAT4 13
#define RED1 3
#define GREEN1 2
#define RED2 6
#define GREEN2 5
#define RED3 9
#define GREEN3 8
#define RED4 12
#define GREEN4 11
#define GREENDURATION 3000 // 3 seconds
// Structure to store button press information
struct ButtonPress {
int buttonNumber;
};
// Create a queue of ButtonPress structures with a maximum capacity of 10 items
ArduinoQueue<ButtonPress> buttonQueue(10);
unsigned long previousGreenMillis = 0;
bool doorClosedState = false;
bool prevDoorState = false; // Variable to keep track of the previous state of the door button
int currentGreenLedPin = -1;
bool greenLedOff = true;
void setup() {
Serial.begin(9600);
// Initialize button pins
pinMode(DOORCLOSED, INPUT_PULLUP);
pinMode(SEAT1, INPUT_PULLUP);
pinMode(SEAT2, INPUT_PULLUP);
pinMode(SEAT3, INPUT_PULLUP);
pinMode(SEAT4, INPUT_PULLUP);
// Initialize LED pins
pinMode(RED1, OUTPUT);
pinMode(GREEN1, OUTPUT);
pinMode(RED2, OUTPUT);
pinMode(GREEN2, OUTPUT);
pinMode(RED3, OUTPUT);
pinMode(GREEN3, OUTPUT);
pinMode(RED4, OUTPUT);
pinMode(GREEN4, OUTPUT);
// Turn off all LEDs initially
digitalWrite(RED1, LOW);
digitalWrite(GREEN1, LOW);
digitalWrite(RED2, LOW);
digitalWrite(GREEN2, LOW);
digitalWrite(RED3, LOW);
digitalWrite(GREEN3, LOW);
digitalWrite(RED4, LOW);
digitalWrite(GREEN4, LOW);
// Read the initial state of the door and set prevDoorState accordingly
prevDoorState = digitalRead(DOORCLOSED);
}
void loop() {
// Check button presses
checkButton(SEAT1, 1, RED1, GREEN1);
checkButton(SEAT2, 2, RED2, GREEN2);
checkButton(SEAT3, 3, RED3, GREEN3);
checkButton(SEAT4, 4, RED4, GREEN4);
// Check if door button state has changed
bool doorState = digitalRead(DOORCLOSED);
if (doorState != prevDoorState && doorState == LOW) { // Door button pressed
doorClosedState = !doorClosedState; // Toggle the state
Serial.print("Door closed state: ");
Serial.println(doorClosedState ? "LOCKED" : "UNLOCKED");
if (!doorClosedState && !buttonQueue.isEmpty()) {
buttonQueue.dequeue();
Serial.print("Queue advanced. ");
Serial.print(buttonQueue.itemCount());
Serial.println(" in queue.");
// Reset the timer for turning off the previous green LED
previousGreenMillis = millis();
// Turn off the current green LED
if (currentGreenLedPin != -1) {
digitalWrite(currentGreenLedPin, LOW);
currentGreenLedPin = -1;
greenLedOff = true; // Set the flag indicating the green LED is off
}
}
}
prevDoorState = doorState;
// Check if any green LEDs need to be turned off after 3 seconds
if (millis() - previousGreenMillis >= GREENDURATION) {
// Turn off the current green LED
if (currentGreenLedPin != -1) {
digitalWrite(currentGreenLedPin, LOW);
currentGreenLedPin = -1;
greenLedOff = true; // Set the flag indicating the green LED is off
}
}
// Check if the green LED is confirmed to be off before proceeding
if (greenLedOff) {
// Proceed with checking other button presses or any other necessary tasks
// Reset the flag
greenLedOff = false;
}
delay(100); // Small delay for stability
}
// Function to check a button for a press and enqueue it if pressed
// Define debounce delay in milliseconds
#define DEBOUNCE_DELAY 500
// Function to check a button for a press and enqueue it if pressed
// Function to check a button for a press and enqueue it if pressed
void checkButton(int buttonPin, int buttonNumber, int redLedPin, int greenLedPin) {
int buttonState = digitalRead(buttonPin);
// Check if the button state is LOW, indicating a press
if (buttonState == LOW) {
if (!buttonQueue.isFull()) {
ButtonPress newPress;
newPress.buttonNumber = buttonNumber;
buttonQueue.enqueue(newPress); // Enqueue the button press
// Turn on the red LED
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, LOW); // Ensure green LED is off
} else {
Serial.println("Queue is full! Cannot add more.");
}
// Print the number of items in the queue
Serial.print("Number of items in the queue: ");
Serial.println(buttonQueue.itemCount());
}
// Check if the front of the queue matches this button press and activate respective green LED
if (!buttonQueue.isEmpty() && buttonQueue.front().buttonNumber == buttonNumber) {
// Turn off the currently lit green LED
if (currentGreenLedPin != -1) {
digitalWrite(currentGreenLedPin, LOW);
}
// Turn on the new green LED
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin, LOW); // Turn off the red LED
currentGreenLedPin = greenLedPin; // Update the current green LED pin
}
}