I'm a noob and wrote an alarm clock sketch with my girls as a learning exercise. I'm quite sure it's laughably bad by any measure and is very basic to account for my (in)ability. Sorry but it's all I can do for now. I am using now.unixtime() and simply triggering an alarm loop when the now.unixtime = a target unix time. I wasn't sure how else to work with comparing times.
The human time displays correctly and is set on compiling from the host computer. However the Unix time is incorrect, and I presume is a result of the difference between my local time and UTC. I wondered if there was a way to have the correct unix time without affecting the local time?
I also wondered how I should be handling the times to do away with Unix time and working directly with human times while keeping the sketch fundamentally unchanged. While I'm sure it's shockingly bad, at least I can understand it ![]()
I hope this is the correct way to share this code.
Thank you
#include <Wire.h> //loads I2C library
#include <LiquidCrystal_I2C.h> //loads LCD I2C library
#include <RTClib.h> //loads RTC library
RTC_DS1307 RTC;
LiquidCrystal_I2C lcd (0x3F,16,2); //I2C address of LCD, 16 characters, 2 lines
#define buz 13 //defining buzzer pin as pin 13
long alarmOn = 1536956950; //set alarm time in unixtime
void setup()
{
Serial.begin(9600); //begin serial communications
Wire.begin(); //begin I2C communications
RTC.begin(); //begin RTC
pinMode(buz, OUTPUT); //setup buz pin as an output
lcd.begin(16,2); //setup lcd as 16x2 display
lcd.backlight(); //turns on backlight
lcd.setCursor(0,0); //sets cursor to top left
lcd.print("Nina and Fia's"); //print 1st line message
lcd.setCursor(0,1); //set cursor to 2nd line
lcd.print(" Alarm Clock"); //set second line message
//RTC.adjust(DateTime(__DATE__, __TIME__)); //Uncomment this line to set the date and time
delay(5000); //startup message delay
lcd.clear(); //clears screen after startup message
}
void loop()
{
DateTime now = RTC.now(); //defines RTC?
lcd.setCursor(0,0); //below displays time and date
lcd.print("Time: ");
lcd.print(now.hour());
lcd.print(":");
lcd.print(now.minute());
lcd.setCursor(0,1);
lcd.print("Date: ");
lcd.print(now.day());
lcd.print("/");
lcd.print(now.month());
lcd.print("/");
lcd.print(now.year());
{
Serial.print(now.unixtime()); //print unix time to serial port
Serial.println(); //advances one line on serial monitor
delay(1000);
}
if(now.unixtime() == alarmOn) //Compare the current time with the Alarm time, both in unixtime.
{
for(int i=0; i<=10; i++)
{
lcd.clear(); //clears time and date
lcd.print(" Nina and Fia"); //displays message
lcd.setCursor(0,1);
lcd.print(" WAKE UP!!!");
digitalWrite(buz,HIGH); //turns buzzer on
delay(1000);
digitalWrite(buz, LOW);
delay(1000);
lcd.clear();
}
}
}