/*
* Anduino - Lite Version
*
* Features:
* - Four buttons to select different colors
* - Four relays to control color-specific devices or LEDs
* - LCD display showing a stopwatch and the current active color
* - Debounce mechanism for button presses
*
* Hardware Setup:
* - Buttons: Connected to pins 33 (Red), 27 (Yellow), 26 (Green), 32 (Blue)
* - Relays: Connected to pins 19 (Red), 18 (Yellow), 4 (Green), 5 (Blue)
* - LCD: I2C connection, address 0x27
*
* Operation:
* - The system starts with the Green color active
* - Pressing a button changes the active color and resets the stopwatch
* - The LCD updates every second, showing the elapsed time and current color
*
* Note: This is a simplified version of a larger system, designed for offline use and testing.
* It does not include WiFi, Firebase, or other online features of the full version.
*
* Created for Wokwi simulation environment
* Last Updated: Wed, 25 Sep 2024
*/
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin definitions for buttons and relays (Red, Yellow, Green, Blue)
const int BTN_PINS[] = {33, 27, 26, 32};
const int RELAY_PINS[] = {19, 18, 4, 5};
const int NUM_COLORS = 4;
// LCD display setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Constants
const unsigned long DISPLAY_UPDATE_INTERVAL = 1000; // Update every 1 second
const char *COLOR_NAMES[] = {"RED", "YELLOW", "GREEN", "BLUE"};
// Variables
uint8_t currentColor = 0;
unsigned long stopwatchStart = 0;
unsigned long lastUpdate = 0;
char prevStopwatchStr[9] = "";
char prevBottomLine[17] = "";
void setColor(uint8_t color);
void updateDisplay();
void setupHardware();
void setup() {
Serial.begin(115200);
setupHardware();
currentColor = 2; // Start with green
stopwatchStart = millis() / 1000; // Use millis() instead of NTP
// Turn on green LED initially
digitalWrite(RELAY_PINS[currentColor], HIGH);
updateDisplay();
}
void loop() {
unsigned long currentMillis = millis();
// Check button presses (all flipped default HIGH)
for (uint8_t i = 0; i < NUM_COLORS; i++) {
if (digitalRead(BTN_PINS[i]) == LOW) {
delay(50); // Debounce
if (digitalRead(BTN_PINS[i]) == LOW) {
setColor(i);
while (digitalRead(BTN_PINS[i]) == LOW);
}
}
}
// Update display
if (currentMillis - lastUpdate >= DISPLAY_UPDATE_INTERVAL) {
updateDisplay();
lastUpdate = currentMillis;
Serial.println("Display updated");
}
}
void setColor(uint8_t color) {
if (color != currentColor) {
stopwatchStart = millis() / 1000;
// Turn off all relays
for (uint8_t i = 0; i < NUM_COLORS; i++) {
digitalWrite(RELAY_PINS[i], LOW);
}
// Turn on selected color
currentColor = color;
digitalWrite(RELAY_PINS[currentColor], HIGH);
}
}
void updateDisplay() {
unsigned long currentTime = millis() / 1000;
unsigned long elapsed = currentTime - stopwatchStart;
uint8_t hours = elapsed / 3600;
uint8_t minutes = (elapsed % 3600) / 60;
uint8_t seconds = elapsed % 60;
char stopwatchStr[9];
snprintf(stopwatchStr, sizeof(stopwatchStr), "%02d:%02d:%02d", hours, minutes, seconds);
char bottomLine[17];
snprintf(bottomLine, sizeof(bottomLine), "%-6s --- LITE", COLOR_NAMES[currentColor]);
if (strcmp(stopwatchStr, prevStopwatchStr) != 0) {
lcd.setCursor((16 - strlen(stopwatchStr)) / 2, 0);
lcd.print(stopwatchStr);
strcpy(prevStopwatchStr, stopwatchStr);
}
if (strcmp(bottomLine, prevBottomLine) != 0) {
lcd.setCursor(0, 1);
lcd.print(bottomLine);
strcpy(prevBottomLine, bottomLine);
}
}
void setupHardware() {
for (uint8_t i = 0; i < NUM_COLORS; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
pinMode(RELAY_PINS[i], OUTPUT);
digitalWrite(RELAY_PINS[i], LOW);
}
Wire.begin();
lcd.init();
lcd.backlight();
}