m33spiral 发表于 2014-1-16 13:10:31

用ds3231和继电器做个定时插座 写了部分程序 但有点问题

本帖最后由 m33spiral 于 2014-1-16 13:12 编辑

打算做个定时控制器 可以从串口监视器设定当前时间和开关时间,现在loop中的时间判定好像有问题.... 是不是得用中断写???
#include <Wire.h>
#include <EEPROM.h>
#include <DS3231.h>

int Relay1=4;
int Relay2=5;
int Relay3=6;
int Relay4=7;
byte Year;
byte Month;
byte Date;
byte DoW;
byte Hour;
byte Minute;
byte Second;
byte OnHour;
byte OnMinute;
byte OffHour;
byte OffMinute;
bool PM;
bool h12;
bool Century=false;


DS3231 Clock;

void setup()
{
   int Address = 0;
   for (int i = 0; i < 4; i++)
   {
    pinMode(Relay1, OUTPUT);
   }
   
   Serial.begin(9600);
   Wire.begin();
   
    Serial.println("input Y to set time");
    delay(4000);
    if (Serial.read()==89)
    {
    Serial.println("year");
    delay(2000);
    Year=byte(Serial.parseInt());
    Serial.println("month");
    delay(2000);
    Month=byte(Serial.parseInt());
    Serial.println("date");
    delay(2000);
    Date=byte(Serial.parseInt());
    Serial.println("day");
    delay(2000);
    DoW=byte(Serial.parseInt());
    Serial.println("hour");
    delay(2000);
    Hour=byte(Serial.parseInt());
    Serial.println("minute");
    delay(2000);
    Minute=byte(Serial.parseInt());
    Serial.println("second");
    delay(2000);
    Second=byte(Serial.parseInt());
    Clock.setSecond(Second);//Set the second
    Clock.setMinute(Minute);//Set the minute
    Clock.setHour(Hour);//Set the hour
    Clock.setDoW(DoW);    //Set the day of the week
    Clock.setDate(Date);//Set the date of the month
    Clock.setMonth(Month);//Set the month of the year
    Clock.setYear(Year);
    }//
   Serial.println("input Y to set switch time");
       delay(4000);
       if (Serial.read()==89)
       {
         Serial.println("input OnHour");
         delay(2000);
         OnHour=byte(Serial.parseInt());
         Serial.println("input OnMinute");
         delay(2000);
         OnMinute=byte(Serial.parseInt());
         Serial.println("input OffHour");
         delay(2000);
         OffHour=byte(Serial.parseInt());
         Serial.println("input OffMinute");
         delay(2000);
         OffMinute=byte(Serial.parseInt());
         EEPROM.write(Address, OnHour);
         Address=Address+1;
         EEPROM.write(Address, OnMinute);
         Address=Address+1;
         EEPROM.write(Address, OffHour);
         Address=Address+1;
         EEPROM.write(Address, OffMinute);
       }
       else
       {
         OnHour=EEPROM.read(Address);
         Address=Address+1;
         OnMinute=EEPROM.read(Address);
         Address==Address+1;
         OffHour=EEPROM.read(Address);
         Address=Address+1;
         OffMinute=EEPROM.read(Address);
       }
               
   
   
   

   
}


void loop()
{
byte NowHour=Clock.getHour(h12,PM);
byte NowMinute=Clock.getMinute();
if((OnHour<=NowHour)&&(NowHour<OffHour))
{
      if ((OnMinute<=NowMinute)&&(NowMinute<OffMinute))
      {
      digitalWrite(13,HIGH);
      }
      else
      {
      digitalWrite(13,LOW);
      }
}
else
{
    digitalWrite(13,LOW);
}

}

zoologist 发表于 2014-1-16 14:33:27

你是打算让人手输入事件的么?

zoologist 发表于 2014-1-17 10:23:23

推荐你参考一下 http://digital-salvage.net/?p=36

m33spiral 发表于 2014-1-17 19:13:14

zoologist 发表于 2014-1-17 10:23 static/image/common/back.gif
推荐你参考一下 http://digital-salvage.net/?p=36

我程序是在loop里不断检测当前时间 如果 打开时间<=当前时间<关闭时间 就接通继电器 否则断开.....没用中断...是不是利用ds3231的中断会更好?
页: [1]
查看完整版本: 用ds3231和继电器做个定时插座 写了部分程序 但有点问题