|
|
我這組編碼是用來顯示時間和控制餵食機的,但不合道為什麼時間的秒數會不動,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);
}
} |
|