絾謌 发表于 2014-4-17 19:20:00

哪位大神帮我修改一下程序好吗?

我做的是一个串口输入定时时间然后,时间到之后控制13灯亮灭。
#include <DS1302.h>
String month;//定义一个月份字符串变量
DS1302rtc(5,6,7);
char buf;
char day;//定义ds1307接线柱
Time otime;
String comdata = "";
int numdata ={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 == ',' || comdata == 0x10 || comdata == 0x13)
            {
                j++;
            }
         else
            {
                numdata = numdata * 10 + (comdata - '0');
            }
      }
h1=numdata;
h2=numdata;
m1=numdata;
m2=numdata;
      mark = 0;j=0;
         comdata = String("");
      /* 清空 numdata */
      for(int i = 0; i < 4 ; i++) numdata=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);
}

Super169 发表于 2014-4-17 20:48:52

沒用過 1302 的庫, 但從程式上看, 有點矛盾.

/* Get the current time and date from the chip */
Time t = rtc.time();

時間是需要從 rtc.time() 中讀取的.

但.... otime 只定義了, 不用讀取就可以用來比較?


Time otime;
:
:
if(otime.hr==h1&&otime.min==m1)
digitalWrite(13,HIGH);
if(otime.hr==h2&&otime.min==m2)
digitalWrite(13,LOW);

页: [1]
查看完整版本: 哪位大神帮我修改一下程序好吗?