用ds3231制作定时喂鱼,出现问题,求大神帮忙。
我想用ds3231定时,每天给鱼喂食。程序下载后,舵机马上不停工作,没有等到7点。前面从 ds3231获得时间并显示到电脑是没问题的,下面是主程序部分,请问大神,这是哪里出问题?void loop()
{
ReadDS3231();
while(hour =7){ for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(15);
}
} 这个程序应该在hour=7时一直工作,可能持续1一个小时。
是不是一到7点就工作,喂一次就停止?如果这样,建议将时、分、秒都用上;或用 if 语句,判断后跳转。
#include <DS3231.h>
#include <Wire.h>
#include <Servo.h>
Servo myservo;// 定义舵机对象,最多八个
int pos = 0; // 定义舵机转动位置
DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
byte year, month, date, DoW, hour, minute, second;
void setup() {
// Start the I2C interface
Wire.begin();
Clock.setSecond(50);//Set the second
Clock.setMinute(59);//Set the minute
Clock.setHour(11);//Set the hour
Clock.setDoW(5); //Set the day of the week
Clock.setDate(31);//Set the date of the month
Clock.setMonth(5);//Set the month of the year
Clock.setYear(13);//Set the year (Last two digits of the year)
// Start the serial interface
Serial.begin(115200);
myservo.attach(9);// 设置舵机控制针脚
}
void ReadDS3231()
{
int second,minute,hour,date,month,year,temperature;
second=Clock.getSecond();
minute=Clock.getMinute();
hour=Clock.getHour(h12, PM);
date=Clock.getDate();
month=Clock.getMonth(Century);
year=Clock.getYear();
temperature=Clock.getTemperature();
Serial.print("20");
Serial.print(year,DEC);
Serial.print('-');
Serial.print(month,DEC);
Serial.print('-');
Serial.print(date,DEC);
Serial.print(' ');
Serial.print(hour,DEC);
Serial.print(':');
Serial.print(minute,DEC);
Serial.print(':');
Serial.print(second,DEC);
Serial.print('\n');
Serial.print("Temperature=");
Serial.print(temperature);
Serial.print('\n');
}
void Myservo()
{
// 0到180旋转舵机,每次延时15毫秒
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
// 180到0旋转舵机,每次延时15毫秒
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(15);
}
}
void loop()
{
ReadDS3231();
if(minute ==1,second ==1){Myservo();}
delay(1000);
}
while(hour == 7) 谢谢大神指导,==才是逻辑判断,=是负值。我改了一下,但是现在时间到了,但是不会动。我把程序附在下面,麻烦大神看看是什么问题。
#include <DS3231.h>
#include <Wire.h>
#include <Servo.h>
Servo myservo;// 定义舵机对象,最多八个
int pos = 0; // 定义舵机转动位置
DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
byte year, month, date, DoW, hour, minute, second;
void setup() {
// Start the I2C interface
Wire.begin();
Clock.setSecond(30);//Set the second
Clock.setMinute(52);//Set the minute
Clock.setHour(9);//Set the hour
Clock.setDoW(3); //Set the day of the week
Clock.setDate(25);//Set the date of the month
Clock.setMonth(10);//Set the month of the year
Clock.setYear(17);//Set the year (Last two digits of the year)
// Start the serial interface
Serial.begin(115200);
myservo.attach(9);// 设置舵机控制针脚
}
void ReadDS3231()
{
int second,minute,hour,date,month,year,temperature;
second=Clock.getSecond();
minute=Clock.getMinute();
hour=Clock.getHour(h12, PM);
date=Clock.getDate();
month=Clock.getMonth(Century);
year=Clock.getYear();
temperature=Clock.getTemperature();
Serial.print("20");
Serial.print(year,DEC);
Serial.print('-');
Serial.print(month,DEC);
Serial.print('-');
Serial.print(date,DEC);
Serial.print(' ');
Serial.print(hour,DEC);
Serial.print(':');
Serial.print(minute,DEC);
Serial.print(':');
Serial.print(second,DEC);
Serial.print('\n');
Serial.print("Temperature=");
Serial.print(temperature);
Serial.print('\n');
}
void loop()
{
ReadDS3231();
if(hour==9&&minute==52&&second==35){
// 0到180旋转舵机,每次延时15毫秒
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
// 180到0旋转舵机,每次延时15毫秒
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(15);
}
}
delay(1000);
}
本帖最后由 kirbyklein 于 2017-10-25 15:56 编辑
你的ReadDS3231()函数里把int second,minute,hour,date,month,year,temperature; 这句去掉,要不然你下面的赋值second=Clock.getSecond();
minute=Clock.getMinute();
hour=Clock.getHour(h12, PM);
date=Clock.getDate();
month=Clock.getMonth(Century);
year=Clock.getYear();
就存在你前面定义的局部变量里了,而没有存到byte year, month, date, DoW, hour, minute, second;这些全局变量里,导致下面得不到时间,我想可能是这个问题 kirbyklein 发表于 2017-10-25 15:51
你的ReadDS3231()函数里把int second,minute,hour,date,month,year,temperature; 这句去掉,要不然你下面的 ...
说的对!!!
另外,在setup()里面,设置时间有问题,即DS3231设置只要设置一次,以后都不需要,除非产生较大误差时进行校正。 kirbyklein 发表于 2017-10-25 15:51
你的ReadDS3231()函数里把int second,minute,hour,date,month,year,temperature; 这句去掉,要不然你下面的 ...
太感谢大神指导,是函数定义范围的问题。我把这句函数定义放到setup()里面,loop()里面就可以引用了,问题解决。可是temperature移过去,编译就出错,所以我把temperature还是放在ReadDS3231()函数里。不影响使用。万分感谢!
页:
[1]