我做的是一个串口输入定时时间然后,时间到之后控制13灯亮灭。
#include <DS1302.h>
String month; //定义一个月份字符串变量
DS1302 rtc(5,6,7);
char buf[50];
char day[10];//定义ds1307接线柱
Time otime;
String comdata = "";
int numdata[4] ={0}, j = 0, mark = 0;
int h1=0;
int m1=0;
int h2=0;
int m2=0;
int h1bt=0;
int m1bt=0;
int h2bt=0;
int m2bt=0;
int sdbt=0;
int sd=0;
void print_time()
{
/* Get the current time and date from the chip */
Time t = rtc.time();
/* Name the day of the week */
memset(day, 0, sizeof(day)); /* clear day buffer */
switch (t.day) {
case 1:
strcpy(day, "Sunday");
break;
case 2:
strcpy(day, "Monday");
break;
case 3:
strcpy(day, "Tuesday");
break;
case 4:
strcpy(day, "Wednesday");
break;
case 5:
strcpy(day, "Thursday");
break;
case 6:
strcpy(day, "Friday");
break;
case 7:
strcpy(day, "Saturday");
break;
}
/* Format the time and date and insert into the temporary buffer */
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
day,
t.yr, t.mon, t.date,
t.hr, t.min, t.sec);
/* Print the formatted string to serial so we can see the time */
Serial.println(buf);
}
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
rtc.write_protect(false);
rtc.halt(false);
/* Make a new time object to set the date and time */
/* Tuesday, May 19, 2009 at 21:16:37. */
Time t(2014, 4, 17, 19, 06, 37, 3);
/* Set the time and date on the chip */
rtc.time(t);
}
void loop()
{while (Serial.available() > 0)
{
comdata += char(Serial.read());
delay(2);
mark = 1;
}
/* 以逗号分隔分解comdata的字符串,分解结果变成转换成数字到numdata[]数组 */
if(mark == 1)
{
Serial.print("You inputed : ");
Serial.println(comdata);
for(int i = 0; i < comdata.length() ; i++)
{
if(comdata[i] == ',' || comdata[i] == 0x10 || comdata[i] == 0x13)
{
j++;
}
else
{
numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
}
}
h1=numdata[0];
h2=numdata[1];
m1=numdata[2];
m2=numdata[3];
mark = 0;j=0;
comdata = String("");
/* 清空 numdata */
for(int i = 0; i < 4 ; i++) numdata[i]=0;
}
print_time();
delay(1000);
if(otime.hr==h1&&otime.min==m1)
digitalWrite(13,HIGH);
if(otime.hr==h2&&otime.min==m2)
digitalWrite(13,LOW);
} |