R2は、最終470Ωで落ち着きました。
#include <LiquidCrystal.h>
#include <TimeLib.h>
//LiquidCrystal(rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(7, 6, 2, 3, 4, 5);
#define kTimeOffset (9 * 3600)
char* GetLine(char c);
boolean Display(char s[]); //GPS生文字
//*****************************
// Bell
//*****************************
void Bell(boolean start) {
static uint32_t nowT, oldT;
static uint8_t count;
static uint8_t st = 0;
switch (st) {
case 0:
if (start == true) {
count = 3;
oldT = millis();
analogWrite(9, 127);
st = 1;
}
break;
//0分のベル
case 1:
nowT = millis();
if ((nowT - oldT) > 50) {
oldT = millis();
analogWrite(9, 0);
st = 2;
}
break;
case 2:
nowT = millis();
if ((nowT - oldT) > 50) {
oldT = millis();
count--;
if (count) {
analogWrite(9, 127);
st = 1;
} else {
st = 3;
}
}
break;
case 3:
if (second() != 0) st = 0;
break;
}
}
//*****************************
// setup
//*****************************
void setup(void) {
Serial.begin(115200);
Serial1.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(1, 0);
lcd.print("CLOCK Ver1.0");
delay(1000);
lcd.clear();
}
//*****************************
// loop
//*****************************
void loop(void) {
char *s, c;
boolean bell = false;
if (Serial1.available()) {
c = Serial1.read();
Serial.write(c);
s = GetLine(c);
if (s) {
bell = Display(s);
}
}
Bell(bell);
}
//*****************************
// GetLine
//*****************************
char* GetLine(char c) {
static char rx[128];
static uint8_t pt = 0;
static uint8_t st = 0;
char* str = 0;
switch (st) {
case 0:
if (c == '$')
st = 1;
break;
case 1:
if (c == '*') {
pt = 0;
str = rx;
st = 0;
} else {
if (pt >= 120) pt = 0; //おそらく電波弱くてエラー
rx[pt++] = c;
}
break;
}
return str;
}
//*****************************
// Display
//*****************************
boolean Display(char s[]) {
uint16_t ye;
uint8_t mo, da, week;
static uint8_t ho, mi, se;
char line1[17];
char mor;
boolean res;
res = false;
if (strncmp("GPGGA", s, 5) == 0) {
ho = s[6] - '0';
ho *= 10;
ho += (s[7] - '0') * 1;
mi = s[8] - '0';
mi *= 10;
mi += (s[9] - '0') * 1;
se = s[10] - '0';
se *= 10;
se += (s[11] - '0') * 1;
}
if (strncmp("GPZDA", s, 5) == 0) { //この後、時間余り
ye = s[23] - '0';
ye *= 1000;
ye += (s[24] - '0') * 100;
ye += (s[25] - '0') * 10;
ye += (s[26] - '0') * 1;
mo = s[20] - '0';
mo *= 10;
mo += (s[21] - '0') * 1;
da = s[17] - '0';
da *= 10;
da += (s[18] - '0') * 1;
setTime(ho, mi, se, da, mo, ye);
adjustTime(kTimeOffset);
sprintf(line1, "%4d.%2d.%2d", year(), month(), day());
lcd.setCursor(2, 0);
lcd.print(line1);
ho = hour();
mor = 'A';
if (ho >= 12) {
ho -= 12;
mor = 'P';
}
sprintf(line1, "%cM%2d:%02d", mor, ho, minute());
lcd.setCursor(3, 1);
lcd.print(line1);
ho = hour();
if (ho >= 5 && ho <= 21) {
//5時から21時までの0分でブザーを鳴らす
if (minute() == 0 && second() == 0) res = true;
}
}
return res;
}
2022/8/22 analogWriteは、ループで同じ値でも再設定すると×みたい。直さないと…
2022/8/26 ↑修正しました
2022/9/11 文字数上限追加