/*
Wokwi | projects
Need help with a speaker with lcd screen
armin Wednesday, December 10, 2025 6:30 PM
i really dont know what i did wrong, im new to this, so any help would come in handy.
This is the link https://wokwi.com/projects/449989709560970241
*/
#include <Wire.h>
#include <LiquidCrystal.h>
#include <RTClib.h>
const int NUM_BTNS = 3;
const int BUZZER_PIN = 6;
const int BTN_PINS[] = {5, 4, 3};
int alarmHour = 0;
int alarmMinute = 0;
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS];
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
RTC_DS1307 rtc;
void setAlarmTime() {
char dispBuffer[16];
// Increment the alarm hour when UP_BTN_PIN is pressed
if (digitalRead(BTN_PINS[0]) == LOW) {
alarmHour++;
if (alarmHour > 23) {
alarmHour = 0;
}
delay(200);
}
// Decrement the alarm hour when DN_BTN_PIN is pressed
if (digitalRead(BTN_PINS[1]) == LOW) {
alarmHour--;
if (alarmHour < 0) {
alarmHour = 23;
}
delay(200);
}
// Display the alarm time on the LCD
snprintf(dispBuffer, 15, "Alarm: %2d:%02d", alarmHour, alarmMinute);
lcd.setCursor(0, 0);
lcd.print(dispBuffer);
}
void activateAlarm() {
// Sound the buzzer for 5 seconds
for (int i = 0; i < 5; i++) {
//digitalWrite(BUZZER_PIN, HIGH);
tone(BUZZER_PIN, 1000);
delay(500);
//digitalWrite(BUZZER_PIN, LOW);
noTone(BUZZER_PIN);
delay(500);
}
}
// function returns which button was pressed, or 0 if none
int checkButtons() {
int btnPressed = 0;
for (int i = 0; i < NUM_BTNS; i++) {
btnState[i] = digitalRead(BTN_PINS[i]); // check each button
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
if (btnState[i] == 0) { // was just pressed
btnPressed = i + 1;
}
delay(20); // debounce
}
}
return btnPressed;
}
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
rtc.begin();
// Set the time of RTC if it's not set
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
for (int i = 0; i < NUM_BTNS; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
}
//pinMode(SET_BTN_PIN, INPUT_PULLUP);
//pinMode(UP_BTN_PIN, INPUT_PULLUP);
//pinMode(DN_BTN_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
// Attach an interrupt to the SET_BTN_PIN pin
//attachInterrupt(digitalPinToInterrupt(SET_BTN_PIN), setAlarmTime, FALLING);
Serial.println("Ready");
lcd.print("Alarm System");
}
void loop() {
char dispBuff[16];
int btnNum = checkButtons();
if (btnNum) {
Serial.print("Button ");
Serial.print(btnNum);
Serial.println(" pressed");
if (btnNum == 3) {
setAlarmTime();
}
}
// Read the current time from RTC
DateTime now = rtc.now();
// Display the current time on the LCD
lcd.setCursor(4, 1);
snprintf(dispBuff, 16, "%2d:%02d:%02d", now.hour(), now.minute(), now.second());
lcd.print(dispBuff);
// Check if the alarm time is reached
if (now.hour() == alarmHour && now.minute() == alarmMinute) {
activateAlarm();
}
}
Up
Down
Set