Hello all,
I'm trying to create an automated device that runs based on time of day ... figured a good place to start would be in making a basic two-line LCD clock.
One really important thing I want to do is have the ability to set the clock after the Arduino is up and running. Eventually I want to set time through a menu, but for now I just have an interrupt button that goes to a "set time" screen with hard-coded values (eventually it'll pull rtc.nowHour/Minute values but for now it's rando values).
Where I run into a real headache is at the end of my interrupt. You'll see at the end where I'm trying to write time back to the RTC and that's where my program just stops responding. If I comment out rtc.adjust then I can get out of the interrupt and back to the main loop. I've tried a half-dozen or so libraries and similarly get stuck in the same place. I figure it has something to do with stopping and restarting the RTC, but am at a loss on how to actually do that.
Any feedback would be most appreciated.
I've been trying to solve this problem for more than week with a lot of Googling and searching here (if anyone here can link to the solution I'll be eternally grateful and eternally shamed) ... I find it hard to believe no one else has done what I'm looking to here.
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
const byte interruptMenu = 2;
const byte okay = 3;
const byte draw_hour = 12;
const byte draw_minute = 0;
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0 //<50
#define btnUP 1 //<195
#define btnDOWN 2 //<380
#define btnLEFT 3 //<555
#define btnSELECT 4 //<650
#define btnNONE 5
void setup() {
lcd.begin(16,2);
rtc.begin();
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
rtc.adjust(DateTime(1985, 2, 2, 14, 20, 0));
pinMode(interruptMenu, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptMenu), MenuInterrupt, LOW);
pinMode(okay, INPUT_PULLUP);
}
void loop() {
DateTime now = rtc.now();
lcd.setCursor(2,0);
lcd.print(":");
if (now.hour()>11) {
lcd.setCursor(5,0);
lcd.print("pm");
}
else {
lcd.setCursor(5,0);
lcd.print("am");
}
if (now.hour()<13) {
if (now.hour()<10) {
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(1,0);
lcd.print(now.hour());
}
else {
lcd.setCursor(0,0);
lcd.print(now.hour());
}
}
else {
if (now.hour()<22) {
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(1,0);
lcd.print(now.hour()-12);
}
else {
lcd.setCursor(0,0);
lcd.print(now.hour()-12);
}
}
if (now.minute()<10) {
lcd.setCursor(3,0);
lcd.print("0");
lcd.setCursor(4,0);
lcd.print(now.minute());
}
else {
lcd.setCursor(3,0);
lcd.print(now.minute());
}
}
void MenuInterrupt() {
while(digitalRead(okay) == HIGH) {
lcd.setCursor(0,0);
lcd.print("Set Time");
lcd.setCursor(0,1);
lcd.setCursor(2,1);
lcd.print(":");
if (draw_hour>11) {
lcd.setCursor(5,1);
lcd.print("pm");
}
else {
lcd.setCursor(5,1);
lcd.print("am");
}
if (draw_hour<13) {
if (draw_hour<10) {
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(1,1);
lcd.print(draw_hour);
}
else {
lcd.setCursor(0,1);
lcd.print(draw_hour);
}
}
else {
if (draw_hour<22) {
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(1,1);
lcd.print(draw_hour-12);
}
else {
lcd.setCursor(0,1);
lcd.print(draw_hour-12);
}
}
if (draw_minute<10) {
lcd.setCursor(3,1);
lcd.print("0");
lcd.setCursor(4,1);
lcd.print(draw_minute);
}
else {
lcd.setCursor(3,1);
lcd.print(draw_minute);
}
}
rtc.adjust(DateTime(1985, 2, 2, draw_hour, draw_minute, 0));
lcd.clear();
}