极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8894|回复: 1

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

[复制链接]
发表于 2014-4-17 19:20:00 | 显示全部楼层 |阅读模式
我做的是一个串口输入定时时间然后,时间到之后控制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);
}
回复

使用道具 举报

发表于 2014-4-17 20:48:52 | 显示全部楼层
沒用過 1302 的庫, 但從程式上看, 有點矛盾.

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

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

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


  1. Time otime;
  2. :
  3. :
  4. if(otime.hr==h1&&otime.min==m1)
  5. digitalWrite(13,HIGH);
  6. if(otime.hr==h2&&otime.min==m2)
  7. digitalWrite(13,LOW);
复制代码


回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 13:34 , Processed in 0.129207 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表