Files
push4baby/arduino_logic.ino
2026-02-18 20:53:27 +01:00

77 lines
2.1 KiB
C++

#include <Wire.h>
#include <RTClib.h>
#include <EEPROM.h>
RTC_DS3231 rtc;
const int buttonPins[] = {2, 3, 4, 5};
const int ledPins[] = {8, 9, 10, 11};
const int ledMap[] = {3, 1, 0, 2};
const int resistorPin = 6; // RESISTOR GOES HERE (Pin 6 to GND)
bool ledState[] = {false, false, false, false};
bool resetPerformed = false;
unsigned long lastKeepAlive = 0;
const int pulseDuration = 800; // 0.2s is usually enough to reset the timer
const int pulseInterval = 10000; // 20s interval (test if your power bank handles this)
void setup() {
rtc.begin();
// Ensure the resistor pin starts LOW (off)
pinMode(resistorPin, OUTPUT);
digitalWrite(resistorPin, LOW);
// Disable the onboard LED explicitly
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
for (int i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
pinMode(ledPins[i], OUTPUT);
ledState[i] = EEPROM.read(i);
digitalWrite(ledPins[i], ledState[i] ? HIGH : LOW);
}
}
void loop() {
DateTime now = rtc.now();
unsigned long currentMillis = millis();
// --- 1. SILENT KEEP-ALIVE (PIN 6 ONLY) ---
if (currentMillis - lastKeepAlive > pulseInterval) {
digitalWrite(resistorPin, HIGH);
if (currentMillis - lastKeepAlive > (pulseInterval + pulseDuration)) {
digitalWrite(resistorPin, LOW);
lastKeepAlive = currentMillis;
}
}
// --- 2. BUTTONS ---
for (int i = 0; i < 4; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
delay(50);
int targetLed = ledMap[i];
ledState[targetLed] = !ledState[targetLed];
digitalWrite(ledPins[targetLed], ledState[targetLed] ? HIGH : LOW);
EEPROM.update(targetLed, ledState[targetLed]);
while(digitalRead(buttonPins[i]) == LOW);
}
}
// --- 3. DAILY RESET (12:00 PM) ---
if (now.hour() == 12 && now.minute() == 0 && now.second() == 0) {
if (!resetPerformed) {
for (int k = 0; k < 4; k++) {
ledState[k] = false;
digitalWrite(ledPins[k], LOW);
EEPROM.update(k, false);
}
resetPerformed = true;
}
} else {
resetPerformed = false;
}
}