Hi Guys,
I am presently automating my central heating system. Part of this plan requires a time clock based on the Arduino Mega 2560 with a DS1307 shield together with thermostats to control the furnace. I am using the Arduino IDE 1.0.5.
The attached program is based on the Time and TimeAlarms examples and requires an input from the Serial Monitor to set the RTC and display on the LCD. This part of the trial program works fine but the section associated with the alarm fails to function.
Please help.
To test the program
Set Time to 13 Jan2014 7.58am T1389599884
ALARMS
Alarm.alarmRepeat(8,00,0, KMorning1AlarmOn); // 8:00am every day
Alarm.alarmRepeat(8,02,0, KMorning1AlarmOff); // 8:00am every day
digitalClockDisplay();
Alarm.delay(200);
FUNCTIONS
void KMorning1AlarmOn()
{
digitalWrite(led, HIGH); // turn LED ON
lcd.print("morning alarm on"); // Print a message to the LCD.
// mySerial.println("Alarm: - turn lights on");
}
void KMorning1AlarmOff()
{
digitalWrite(led, LOW); // turn LED OFF
lcd.print("morning alarm off"); // Print a message to the LCD.
// mySerial.println("Alarm: - turn lights off");
}
/*15/1/14
* TimeRTCSet sketch
* example code illustrating Time library with real-time clock.
* LED switches On/Off
* RTC is set in response to serial port time message
* A Processing example sketch to set the time is included in the download
* Set Time to 13 Jan2014 7.58am T1389599884
*/
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include <TimeAlarms.h>
//#include <SoftwareSerial.h>
#include <LiquidCrystal.h> // include the library code
int led = 13; //LED pin
//SoftwareSerial mySerial(10, 3); // RX, TX
//constants for the number of rows and columns in the LCD
const int numRows = 2;
const int numCols = 16;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(30, 31, 32, 33, 34, 35);
void setup()
{
pinMode(30, OUTPUT);
pinMode(31, OUTPUT);
pinMode(32, OUTPUT);
pinMode(33, OUTPUT);
pinMode(34, OUTPUT);
pinMode(35, OUTPUT);
lcd.begin(numCols, numRows);
pinMode(led, OUTPUT);
digitalWrite(led, LOW); //Turn off LED
lcd.print("RTC CLOCK"); // Print a message to the LCD.
// set the data rate for the SoftwareSerial port
delay(1000);
// mySerial.begin(9600);
//mySerial.println("Hello world, RTC");
Alarm.alarmRepeat(8,00,0, KMorning1AlarmOn); // 8:00am every day
Alarm.alarmRepeat(8,02,0, KMorning1AlarmOff); // 8:00am every day
digitalClockDisplay();
Alarm.delay(200);
}
void digitalClockDisplay(){
// digital clock display of the time on LCD
lcd.setCursor(0, 1);
lcd.print(hour());
printDigits(minute());
printDigits(second());
lcd.print(" ");
lcd.print(day());
lcd.print(" ");
lcd.print(month());
lcd.print(" ");
lcd.print(year());
lcd.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints
// preceding colon and leading 0
lcd.print(":");
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}
/* code to process time sync messages from the serial port */
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed
// by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
time_t processSyncMessage() {
// return the time if a valid sync message is received on the serial port.
// time message consists of a header and ten ascii digits
while(Serial.available() >= TIME_MSG_LEN ){
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
return pctime;
}
}
return 0;
}
// pinMode(led, OUTPUT);
//digitalWrite(led,LOW);
// delay(1000);
void KMorning1AlarmOn()
{
digitalWrite(led, HIGH); // turn LED ON
lcd.print("morning alarm on"); // Print a message to the LCD.
// mySerial.println("Alarm: - turn lights on");
}
void KMorning1AlarmOff()
{
digitalWrite(led, LOW); // turn LED OFF
lcd.print("morning alarm off"); // Print a message to the LCD.
// mySerial.println("Alarm: - turn lights off");
}
void loop()
{
if(Serial.available())
{
time_t t = processSyncMessage();
if(t >0)
{
RTC.set(t); // set the RTC and the system time to the received value
setTime(t);
}
}
digitalClockDisplay();
Alarm.delay(200);
}