I'm trying to make a relatively basic Arduino clock that also measures Temp/Humidity, but I think there is something amiss with my pushbutton, which should allow me to set the time and a couple of alarms. When a pull-up resistor is used, they are nonresponsive, but when they are not they will simply power cycle the LCD in use. My code compiles fine, and several other individuals I've reached out to say my code should work as advertised which leads me to believe I'm overlooking something. Any help is appreciated.
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
#include <DHT.h>
#include <Bounce2.h> // Added Bounce2 library for button debouncing
#define DHTPIN 22 // Pin connected to the DHT11 sensor
#define DHTTYPE DHT11 // DHT type
DHT dht(DHTPIN, DHTTYPE);
RTC_DS3231 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Change the pin numbers accordingly for your setup
const int passiveBuzzerPin = 13;
const int activeBuzzerPin = 6;
const int buttonSetHour = 10;
const int buttonSetMinute = 9;
const int buttonSetAlarm1 = 8;
const int buttonSetAlarm2 = 7;
bool alarm1Set = false;
bool alarm2Set = false;
// Define Bounce objects for the buttons
Bounce buttonHour = Bounce(); // Bounce object for buttonSetHour
Bounce buttonMinute = Bounce(); // Bounce object for buttonSetMinute
Bounce buttonAlarm1 = Bounce(); // Bounce object for buttonSetAlarm1
Bounce buttonAlarm2 = Bounce(); // Bounce object for buttonSetAlarm2
void setup() {
Serial.begin(9600);
Wire.begin();
rtc.begin();
dht.begin();
lcd.begin(16, 2);
lcd.print(" Alarm Clock ");
delay(2000);
lcd.clear();
pinMode(passiveBuzzerPin, OUTPUT);
pinMode(activeBuzzerPin, OUTPUT);
// Set button pins as INPUT_PULLUP
pinMode(buttonSetHour, INPUT_PULLUP);
pinMode(buttonSetMinute, INPUT_PULLUP);
pinMode(buttonSetAlarm1, INPUT_PULLUP);
pinMode(buttonSetAlarm2, INPUT_PULLUP);
// Set debounce interval to 50ms (adjust as needed)
buttonHour.attach(buttonSetHour, INPUT_PULLUP);
buttonHour.interval(50);
buttonMinute.attach(buttonSetMinute, INPUT_PULLUP);
buttonMinute.interval(50);
buttonAlarm1.attach(buttonSetAlarm1, INPUT_PULLUP);
buttonAlarm1.interval(50);
buttonAlarm2.attach(buttonSetAlarm2, INPUT_PULLUP);
buttonAlarm2.interval(50);
}
void loop() {
DateTime now = rtc.now();
int hour = now.hour();
int minute = now.minute();
int second = now.second();
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
displayTime(hour, minute, second);
displayTemperatureAndHumidity(temperature, humidity);
checkAlarms(hour, minute);
}
void displayTime(int hour, int minute, int second) {
lcd.setCursor(0, 0);
lcd.print("Time: ");
if (hour < 10) {
lcd.print("0");
}
lcd.print(hour);
lcd.print(":");
if (minute < 10) {
lcd.print("0");
}
lcd.print(minute);
lcd.print(":");
if (second < 10) {
lcd.print("0");
}
lcd.print(second);
}
void displayTemperatureAndHumidity(float temperature, float humidity) {
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature, 1);
lcd.print(" C");
lcd.setCursor(9, 1);
lcd.print("Humidity: ");
lcd.print(humidity, 1);
lcd.print(" %");
}
const int ALARM1_HOUR = 7;
const int ALARM1_MINUTE = 30;
const int ALARM2_HOUR = 10;
const int ALARM2_MINUTE = 0;
void checkAlarms(int hour, int minute) {
// Update the button states
buttonHour.update();
buttonMinute.update();
buttonAlarm1.update();
buttonAlarm2.update();
// Toggle the alarm setting when buttons are pressed
if (buttonAlarm1.fell()) {
alarm1Set = !alarm1Set;
}
if (buttonAlarm2.fell()) {
alarm2Set = !alarm2Set;
}
if (alarm1Set && hour == ALARM1_HOUR && minute == ALARM1_MINUTE) {
tone(activeBuzzerPin, 2000, 1000);
}
if (alarm2Set && hour == ALARM2_HOUR && minute == ALARM2_MINUTE) {
tone(activeBuzzerPin, 2000, 1000);
}
}





