/*
Arduino | coding-help
Code not working.
Michael— January 16, 2025 at 9:24 AM
I wrote the code for a alarm clock project im working on,
but the code doesn't seem to be working when I try simulating.
The simulator is wokwi.
https://wokwi.com/projects/420247060897019905
Code: https://pastebin.mozilla.org/BiCtgsO0
*/
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
// constants
const unsigned long ONE_SEC = 1000;
const int NUM_BTNS = 6; // how many buttons
const int LED_PIN = 6;
const int BUZZER_PIN = 5;
const int BTN_PINS[NUM_BTNS] = {12, 11, 10, 9, 8, 7};
const char daysOfTheWeek[][4] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
// global variables
bool isAlarm = false;
bool isSnooze = false;
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS]; // or set HIGH
int alarmHour = 0;
int alarmMin = 0;
unsigned long prevTime = 0;
DateTime now;
// create objects
LiquidCrystal_I2C lcd0(0x26, 16, 2);
LiquidCrystal_I2C lcd1(0x27, 16, 2);
RTC_DS1307 rtc;
// function returns which button was pressed, or 0 if none
int checkButtons() {
int btnPressed = 0;
for (int i = 0; i < NUM_BTNS; i++) {
// check each button
btnState[i] = digitalRead(BTN_PINS[i]);
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
if (btnState[i] == LOW) { // was just pressed
btnPressed = i + 1;
//Serial.print("Button ");
//Serial.print(i + 1);
//Serial.println(" pressed");
}
delay(20); // debounce
}
}
return btnPressed;
}
void updateTimeDisplay() {
char printBuffer[40];
char lcdBuffer[16];
// serial display
snprintf(printBuffer, 40, "Date&Time:\t%s, %4d/%d/%d\t%d:%02d:%02d",
daysOfTheWeek[now.dayOfTheWeek()],
now.year(), now.month(), now.day(),
now.hour(), now.minute(), now.second());
Serial.println(printBuffer);
// lcd0 display
snprintf(lcdBuffer, 16, "%s, %d/%d/%d",
daysOfTheWeek[now.dayOfTheWeek()],
now.year(), now.month(), now.day());
lcd0.setCursor(0, 0);
lcd0.print(lcdBuffer);
snprintf(lcdBuffer, 16, "%d:%02d:%02d",
now.hour(), now.minute(), now.second());
lcd0.setCursor(5, 1);
lcd0.print(lcdBuffer);
}
void setup() {
Serial.begin(9600);
// setup the 12C LCDs
lcd0.init();
lcd1.init();
lcd0.backlight();
lcd1.backlight();
// setup RTC
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1);
}
// pin setup
for (int i = 0; i < NUM_BTNS; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
}
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
// automatically sets the RTC to the date & time on PC this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop () {
char lcdBuffer[16];
// update serial & lcd0 once a second
if (millis() - prevTime >= ONE_SEC) {
prevTime = millis();
now = rtc.now();
updateTimeDisplay();
}
// check the buttons & update lcd1 all the time
int btnNumber = checkButtons();
if (btnNumber > 0) {
if (btnNumber == 1) {
Serial.println("SNOOZE");
} else if (btnNumber == 2) {
alarmHour++;
if (alarmHour > 23) alarmHour = 23;
} else if (btnNumber == 3) {
alarmHour--;
if (alarmHour < 0) alarmHour = 0;
} else if (btnNumber == 4) {
alarmMin++;
if (alarmMin > 59) alarmMin = 59;
} else if (btnNumber == 5) {
alarmMin--;
if (alarmMin < 0) alarmMin = 0;
} else if (btnNumber == 6) {
isAlarm = !isAlarm;
}
} else {
//Serial.println("No button pressed");
}
// lcd1 display
snprintf(lcdBuffer, 16, "Alarm: %02d:%02d", alarmHour, alarmMin);
lcd1.setCursor(2, 0);
lcd1.print(lcdBuffer);
snprintf(lcdBuffer, 16, "Alarm %s", isAlarm ? "is set " : "not set");
lcd1.setCursor(2, 1);
lcd1.print(lcdBuffer);
// alarm LED
digitalWrite(LED_PIN, isAlarm ? HIGH : LOW);
}
Snooze
Hour +/-
Min +/-
Alarm