First, i am noob in arduino and just tinkering arduino.
i make a clock using :
- Arduino UNO
- RTC DS1307
- LCD 16*2 character
the purpose of my sketch:
- Read Serial data VB from VB to set RTC if Serial Header is 'T'
example : T1310258400 set RTC to July 10th 2011. 00:40:00 - Read Serial data VB from VB to write EEPROM if Serial Header is 'I'
example :
I080014000045
- then save :
-
- 08 to EEPROM address 200 ==> alarm start hour
-
- 00 to EEPROM address 201 ==> alarm start minute
-
- 14 to EEPROM address 202 ==> alarm end hour
-
- 00 to EEPROM address 203 ==> alarm end minute
-
- 00 to EEPROM address 204 ==> alarm interval hour
-
- 45 to EEPROM address 205 ==> alarm interval minute
- desired output:
- alarm ring every 45 minutes since 08.00 up to 14.00
- Comparing RTC time and alarm data to ring the alarm
problem:
if i compare RTC time and EEPROM value and start the alarm, alarm time is late.
example :
alarm is set like above
alarm ring at 08.45.26 (hh.mm.ss), my desired output : alarm ring at 08.45.00
alarm ring every 45 minutes and approx. 25 seconds
alarm ring at 14.14.30 (hh.mm.ss), my desired output : alarm didn't ring at that time
questions:
- any mistakes in my sketch?
- how to simplyfy this sketch?
#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 7, 13, 12, 11, 10);
//RTC variable
#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
//===========end============
//variable for InternalSlave1() function
#define PJG_DATA 13 //the length of the alarm data
#define INT_SLAVE 'I'//alarm interval mode header
#define OTHERS 'B'//nothing
//===========end===========
//variable for EEPROMread() function
int address = 200;
byte value;
//===========end===========
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
pinMode(13,OUTPUT);
setSyncProvider(RTC.get); // the function to get the time from the RTC
setSyncInterval(600);
if(timeStatus()!= timeSet)
lcd.println("Can't sync RTC! ");
else
lcd.println(" Can sync RTC! ");
delay(1000);
lcd.clear();
}
void loop()
{
compareAlarmInt();
Alarm.delay(0);
if(Serial.available())
{
char header = Serial.read();
if (header == TIME_HEADER) //ex : T1309734393
processSyncMessage();
else if (header == INT_SLAVE) //ex : I080012000045
IntervalSlave1();
}
digitalClockDisplay();
delay(1000);
}
/*====================read VB serial data to set RTC===================*/
void processSyncMessage()
{
time_t pctime = 0;
// return the time if a valid sync message is received on the serial port.
while (Serial.available() > TIME_MSG_LEN);
for(int i=0; i < TIME_MSG_LEN -1; i++)
{
char c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
time_t t = pctime;
RTC.set(t);
setTime(t);
//return;
}
//read ALARM (mode INTERVAL for SLAVE 1) from VB (Serial data) and save to EEPROM ARDUINO//
void IntervalSlave1()
{
}
/*====================COMPARING TIME data and ALARM data =======================*/
void compareAlarmInt(){
char y = hour();
char z = minute();
char c = EEPROM.read (200); //hourEEPROM ==>alarm start hour
char d = EEPROM.read (201); //minuteEEPROM ==>alarm start minute
char e = EEPROM.read (202); //hourEEPROM ==>alarm end hour
char f = EEPROM.read (203); //minuteEEPROM ==>alarm end minute
if ( y >= c && z >= d && y <= e && z <= f ){
//Serial.println("time data equal with alarm data");
char g = EEPROM.read (204); //hourEEPROM ==>alarm interval hour
char h = EEPROM.read (205); //minuteEEPROM ==>alarm interval minute
Alarm.timerRepeat(g, h, 0, intSlave1);
}
else
Serial.println("===========================");
}
/*====================INTERVAL SLAVE 1 COMMAND=================*/
void intSlave1(){
Serial.println("bip...bipp..bippp");
}
void digitalClockDisplay()
{
//display the clock
//same with mem's example
}
^^ thanks, and sorry for my english ^^
regards,
.cop.