/*****************************************************************
Author: Tiger888
Date: 24/08/24
Last Update: Initial Version
The Project: Car Light Reminder System
Components:
Arduino Nano
External Car Battery
4 x AMS117 5V regulators
3 x Single Pole (NO) swithes. They required only for Wokwi Simulation.
5 x LEDs for system status indication
1 x Busser for alert sound
5 x Resistors 220 Ohms: Current Limiter for LEDs
Input Sensors or states: Engine, tail Light and Door swith
Alert Conditions: Engine OFF and Tail Light ON and Door ON (Open)
Alert Medium: Red LED On and Buzzer sound
*****************************************************************/
#include <Button.h>
const int buttonCount = 3; // Number of buttons
Button buttons[buttonCount] = {Button(2), Button(3), Button(4)}; // Initialize buttons
const int ledPins[buttonCount] = {7, 8, 9}; // Corresponding LEDs for each button
bool ledStates[buttonCount] = {false, false, false}; // Store LED states
unsigned long lastDebounceTime[buttonCount] = {0, 0, 0}; // Debounce timers for each button
unsigned long debounceDelay = 50; // Debounce delay time
const int redLedPin = 6; // Pin for the red LED
const int buzzerPin = 5; // Pin for buzzer
void setup() {
for (int i = 0; i < buttonCount; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Ensure LEDs are off initially
}
pinMode(redLedPin, OUTPUT);
digitalWrite(redLedPin, LOW); // Ensure red LED is off initially
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW); // Ensure buzzer is off initially
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < buttonCount; i++) {
toggleButton(buttons[i], ledPins[i], ledStates[i], lastDebounceTime[i]);
}
// 1st Logic for the red LED & Buzzer
if (!ledStates[0] && ledStates[1] && ledStates[2]) { // Engine OFF, Tail Light ON, Door Open
digitalWrite(redLedPin, HIGH);
activateBuzzer();
Serial.println("Red LED ON: Engine OFF, Tail Light ON, Door Open");
} else {
digitalWrite(redLedPin, LOW);
digitalWrite(buzzerPin, LOW);
Serial.println("Red LED OFF, Buzzer OFF");
}
/* 2nd Logic for the red LED & Buzzer : this is optional when driver
ignores the first warning alert
*/
// if (!ledStates[0] && ledStates[1]) { // Engine OFF, Tail Light ON, Ignore Door Open
// digitalWrite(redLedPin, HIGH);
// activateBuzzer();
// Serial.println("Red LED ON: Engine OFF, Tail Light ON, Ignore Door Open or Close");
// } else {
// digitalWrite(redLedPin, LOW);
// digitalWrite(buzzerPin, LOW);
// Serial.println("Red LED OFF, Buzzer OFF");
// }
}
void toggleButton(Button& button, int buttonLedPin, bool& ledState, unsigned long& lastDebounceTime) {
if (button.toggled()) {
unsigned long currentTime = millis();
if ((currentTime - lastDebounceTime) > debounceDelay) {
if (button.read() == Button::PRESSED) {
ledState = !ledState; // Toggle the LED state
digitalWrite(buttonLedPin, ledState ? HIGH : LOW); // Update LED based on new state
Serial.print("Button on pin ");
Serial.print(buttonLedPin);
Serial.println(ledState ? " - LED ON" : " - LED OFF");
lastDebounceTime = currentTime; // Update debounce time
}
}
}
}
void activateBuzzer(){
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(buzzerPin, LOW);
delay(500);
}