极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 16018|回复: 7

用ds3231制作定时喂鱼,出现问题,求大神帮忙。

[复制链接]
发表于 2017-10-24 19:39:09 | 显示全部楼层 |阅读模式
我想用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);
      }
   

      }
回复

使用道具 举报

发表于 2017-10-24 21:13:17 | 显示全部楼层
这个程序应该在hour=7时一直工作,可能持续1一个小时。
是不是一到7点就工作,喂一次就停止?如果这样,建议将时、分、秒都用上;或用 if 语句,判断后跳转。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-24 21:58:03 | 显示全部楼层


#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);

}

回复 支持 反对

使用道具 举报

发表于 2017-10-25 00:17:16 | 显示全部楼层
while(hour == 7)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-25 10:21:11 | 显示全部楼层
谢谢大神指导,==才是逻辑判断,=是负值。我改了一下,但是现在时间到了,但是不会动。我把程序附在下面,麻烦大神看看是什么问题。


#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);

}

回复 支持 反对

使用道具 举报

发表于 2017-10-25 15:51:23 | 显示全部楼层
本帖最后由 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;这些全局变量里,导致下面得不到时间,我想可能是这个问题
回复 支持 反对

使用道具 举报

发表于 2017-10-25 18:44:12 | 显示全部楼层
kirbyklein 发表于 2017-10-25 15:51
你的ReadDS3231()函数里把int second,minute,hour,date,month,year,temperature; 这句去掉,要不然你下面的 ...

说的对!!!
另外,在setup()里面,设置时间有问题,即DS3231设置只要设置一次,以后都不需要,除非产生较大误差时进行校正。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-25 19:34:10 | 显示全部楼层
kirbyklein 发表于 2017-10-25 15:51
你的ReadDS3231()函数里把int second,minute,hour,date,month,year,temperature; 这句去掉,要不然你下面的 ...

太感谢大神指导,是函数定义范围的问题。我把这句函数定义放到setup()里面,loop()里面就可以引用了,问题解决。可是temperature移过去,编译就出错,所以我把temperature还是放在ReadDS3231()函数里。不影响使用。万分感谢!
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 22:27 , Processed in 0.038338 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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