极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10787|回复: 1

為什麼我把兩組編碼合在一起就運行不了?

[复制链接]
发表于 2014-5-21 16:37:36 | 显示全部楼层 |阅读模式
我這組編碼是用來顯示時間和控制餵食機的,但不合道為什麼時間的秒數會不動,20秒後LCD會被清屏,不斷循環,有高人可以指點一下嗎?

/*
Arduino ?接 DS1302
代??源:http://quadpoint.org/projects/arduino-ds1302
增加了串口?整??代?
*/
    #include <LiquidCrystal.h>
#include <stdio.h>
#include <string.h>
#include <DS1302.h>
#include <Servo.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


/* 接口定?
CE(DS1302 pin5) -> Arduino D5
IO(DS1302 pin6) -> Arduino D6
SCLK(DS1302 pin7) -> Arduino D7
*/
uint8_t CE_PIN   = 6;
uint8_t IO_PIN   = 7;
uint8_t SCLK_PIN = 8;

/* 日期?量?存 */
char buf[50];
char day[10];
/* 串口?据?存 */
String comdata = "";
int numdata[7] ={0}, j = 0, mark = 0;
/* ?建 DS1302 ?象 */
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

//long FEEDINGDELAY = 43200000; // 12 hours
  long FEEDINGDELAY = 20000; // 16 hours
//long FEEDINGDELAY = 60000;

long endtime;
long now;

void print_time()
{
    /* ? DS1302 ?取?前?? */
    Time t = rtc.time();
    /* ?星期??字???名? */
    memset(day, 0, sizeof(day));
    switch (t.day)
    {
    case 1: strcpy(day, "Sun"); break;
    case 2: strcpy(day, "Mon"); break;
    case 3: strcpy(day, "Tue"); break;
    case 4: strcpy(day, "Wed"); break;
    case 5: strcpy(day, "Thur"); break;
    case 6: strcpy(day, "Fri"); break;
    case 7: strcpy(day, "Sat"); break;
    }
    /* ?日期代?格式化?成buf等待?出 */
    snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d ", day, t.yr, t.mon, t.date);
   lcd.clear();
      lcd.begin(16, 2);
      lcd.setCursor(0,0);
      lcd.print(buf);
      Serial.println(buf);
      snprintf(buf, sizeof(buf), "%02d:%02d:%02d", t.hr, t.min, t.sec);
      lcd.setCursor(1, 1);
      lcd.print(buf);               
    /* ?出日期到串口 */
    Serial.println(buf);
}


void setup()
{
    Serial.begin(9600);
    rtc.write_protect(false);
    rtc.halt(false);
   

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

  myservo.write(0);
  delay(15);
  
  
  // FEED THE FISH..


  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }

}

void loop()
{

    /* ?串口有?据的?候,??据拼接到?量comdata */
    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');
            }
        }
        /* ???好的numdata?成??格式,?入DS1302 */
        Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], numdata[6]);
        rtc.time(t);
        mark = 0;j=0;
        /* 清空 comdata ?量,以便等待下一次?入 */
        comdata = String("");
        /* 清空 numdata */
        for(int i = 0; i < 7 ; i++) numdata[i]=0;
    }
   
    /* 打印?前?? */
    print_time();
    delay(1000);
   
     // process times
  now = millis();
  endtime = now + FEEDINGDELAY;
  
  while(now < endtime) {
   myservo.write(0);

  
   
  }
  
  // FEED THE FISH..


  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                  
}
}
回复

使用道具 举报

发表于 2014-5-22 10:03:10 | 显示全部楼层
本帖最后由 Super169 于 2014-5-22 10:07 编辑

時間不動是正常, 先計算一下你的 loop() 執行一次要多少時間.
而且, 看程式應該是 loop 死的:


  1.   long FEEDINGDELAY = 20000; // 16 hours
  2. :
  3. :
  4.      // process times
  5.   now = millis();
  6.   endtime = now + FEEDINGDELAY;
  7.   
  8.   while(now < endtime) {
  9.    myservo.write(0);
  10. }
复制代码


now 跟 endtime 沒改動, while(now < endtime) always true
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 19:44 , Processed in 0.051852 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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