//www.elegoo.com
//2016.12.9
/*
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
#define LED 13 //LED on the bord
#define BYOU_ON 50 // 秒を表現するLEDをつけている時間 (単位:ミリ秒)
#define BYOU_OFF 1000 - BYOU_ON // 秒を表現するLEDを消している時間 (単位:ミリ秒)
#define LED_MIDORI 4 // 緑色LEDの端子番号
#define LED_KIIRO 2 // 黄色LEDの端子番号
#define LED_AKA 3 // 赤色LEDの端子番号
// タイマー時間設定(単位:秒)
#define TIMER_JIKAN 6
// 残り時間を表現するLEDの制御時間
#define KIIRO_JIKAN TIMER_JIKAN / 3
#define AKA_JIKAN TIMER_JIKAN / 3 * 2
// 残り時間表示用LEDの制御関数
void controlTimeLed(uint8_t midori, uint8_t kiiro, uint8_t aka) {
digitalWrite(LED_MIDORI, midori);
digitalWrite(LED_KIIRO, kiiro);
digitalWrite(LED_AKA, aka);
}
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Hello, World!");
pinMode(LED_BUILTIN, OUTPUT);//LED on the bord
// タイマー開始時に緑色LEDを点灯
controlTimeLed(HIGH, LOW, LOW);
pinMode(6, OUTPUT);
}
void loop() {
// 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:
//lcd.print(millis() / 1000);
// lcd.print("3");
uint8_t count; // for文で回数を数えるために使用する変数
digitalWrite(6, HIGH);
// TIMER_JIKAN分の回数を数える
for( count=0; count<TIMER_JIKAN; count++) {
lcd.setCursor(0, 1);
lcd.print(count);
// 残り時間表現用LEDの制御
if( count == KIIRO_JIKAN ) {
controlTimeLed(LOW, HIGH, LOW);
}
if( count == AKA_JIKAN ){
controlTimeLed(LOW, LOW, HIGH);
}
// 残り時間に応じて処理を変える
if( count >= ( 5) ) {
// 残り時間が5秒以下になったら、1秒に1回青色LEDを点滅して音を鳴らす
digitalWrite(LED, HIGH);
delay(BYOU_ON);
digitalWrite(LED, LOW);
delay(BYOU_OFF);
} else {
// そうでなければ、1秒に1回青色LEDを点滅する
digitalWrite(LED, HIGH);
delay(BYOU_ON);
digitalWrite(LED, LOW);
delay(BYOU_OFF);
}
}
// 時間になったので秒のLED(青色LED)を点灯する
digitalWrite(LED_BUILTIN, HIGH);
lcd.setCursor(0, 1);
lcd.print(count);
// 何もしないで待つ
while( true ) {
controlTimeLed(HIGH, HIGH, HIGH);
lcd.begin(16, 2);
lcd.autoscroll();
lcd.print("JIKANdayo");
controlTimeLed(LOW, LOW, LOW);
}
}