Arduinoで作ろう(14) ラーメンタイマー
aitendoで買った Multi function Shieldで3分間ラーメンタイマーを作ってみました。
LCDに比べて7セグLEDを点けるのはちょっと面倒ですが,やっぱりなんだか魅力的ですよね。
ココに取説のPDFがあるし,ココにライブラリ,ココにサンプルスケッチがありますが,ライブラリを使わないでラーメンタイマーを作ってみましょう。
このシールドには,74HC595が2個ついていて,Latch,Clock,SDIはそれぞれ4,7,8ピンです。
0~9の7セグ(アノードコモン)点灯パターンの配列を作って,各桁に入れる数字のためのバッファ配列も作っておきます。
millis()を使って残り時間を計算し, shiftOutで7セグLEDに数字データを送ってやります。
3分立ったら「チャルメラ」の音楽が鳴るようになってます(^^)
スケッチはこちら→ 20180625.ino
/*
Mult_function_shield
ラーメンタイマー180秒
*/
const int buz = 3; // Buzzerピン
const int LAT = 4; // Latchピン
const int CLK = 7; // Clockピン
const int SDI = 8; // Serial Data In ピン
const int time = 180;
int timer = time; //残り時間
int Buffer[4] = {0}; //1~4桁 数字のバッファ
const byte digit[10] = //7seg 点灯パターン
{
B00000011, // 数字 0
B10011111, // 1
B00100101, // 2
B00001101, // 3
B10011001, // 4
B01001001, // 5
B01000001, // 6
B00011011, // 7
B00000001, // 8
B00001001 // 9
};
const byte keta[4] =
{
B00010000, //1桁め
B00100000, //2桁め
B01000000, //3桁め
B10000000 //4桁め
};
void setup() {
pinMode(SDI, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(LAT, OUTPUT);
pinMode(buz, OUTPUT);
}
void loop() {
timer = time - millis() / 1000;
if (timer > 0) {
Buffer[3] = int(timer) / 1000;
Buffer[2] = (int(timer) % 1000) / 100;
Buffer[1] = (int(timer) % 100) / 10;
Buffer[0] = (int(timer) % 100) % 10;
Disp();
}
else if (timer == 0) {
digitalWrite(LAT, LOW);
shiftOut(SDI, CLK, LSBFIRST, 0b00000011); //数字 0
shiftOut(SDI, CLK, LSBFIRST, B00010000); //1桁目
digitalWrite(LAT, HIGH);
tone(buz, 392, 200); delay(200); //チャルメラ
tone(buz, 440, 200); delay(200);
tone(buz, 494, 800); delay(800);
tone(buz, 440, 200); delay(200);
tone(buz, 392, 200); delay(800);
tone(buz, 392, 200); delay(200);
tone(buz, 440, 200); delay(200);
tone(buz, 494, 200); delay(200);
tone(buz, 440, 200); delay(200);
tone(buz, 392, 200); delay(200);
tone(buz, 440, 1000); delay(1000);
}
else {
}
}
void Disp() {
for (byte j = 0; j < 4; j++) {
digitalWrite(LAT, LOW);
shiftOut(SDI, CLK, LSBFIRST, digit[Buffer[j]]);
shiftOut(SDI, CLK, LSBFIRST, keta[j]); // 桁
digitalWrite(LAT, HIGH);
delay(4);
}
}
| 固定リンク | 0
「Arduino」カテゴリの記事
- WCH-LinkE とWCH-Link は別モノである(2023.12.02)
- Arduinoで作ろう(63) UNO R4 WiFiでスクロールNTP時計(2023.12.01)
- Arduinoで作ろう(62) ESP8266+SSD1306でNTP時計(2023.11.18)
- 新型 Arduino UNO R4 MINIMA を買った(2023.07.01)
「aitendo」カテゴリの記事
- aitendo 2023お楽しみ福袋(2023.01.02)
- aitendo 2022 お楽しみ福袋(2022.01.06)
- ZK-80で足し算(2020.07.23)
- TK-80互換機 ZK-80のキット(2020.07.21)
- Arduinoで作ろう(47) GPSモジュールで現在地の緯度・経度を取得(2020.04.18)