Created
September 9, 2018 10:52
-
-
Save KatsuhiroMorishita/5d3d462b447d841752f7ec2473e268e3 to your computer and use it in GitHub Desktop.
Arduinoに接続されたRTCの時刻をシリアルモニタで調整するサンプルプログラムです。ネット上に転がっているプログラムはビルドした時刻がセットされるものがほとんどですが、このプログラムはシリアルモニタを使って任意の日時にセットできます。デフォルトでは、Stalker v3.1に搭載されているDS1337を対象にしていますが、他のICでもほとんど改造せずに使えるはずです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arduinoに接続されたRTCの時刻をシリアルモニタで調整するサンプルプログラム | |
// memo: デフォルトでは、Stalker v3.1に搭載されているDS1337を対象にしていますが、他のICでもほとんど改造せずに使えるはずです。 | |
// author: Katsuhiro Morihsita | |
// created: 2018-09-07 | |
// license: MIT | |
#include <DS1337.h> | |
// RTC関係 | |
DS1337 RTC; | |
void setup() { | |
// Serial setting | |
Serial.begin(57600); | |
Serial.println("**Program Start**"); | |
delay(100); | |
// RTC setup | |
RTC.begin(); | |
set_RTC(); | |
Serial.println("--setup fin--"); | |
} | |
void loop() { | |
// 約1秒ごとにRTCから時刻を取得して表示 | |
DateTime t = RTC.now(); | |
Serial.println(t.iso8601()); | |
delay(1000); | |
} | |
// RTCの時刻を調整します。呼び出す前に、RTC.begin();を実行してください。 | |
void set_RTC(){ | |
const long wait_time = 30000l; // 入力を待ち受ける時間。unit is ms. | |
Serial.println("This is RTC time adjust mode."); | |
Serial.println("Input date. format is year,month,day,hour,min,sec,week."); | |
Serial.println("The week is Su=0 Mo=1 Tu=3 We=4 Th=5 Fr=6 Sa=7."); | |
Serial.println("For example, '2018,9,8,22,5,10,7'."); | |
Serial.println("If you want to exit, input just '*'."); | |
Serial.print("Waiting time is "); | |
Serial.print(wait_time); | |
Serial.println(" ms."); | |
long start_time = millis(); | |
while(millis() - start_time < wait_time){ | |
String line = Serial.readString(); // "2018,9,8,22,5,10,7¥r¥n"みたいな文字列を期待 | |
line.trim(); // 改行コードを削除 | |
if(line.length() > 0){ | |
Serial.print("text: "); | |
Serial.println(line); | |
// 初期チェック | |
int k = line.indexOf("*"); // *が入っていたら、モードを抜ける | |
if(k > 0){ | |
Serial.println("A * was found in text. So exit()."); | |
return; | |
} | |
// 文字列解析 | |
int count = 0; // カウントしたフィールドの数 | |
int from = 0; // 文字列を探索開始する位置 | |
const int arr_size = 7; // フィールドの数 | |
String date_strings[arr_size]; // date strings splited by ','. | |
while(from <= line.length()){ // 受信した文字列をチェックしてカンマをカウントしつつ文字列を切り出す | |
int p = line.indexOf(",", from); | |
if(p > 0){ // カンマを発見 | |
date_strings[count] = line.substring(from, p); | |
count++; | |
from = p + 1; | |
if(p + 1 == line.length()){ // weekがない場合(最後がカンマ) | |
count = 0; | |
break; | |
} | |
}else{ // 最後のフィールドを取得。なお、カンマが1つもなくてもここは実行される。 | |
date_strings[arr_size - 1] = line.substring(from, line.length()); | |
count++; | |
break; | |
} | |
if(count >= arr_size) // カンマが多すぎたら処理しないように処理(もし8つ以上のフィールドが有っても無視) | |
break; | |
} | |
if(count == 7){ // 7つ分のフィールドから数値を取得して、日時をRTCにセット | |
uint16_t year_i = (uint16_t)date_strings[0].toInt(); // 整数以外を入力すると、0が返る | |
uint8_t month_i = (uint8_t)date_strings[1].toInt(); | |
uint8_t day_i = (uint8_t)date_strings[2].toInt(); | |
uint8_t hour_i = (uint8_t)date_strings[3].toInt(); | |
uint8_t min_i = (uint8_t)date_strings[4].toInt(); | |
uint8_t sec_i = (uint8_t)date_strings[5].toInt(); | |
uint8_t week_i = (uint8_t)date_strings[6].toInt(); /*Su=0 Mo=1 Tu=3 We=4 Th=5 Fr=6 Sa=7 */ | |
DateTime time_data(year_i, month_i, day_i, hour_i, min_i, sec_i, week_i); // 時刻オブジェクトを作成 | |
Serial.print("input date: "); | |
Serial.println(time_data.iso8601()); // 確認のために表示 | |
RTC.adjust(time_data); // RTCにセット | |
DateTime t_RTC = RTC.now(); // セットできたかチェック | |
Serial.print("read date from RTC: "); | |
Serial.println(t_RTC.iso8601()); | |
return; | |
}else{ | |
Serial.println("check data format."); | |
} | |
} | |
delay(10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment