/////////////////////////////////////////
// Anduino //////////////////////////////
// Code by fael @ https://fael.my.id /
// Next: Backend integration & dashboard/
/////////////////////////////////////////
/////////////////////////////////////////
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
// #include <WiFi.h>
// #include <HTTPClient.h>
// Pin definitions
const int BTN_PINS[] = {26, 25, 33, 32}; // Green, Yellow, Red, Blue
const int RELAY_PINS[] = {13, 12, 14, 27}; // Green, Yellow, Red, Blue
const int NUM_COLORS = 4;
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// RTC setup
RTC_DS1307 rtc;
// Variables
const char* COLOR_NAMES[] = {"GREEN", "YELLOW", " RED ", " BLUE"};
int currentColor = 0; // Start with green (index 0)
unsigned long stopwatchStart = 0;
unsigned long lastUpdate = 0;
unsigned long lastSyncTime = 0;
// Variables to store previous values for comparison
char prevStopwatchStr[9] = "";
char prevBottomLine[17] = "";
// Unique device ID (you should set this for each device)
const char* DEVICE_ID = "ANDON_001";
// WiFi credentials
// const char* ssid = "YOUR_WIFI_SSID";
// const char* password = "YOUR_WIFI_PASSWORD";
// Apps Script Web App URL
// const char* appsScriptUrl = "YOUR_APPS_SCRIPT_WEB_APP_URL";
void sendStatusUpdate(const char* deviceId, const char* color, unsigned long elapsedTime) {
// This function will send data to Apps Script
/*
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(appsScriptUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String postData = "device_id=" + String(deviceId) +
"&color=" + String(color) +
"&elapsed_time=" + String(elapsedTime);
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("HTTP Response code: " + String(httpResponseCode));
Serial.println("Response: " + response);
} else {
Serial.println("Error on sending POST: " + String(httpResponseCode));
}
http.end();
} else {
Serial.println("WiFi not connected");
}
*/
// For now, we'll just print to Serial for debugging
Serial.print("Device ID: ");
Serial.print(deviceId);
Serial.print(", Color: ");
Serial.print(color);
Serial.print(", Elapsed Time: ");
Serial.println(elapsedTime);
}
void setup() {
Serial.begin(115200); // Initialize Serial for debugging
// WiFi setup
/*
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
*/
for (int 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();
if (!rtc.begin()) {
lcd.print("RTC failed");
while (1);
}
// Set initial color to green
setColor(0);
updateDisplay();
}
void loop() {
for (int i = 0; i < NUM_COLORS; i++) {
if (digitalRead(BTN_PINS[i]) == LOW) {
delay(50); // Debounce
if (digitalRead(BTN_PINS[i]) == LOW) {
setColor(i);
// Send status update when color changes
sendStatusUpdate(DEVICE_ID, COLOR_NAMES[i], 0);
while (digitalRead(BTN_PINS[i]) == LOW); // Wait for release
}
}
}
if (millis() - lastUpdate >= 1000) {
updateDisplay();
lastUpdate = millis();
}
// Sync stopwatch with Apps Script every minute
if (millis() - lastSyncTime >= 60000) {
unsigned long elapsedTime = (millis() - stopwatchStart) / 1000;
sendStatusUpdate(DEVICE_ID, COLOR_NAMES[currentColor], elapsedTime);
lastSyncTime = millis();
}
}
void setColor(int color) {
digitalWrite(RELAY_PINS[currentColor], LOW);
currentColor = color;
digitalWrite(RELAY_PINS[currentColor], HIGH);
stopwatchStart = millis();
lastSyncTime = millis(); // Reset sync timer when color changes
// Update display when color changes
updateDisplay();
}
void updateDisplay() {
// Stopwatch
unsigned long elapsed = (millis() - stopwatchStart) / 1000;
int hours = elapsed / 3600;
int minutes = (elapsed % 3600) / 60;
int seconds = elapsed % 60;
char stopwatchStr[9];
sprintf(stopwatchStr, "%02d:%02d:%02d", hours, minutes, seconds);
// Color and current time
DateTime now = rtc.now();
// Format bottom line with color on the left, --- in the middle, and time on the right
char bottomLine[17];
snprintf(bottomLine, sizeof(bottomLine), "%-6s --- %02d:%02d", COLOR_NAMES[currentColor], now.hour(), now.minute());
// Update the LCD only if the content has changed
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);
}
}