2015/10/06(火)Arduinoでもタイマー

ざっくりと。

電池付きのRTCモジュール。
http://www.amazon.co.jp/dp/B00VP8EAFW

RTC操作用ライブラリ
https://github.com/SodaqMoja/Sodaq_DS3231

LCDも付けてあるけど、それは置いておいて。
時刻は律儀に変換しなくても、yyyymmdd の大小を比べれば過去か未来かわかる。
#include <Sodaq_DS3231.h>
#include <Wire.h>
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//DateTime dt(2015, 10, 07, 01, 17, 0, 3);
void setup() {
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
//  rtc.setDateTime(dt);
  // put your setup code here, to run once:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Artificial Sun for PAXI");
  pinMode(7, OUTPUT);
}

void loop() {
  char buf[16];
  int deg, hm, lighton, lightoff;
  delay(250);

  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  DateTime now = rtc.now();
  rtc.convertTemperature();
  deg = rtc.getTemperature();
  snprintf(buf, 16, "%02d:%02d:%02d %0d deg", now.hour(), now.minute(), now.second(), deg);
  lcd.print(buf);
  Serial.println(buf);

  // blink
  //digitalWrite(7, now.second() % 2 ? HIGH : LOW );

  // timer
  snprintf(buf, 5, "%02d%02d%02d", now.hour(), now.minute());
  hm = atoi(buf);
 // if (hm > 700 && hm < 1800) {
 if (hm > 115 && hm < 119) {
    digitalWrite(7, HIGH);
  } else {
    digitalWrite(7, LOW);
  }
}