here is the code..
#include <Wire.h>
#include <DS1307.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(53, 51, 49, 47, 45, 43);
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0',','}
};
byte rowPins[ROWS] = {40, 42, 44, 46};
byte colPins[COLS] = {48, 50, 52};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
/*
about set time:
format: year,month,day,week,hour,min,sec
example: 14,03,25,02,13,55,10 2014.03.25 tuesday 13:55:10
*/
int8_t button; // Holds the current button pressed
uint8_t alarmHours = 0, alarmMinutes = 0; // Holds the current alarm time
uint8_t tmpHours;
boolean alarm = false; // Holds the current state of the alarm
unsigned long timeRef;
// DateTime now; // Holds the current date and time information
String comdata = "";
int mark=0;
//store the current time data
int rtc[7];
//store the set time data
byte rr[7];
//light pin
int ledPin = 13;
//initial light
char data;
void setup()
{
DDRC |= _BV(2) | _BV(3); // POWER:Vcc Gnd
PORTC |= _BV(3); // VCC PINC3
pinMode(ledPin, OUTPUT);
//initial baudrate
Serial.begin(9600);
//get current time
RTC.get(rtc, true);
//if time is wrong reset to default time
if (rtc[6] < 12) {
//stop rtc time
RTC.stop();
RTC.set(DS1307_SEC, 1);
RTC.set(DS1307_MIN, 27);
RTC.set(DS1307_HR, 01);
RTC.set(DS1307_DOW, 7);
RTC.set(DS1307_DATE, 12);
RTC.set(DS1307_MTH, 2);
RTC.set(DS1307_YR, 12);
//start rtc time
RTC.start();
}
RTC.SetOutput(DS1307_SQW32KHZ);
}
void loop()
{
showTime();
timeRef = millis();
char key = keypad.getKey();
if (key) { Serial.println(key);
}
switch (keypad.getState())
case PRESSED:
{
if (key == '1') {
makeAlarm();
}
}
}
int showTime()
{
lcd.begin(16,2);
lcd.print("BioAMS");
{
int i;
//get current time
RTC.get(rtc, true);
//print current time format : year month day week hour min sec
for (i = 0; i < 7; i++)
{
Serial.print(rtc[i]);
Serial.print(" ");
//Time
lcd.setCursor(7, 0);
lcd.print(rtc[0]);
lcd.setCursor(9, 0);
lcd.print("/");
lcd.setCursor(10, 0);
lcd.print(rtc[1]);
lcd.setCursor(12, 0);
lcd.print("/");
lcd.setCursor(14, 0);
lcd.print(rtc[2]);
//Week
lcd.setCursor(0, 1);
lcd.print(rtc[3]);
//Date
lcd.setCursor(2, 1);
lcd.print(",");
lcd.setCursor(3, 1);
lcd.print(rtc[4]);
lcd.setCursor(5, 1);
lcd.print("/");
lcd.setCursor(6, 1);
lcd.print(rtc[5]);
lcd.setCursor(8, 1);
lcd.print("/");
lcd.setCursor(10, 1);
lcd.print(rtc[6]);
}
//blink the light
Serial.println();
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
//
int j = 0;
//read all the data
while (Serial.available() > 0)
{
comdata += char(Serial.read());
delay(2);
mark = 1;
}
//if data is all collected,then parse it
if (mark == 1)
{
Serial.println(comdata);
//lcd.setCursor(0,1);
//lcd.print((rtc[i]));
Serial.println(comdata.length());
//parse data
for (int i = 0; i < comdata.length() ; i++)
{
//if the byte is ',' jump it,and parse next value
if (comdata[i] == ',')
{
j++;
}
else
{
rr[j] = rr[j] * 10 + (comdata[i] - '0');
}
}
comdata = String("");
RTC.stop();
RTC.set(DS1307_SEC, rr[6]);
RTC.set(DS1307_MIN, rr[5]);
RTC.set(DS1307_HR, rr[4]);
RTC.set(DS1307_DOW, rr[3]);
RTC.set(DS1307_DATE, rr[2]);
RTC.set(DS1307_MTH, rr[1]);
RTC.set(DS1307_YR, rr[0]);
RTC.start();
mark = 0;
}
}
}
int makeAlarm()
{
unsigned long timeRef;
boolean timeOut = true;
uint8_t tmpMinutes = 0;
lcd.clear();
lcd.print("Alarm Time");
timeRef = millis();
lcd.setCursor(0,1);
lcd.print("Set minutes: 0");
while ( (unsigned long)(millis() - timeRef) < 5000 )
{
// THE PROCESS STOPS HERE...
// int8_t button = Serial.read();
char key = keypad.getKey();
switch (keypad.getState())
if (key== '*')
{
tmpMinutes = tmpMinutes < 55 ? tmpMinutes + 1 : tmpMinutes;
lcd.setCursor(13,1);
lcd.print(" ");
lcd.setCursor(13,1);
if ( tmpMinutes < 10 ) lcd.print(" ");
lcd.print(tmpMinutes);
timeRef = millis();
}
else if ( key == '0' )
{
tmpMinutes = tmpMinutes > 0 ? tmpMinutes - 1 : tmpMinutes;
lcd.setCursor(13,1);
lcd.print(" ");
lcd.setCursor(13,1);
if ( tmpMinutes < 10 ) lcd.print(" ");
lcd.print(tmpMinutes);
timeRef = millis();
}
else if ( key == '#' )
{
while ( button != NO_KEY ) ;
timeOut = false;
break;
}
delay(150);
if ( !timeOut )
{
alarmHours = tmpHours;
alarmMinutes = tmpMinutes;
// transition(Serial.read());
// Turn off the display:
lcd.noDisplay();
delay(3000);
// Turn on the display:
lcd.display();
delay(3000);
}
// else transition(TIME_OUT);
}
lcd.clear();
}
[code/]