マイコンArduinoを使って電光掲示板を作る。
続き
今回は、定型文(5種類)を選択できるように改造していきます。
"こんにちは"
"おはよう"
"ありがとう"
"おやすみ"
"さようなら"
を切替表示出来るようにします。
それに合わせてブレッドボードにもう一つタクトスイッチを追加しました。
D14に接続、配線は他のタクトスイッチと同じ
切替表示の様子
今回のプログラム
// slx_5024
byte address[ ] = {5, 4, 3, 2}; // アドレスは扱いやすい様に配列で持つ
int pos = 0;
int count = 0;
int scroll = 0;
int scroll_speed = 0;
boolean toggle_high = false;
boolean toggle_low = false;
int s_s[] = {1, 10, 30, 70, 150};
int s_s_pos = 2;
short *data_hex;
byte *color;
int data_size;
int select = 1;
boolean toggle_select = false;
boolean change_flag = false;
// "こんにちは"
short data_hex_1[ ] = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x1ff8, 0x00e0, 0x0180, 0x0000, 0x0000, 0x1000,
0x1800, 0x2000, 0x2000, 0x2000, 0x3000, 0x1ffc, 0x0000, 0x0000,
0x0000, 0x0600, 0x0400, 0x0c00, 0x0c00, 0x0800, 0x1800, 0x1f80,
0x1882, 0x30c2, 0x30c6, 0x20c6, 0x60c4, 0x607c, 0x4030, 0x0000,
0x0000, 0x3000, 0x33fc, 0x33fc, 0x2000, 0x2000, 0x2000, 0x2000,
0x2300, 0x2200, 0x2200, 0x2300, 0x21fc, 0x30fc, 0x3000, 0x0000,
0x0000, 0x0400, 0x0400, 0x7ffe, 0x0400, 0x0400, 0x0ce0, 0x0ff8,
0x0c0c, 0x1804, 0x0004, 0x000c, 0x001c, 0x0ff8, 0x0fc0, 0x0000,
0x0000, 0x3020, 0x2020, 0x2020, 0x27fe, 0x2020, 0x2020, 0x6030,
0x6030, 0x63f0, 0x2638, 0x243c, 0x2426, 0x23e0, 0x2180, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
// "おはよう"
short data_hex_2[ ] = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0618, 0x060c, 0x7fec, 0x0606, 0x0602, 0x07e0, 0x0ff8,
0x1e0c, 0x2606, 0x6606, 0x6606, 0x260c, 0x3ef8, 0x00e0, 0x0000,
0x0000, 0x3020, 0x2020, 0x2020, 0x27fe, 0x2020, 0x2020, 0x6030,
0x6030, 0x63f0, 0x2638, 0x243c, 0x2426, 0x23e0, 0x2180, 0x0000,
0x0000, 0x0180, 0x0180, 0x0180, 0x01fc, 0x0180, 0x0180, 0x0180,
0x0180, 0x1fc0, 0x31f0, 0x219c, 0x618e, 0x3f04, 0x1e00, 0x0000,
0x0000, 0x0fc0, 0x00f0, 0x0000, 0x0000, 0x1ff0, 0x3c18, 0x0008,
0x0008, 0x0008, 0x0008, 0x0018, 0x0030, 0x01e0, 0x0f80, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
// "ありがとう"
short data_hex_3[ ] = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0600, 0x0600, 0x3ffc, 0x0600, 0x06c0, 0x0ff8, 0x1e4c,
0x36c6, 0x2286, 0x6386, 0x6306, 0x3e0c, 0x1cf8, 0x00c0, 0x0000,
0x0000, 0x0800, 0x18c0, 0x1bf0, 0x1610, 0x1c18, 0x1818, 0x1808,
0x1008, 0x1008, 0x1018, 0x0010, 0x0070, 0x0fe0, 0x0f00, 0x0000,
0x0004, 0x041a, 0x0c0a, 0x0c11, 0x7fc8, 0x084c, 0x0866, 0x0866,
0x1866, 0x1062, 0x1060, 0x3060, 0x2040, 0x67c0, 0x0100, 0x0000,
0x0000, 0x0200, 0x0200, 0x0200, 0x0200, 0x027c, 0x03e0, 0x0e00,
0x1800, 0x3000, 0x2000, 0x2000, 0x3000, 0x1ffc, 0x03fc, 0x0000,
0x0000, 0x0fc0, 0x00f0, 0x0000, 0x0000, 0x1ff0, 0x3c18, 0x0008,
0x0008, 0x0008, 0x0008, 0x0018, 0x0030, 0x01e0, 0x0f80, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
// "おやすみ"
short data_hex_4[ ] = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0618, 0x060c, 0x7fec, 0x0606, 0x0602, 0x07e0, 0x0ff8,
0x1e0c, 0x2606, 0x6606, 0x6606, 0x260c, 0x3ef8, 0x00e0, 0x0000,
0x0000, 0x1180, 0x18c0, 0x1878, 0x0bfc, 0x1e66, 0x7c02, 0x4406,
0x0406, 0x06fc, 0x0600, 0x0200, 0x0300, 0x0300, 0x0300, 0x0000,
0x0000, 0x0040, 0x0040, 0x7ffe, 0x0040, 0x03c0, 0x0ec0, 0x0840,
0x0860, 0x0860, 0x0fe0, 0x0340, 0x00c0, 0x0780, 0x0e00, 0x0000,
0x0000, 0x3f80, 0x3f80, 0x0100, 0x010c, 0x010c, 0x010c, 0x1ff8,
0x331e, 0x621e, 0x4218, 0x4610, 0x6c20, 0x38e0, 0x0180, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
// "さようなら"
short data_hex_5[ ] = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0080, 0x0080, 0x00c4, 0x7ffe, 0x0040, 0x0060, 0x0420,
0x0e30, 0x1018, 0x3000, 0x3000, 0x3000, 0x1ff8, 0x03f0, 0x0000,
0x0000, 0x0180, 0x0180, 0x0180, 0x01fc, 0x0180, 0x0180, 0x0180,
0x0180, 0x1fc0, 0x31f0, 0x219c, 0x618e, 0x3f04, 0x1e00, 0x0000,
0x0000, 0x0fc0, 0x00f0, 0x0000, 0x0000, 0x1ff0, 0x3c18, 0x0008,
0x0008, 0x0008, 0x0008, 0x0018, 0x0030, 0x01e0, 0x0f80, 0x0000,
0x0000, 0x0c00, 0x0c00, 0x7fbe, 0x0830, 0x0830, 0x1830, 0x1030,
0x1030, 0x33f0, 0x2638, 0x643e, 0x6432, 0x07e0, 0x01c0, 0x0000,
0x0000, 0x07e0, 0x00f0, 0x1800, 0x1000, 0x1000, 0x11f0, 0x171c,
0x180c, 0x1004, 0x0004, 0x000c, 0x001c, 0x1ff0, 0x0f80, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
// カラーを配列で持つ
byte color_1[ ] = {1, 1, 1, 1, 1}; // 1,赤 2,緑 3,橙
byte color_2[ ] = {2, 2, 2, 2}; // 1,赤 2,緑 3,橙
byte color_3[ ] = {3, 3, 3, 3, 3}; // 1,赤 2,緑 3,橙
byte color_4[ ] = {2, 2, 2, 2}; // 1,赤 2,緑 3,橙
byte color_5[ ] = {3, 3, 3, 3, 3}; // 1,赤 2,緑 3,橙
void setup() {
for (int i = 0; i < 11; i++) {
pinMode(i, OUTPUT); // 各ピンの設定 全てアウトプット
}
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
digitalWrite(0,LOW); // SE L 自動でRAM切り替わり
digitalWrite(1,LOW); // A/BB 書き込み用RAM選択
digitalWrite(6,LOW); // GREEN 緑データ
digitalWrite(7,LOW); // CLK クロック
digitalWrite(8,LOW); // WE 書き込み信号 Hで書き込み
digitalWrite(9,LOW); // RED 赤データ
digitalWrite(10,LOW); // ALE アドレスラッチ Hで有効
change(select);
}
void loop() {
for (int i = 0; i < 16; i++) { // アドレス0~15
for (int j = 0; j < sizeof(address); j++)
if ((i & 0x1 << j) != 0) // address[0]~address[3]にビットをセット
digitalWrite(address[j], HIGH);
else
digitalWrite(address[j], LOW);
for (int k = 0;k < 32;k++) {
digitalWrite(6,LOW); // GREEN 緑データ
digitalWrite(9,LOW); // RED 赤データ
if ((data_hex[k + scroll] & 0x8000 >> i) != 0) {
int color_pos = (k + scroll - 32) / 16;
if (color_pos < 0) color_pos = 0;
byte c = color[color_pos];
if (c & 1)
digitalWrite(9,HIGH); // RED 赤データ
if (c & 2)
digitalWrite(6,HIGH); // GREEN 緑データ
}
clock(); // クロック
}
write_ale_we(); // 書き込み操作 関数にまとめて有る
}
count++;
if (count > scroll_speed) {
count = 0;
scroll ++;
if (scroll > data_size - 32)
scroll = 0;
pos++;
if (pos > 15) {
pos = 0;
}
}
delay(s_s[s_s_pos]);
// スピード可変
if (digitalRead(11) == 0 && !toggle_high) {
toggle_high = true;
if (s_s_pos > 0)
s_s_pos--;
}
if (digitalRead(11) == 1) {
toggle_high = false;
}
if (digitalRead(12) == 0 && !toggle_low) {
toggle_low = true;
if (s_s_pos < 4)
s_s_pos++;
}
if (digitalRead(12) == 1) {
toggle_low = false;
}
if (digitalRead(14) == 0 && !toggle_select) {
toggle_select = true;
select++;
if (select > 5)
select = 1;
change(select);
scroll = 0;
delay(100);
}
if (digitalRead(14) == 1) {
toggle_select = false;
}
}
void write_ale_we() {
digitalWrite(10,HIGH); // ALE アドレスラッチ Hで有効
digitalWrite(8,HIGH); // WE 書き込み信号 Hで書き込み
digitalWrite(8,LOW); // WE 書き込み信号 Hで書き込み
digitalWrite(10,LOW); // ALE アドレスラッチ Hで有効
}
void clock() {
digitalWrite(7,HIGH); // クロック
digitalWrite(7,LOW); // クロック
}
void clock_n(int n) {
for (int i = 0;i < n;i++)
clock();
}
void change(int num) {
switch (num) {
case 1:
data_size = sizeof(data_hex_1) / 2;
data_hex = data_hex_1;
color = color_1;
break;
case 2:
data_size = sizeof(data_hex_2) / 2;
data_hex = data_hex_2;
color = color_2;
break;
case 3:
data_size = sizeof(data_hex_3) / 2;
data_hex = data_hex_3;
color = color_3;
break;
case 4:
data_size = sizeof(data_hex_4) / 2;
data_hex = data_hex_4;
color = color_4;
break;
case 5:
data_size = sizeof(data_hex_5) / 2;
data_hex = data_hex_5;
color = color_5;
break;
}
if (change_flag)
return;
short local_hex[data_size]; // sizeofの戻り値がバイト数なので2で割る
// データをクリア
for (int n = 0;n < data_size;n++) { // sizeofの戻り値がバイト数なので2で割る
local_hex[n] = 0; // ビット数は不要 0で16ビット全て0
}
for (int i = 0;i < data_size / 16;i++) { // 32バイトで1文字
for (int j = 0;j < 16;j++) { // 16はデーター数
for (int k = 0;k < 16;k++) { // 16はビット数
if ((data_hex[j + i * 16] & 0x8000 >> k) != 0) // 横データを
local_hex[k + i * 16] |= 0x8000 >> j; // 縦データに
}
}
}
for (int n = 0;n < data_size;n++) { // sizeofの戻り値がバイト数なので2で割る
data_hex[n] = local_hex[n]; // 元に戻す
}
if (num == 5)
change_flag = true;
}
太字が改造した部分
少し改造すれば1~5を順番に表示できる様にもなります。
このモジュールは連結して表示数を増やすことが簡単ですので2枚セットで4文字、4枚セットで8文字と本格的な電光掲示板にも増設出来ます。
一度トライしてみて下さい。
今回のシリーズは如何だったでしょうか?
面白そうだから作って見ようという気になって頂けたら幸いです。
次回からまた、新たにゲームを作るシリーズを始めます。
では、また。