目次 [ Contents ]
プログラミングだけでチャタリングを回避する方法の解説時、サンプルとしてシュウォッチもどきの連射ゲームのスケッチを書きましたが、それを進化させてみました。
必要な最低限のパーツはモーメンタリスイッチです。後はPCとArduinoを接続すればゲームを楽しめます。お子さんの夏休み自由課題なんかにもいいかもしれないですね。
“ゲージ判定”を使った、スイッチ読み取りライブラリを作ってみました。
(2017.6.26 追記)
簡単連打ゲーム “SHOOTER”
最低必要条件
- パソコン
- Arduino(Arduino UNO3でのみ動作確認済み)
- モーメンタリースイッチ
接続
ピンの接続は以下の通り。Arduino内蔵プルアップを使うので超単純です。
遊び方
Arduinoを通電すれば動きます。ArduinoIDEのシリアルモニタを立ち上げてください。
ルールは10秒間に何回スイッチを連打できるかを競う、それだけです。スイッチを押せば3秒カウント後ゲームがスタートします(13ピンのLEDが点滅します)。結果によってコメントが変わります。高得点を目指し連打しましょう。
ゲーム中にキャンセルしたければ、スイッチを長押しします。
高得点はEEPROMに記録されるので電源が切れても残ります。ベストスコアをリセットしたければ、スイッチを押したままArduinoの電源を入れます。
チューニング
マイコンによってはチャタリング回避の値を調整する必要があるかもしれません。以下のスケッチで、チャタリングと実際に押したときの値が分かります。2桁以上の差があると思うので、その結果を踏まえて適切なPUSH_SHORTの値を調べます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#define SW 4 #define PUSH_SHORT 700 unsigned long intvl = 0; void setup() { pinMode(SW, INPUT_PULLUP); Serial.begin(9600); } void loop() { unsigned long gauge = 0; while (!digitalRead(SW)) gauge++; if (gauge > 1) { char cate[2][5] = {"PUSH", "CHTT"}; bool jdg = (gauge <= PUSH_SHORT) ? 1 : 0; Serial.print(cate[jdg]); Serial.print(":"); Serial.println(gauge); intvl = millis(); } if ((millis() - intvl) > 1000) { Serial.println(); intvl = millis(); } } |
機能拡張
他にパーツを足せば、レトロゲームのような雰囲気のある感じで楽しめます。
LED
カウントダウンなど合図を確認するのに便利です。標準的なLEDで大丈夫ですが、ちゃんと抵抗も用意しましょう。
ブザー(ピエゾスピーカー)
一応音が出るようにしました。ショボイ音がレトロ感を演出してくれます。音さえ出れば安いやつでOKです。僕は手っ取り早く100円ショップのキッチンタイマーから抜き取りました。音量を調節したければ抵抗を挟んでください。
8*2 キャラクターLCDディスプレイ(I2C接続)
PCのシリアルモニタではなく、画面に出すといっそう昔の携帯ゲーム機ぽくなります。接続例では定格が5Vを想定しているので、気をつけてください。
配線図(拡張)
以上の拡張パーツと電池も用意すれば、携帯ゲームにもできます!
“SHOOTER” スケッチ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
#include <EEPROM.h> #include <Wire.h> char ver[8] = "ver 2.0"; #define print_go 1 #define lcd_go 1 #define PUSH_SHORT 700 #define PUSH_LONG 400000 #define LED 13 #define SW 4 #define BUZ 10 char titl [8] = "SHOOTER"; char tx_prs [6] = "PUSH"; char tx_aft [2][8] = {"Finish!", "Abort"}; #define tmp_num 10 char tmp_val [tmp_num] = {}; bool counting = false; bool indention = false; unsigned long time_to_go; #define DEF_SCR 160 #define SAVE_ADR 10 short score = 0; #define N 0 #define A 1 #define Bf 2 #define B 3 #define C 4 #define D 5 #define E 6 #define F 7 #define G 8 void setup() { pinMode(LED, OUTPUT); pinMode(BUZ, OUTPUT); pinMode(SW , INPUT_PULLUP); if (print_go) Serial.begin(9600); if (lcd_go) LCD_INI(); delay(10); DIALOG(titl, 0, 0); DIALOG(ver, 0, 1); if (MELODY(4) || !SCORE_READ()) SCORE_WRITE(0); delay(990); time_to_go = millis(); } void DIALOG(char *txt, byte x, byte y) { if (print_go) { if ( y != indention) { Serial.println(); indention = y; if (counting) indention = !indention; } Serial.print(txt); } if (lcd_go) { LCD_CUR(x, y); LCD_PRINT(txt); } } // ------------- BUTTON GAUGE byte BUTTON() { #define INTV 200 long gauge = 0; bool flick = true; unsigned long time_flick = millis(); while (!digitalRead(SW)) { gauge++; if (gauge >= PUSH_LONG) { digitalWrite(LED, flick); unsigned long cur = millis() - time_flick; if (cur > (INTV / 2)) flick = true; if (cur > INTV) { flick = false; time_flick = millis(); } } } if (gauge > PUSH_LONG) return 2; else if (gauge > PUSH_SHORT) return 1; else return 0; } // ------------- MAIN void loop() { bool skipper = false; static bool renew = true; if (STARTUP_SCRN(renew)) renew = false; if (BUTTON()) { if (lcd_go) LCD_CLR(); DIALOG("Ready", 0, 0); for (byte i = 0 ; i < 3 ; i++) { time_to_go = millis(); DIALOG(".", 5 + i, 0); while ((millis() - time_to_go) <= 200) { digitalWrite(LED, HIGH); tone(BUZ, 400, 200); } while ((millis() - time_to_go) <= 800) { digitalWrite(LED, LOW); } } tone(BUZ, G * 2); if (lcd_go) LCD_CLR(); DIALOG("GO!!!", 3, 0); int count = 0; int buz_time = 1000; unsigned long time_to_led = millis(); counting = true; time_to_go = millis(); digitalWrite(LED, HIGH); while ((millis() - time_to_go) < (10 * 1000)) { if (byte ret = BUTTON()) { if (ret == 2) { skipper = true; } else { count++; P_COUNT(count); digitalWrite(LED, LOW); time_to_led = millis(); analogWrite(BUZ, 100); buz_time = 10; } } if (skipper) break; if ((millis() - time_to_led) >= buz_time) { digitalWrite(LED, HIGH); analogWrite(BUZ, 0); } } digitalWrite(LED, LOW); counting = false; indention = true; noTone(BUZ); analogWrite(BUZ, 100); if (lcd_go) LCD_CLR(); DIALOG(tx_aft[skipper], 0, 0); delay(500); analogWrite(BUZ, 0); delay(500); if (!skipper) { byte sft = CONV(count); indention = !indention; DIALOG("record:", 0, 0); indention = !indention; DIALOG(tmp_val, 8 - sft, 1); delay(1000); char comment[4][9] = {"Awesome!", "Good!!! ", "WellDone", "Awful "}; byte rank = 3; if (count > score) { rank = 0; score = count; SCORE_WRITE(score); } else if (count >= (score - 60)) rank = 1; else if (count >= (score - 100)) rank = 2; DIALOG(comment[rank], 0, 0); MELODY(rank); delay(1000); } renew = true; } } // ------------- count val degree byte CONV(int val) { byte sft; for (sft = 1 ; sft <= tmp_num ; sft++) { if (val < pow(tmp_num, sft)) break; } sprintf(tmp_val, "%d", val); return sft; } // ------------- display in counting void P_COUNT(int val) { CONV(9 - (millis() - time_to_go) / 1000); LCD_CUR(7, 0); LCD_PRINT(tmp_val); byte deg = CONV(val); DIALOG(tmp_val, 8 - deg, 1); } // ------------- start screen bool STARTUP_SCRN(bool refresh) { if (print_go) { if ( refresh) { Serial.println(); Serial.println(); Serial.print("Best Score : "); Serial.println(score); Serial.print(tx_prs); Serial.println(" to start"); } } static bool flicker = true; static byte cue = 0; if (refresh) flicker = true; if (lcd_go) { if ((millis() - time_to_go) > 700) { LCD_CLR(); LCD_CUR(0, 0); if (cue <= 10) { LCD_PRINT(titl); } else { LCD_PRINT("BEST"); byte row = CONV(score); LCD_CUR(8 - row, 0); LCD_PRINT(tmp_val); } if (flicker) { LCD_CUR(4, 1); LCD_PRINT(tx_prs); } cue++; if (cue > 18) cue = 0; time_to_go = millis(); flicker = !flicker; } } return true; } // ------------- EEPROM READ boolean SCORE_READ() { byte cell[2] = {0, 0}; short val = 0; for (byte i = 0 ; i < 2 ; i++) { byte adr = SAVE_ADR + i; cell[i] = EEPROM.read(adr); } if (cell[1] >= B00001000) { val = 0; return false; } else { val = cell[1] << 8 | cell[0]; } score = val; return true; } // ------------- EEPROM WRITE void SCORE_WRITE(short save_score) { short val = (save_score != 0) ? save_score : DEF_SCR; for (byte i = 0 ; i < 2 ; i++) { byte cell = 1 - i; byte inv = val >> ( 8 * cell); EEPROM.write(SAVE_ADR + cell, inv); } score = val; } #define I2Cadr 0x3e #define INI_NUM 10 byte contrast = 35; const byte ini_code [INI_NUM] = {0x38, 0x39, 0x04, 0x14, 0x70, 0x5C, 0x6C, 0x38, 0x0C, 0x01}; // ------------- initialize lcd void LCD_INI() { Wire.begin(); for (byte i = 0 ; i < INI_NUM ; i++) { byte cmd = ini_code [i]; if (i == 4) cmd = cmd | (contrast & 0xF); if (i == 5) cmd = cmd | ((contrast >> 4) & 0x3); if (i == 7) delay(200); LCD_CMD(cmd); } delay(2); } // ------------- send command void LCD_CMD(byte cmd) { Wire.beginTransmission(I2Cadr); Wire.write(B00000000); Wire.write(cmd); Wire.endTransmission(); } // ------------- set lcd cursor position void LCD_CUR(byte x, byte y) { LCD_CMD(0x80 | (y * 0x40 + x)); } // ------------- lcd clear void LCD_CLR() { LCD_CMD(0x01); } /* // ------------- lcd_print void LCD_PRINT(int val) { LCD_PRINT(CONV(val)); } */ void LCD_PRINT(const char *txt) { Wire.beginTransmission(I2Cadr); while (*txt) { byte cmd = (*(txt + 1)) ? 0xC0 : 0x40; byte send_txt = *txt; Wire.write(cmd); Wire.write(send_txt); txt++; } Wire.endTransmission(); } const byte phrase[5][30][3] { // [bank],[size],[key, octave, dur] { //IFU-DO-DO {F, 3, 15}, {E, 3, 3}, {F, 3, 3}, {G, 3, 7}, {D, 3, 15}, {C, 3, 15}, {Bf, 2, 15}, {A, 2, 3}, {Bf, 2, 3}, {C, 3, 7}, {G, 2, 15} }, { // Promenade {A, 1, 7}, {G, 1, 7}, {C, 2, 7}, {D, 2, 3}, {G, 2, 3}, {E, 1, 7}, {D, 2, 3}, {G, 2, 3}, {E, 1, 7}, {C, 1, 7}, {D, 1, 7}, {A, 1, 7}, {G, 1, 15} }, { //Mozart {C, 3, 3}, {N, N, 1}, {G, 2, 1}, {C, 3, 3}, {N, N, 1}, {G, 2, 1}, {C, 3, 1}, {G, 2, 1}, {C, 3, 1}, {E, 3, 1}, {G, 3, 2}, {N, N, 3}, {F, 3, 3}, {N, N, 1}, {D, 2, 1}, {F, 3, 3}, {N, N, 1}, {D, 2, 1}, {F, 3, 1}, {D, 3, 1}, {B, 2, 1}, {D, 3, 1}, {G, 2, 3}, }, { //Beethoven {E, 1, 2}, {E, 1, 2}, {E, 1, 2}, {C, 1, 12}, {N, N, 4}, {D, 1, 2}, {D, 1, 2}, {D, 1, 2}, {B, 0, 12} }, { // Opening {D, 2, 3}, {E, 2, 3}, {F, 2, 3}, {G, 2, 15}, } }; int pitch[9] = {0, 220, 233, 247, 262, 294, 330, 349, 392}; boolean MELODY(byte rank) { #define tempo 3000 short sg = tempo / (3 * 4 * 4); byte size_bar; byte rest_def; boolean react = false; switch (rank) { case 0: //IFU-DO-DO size_bar = 11; rest_def = 1; break; case 1: // Promenade size_bar = 13; rest_def = 2; break; case 2: //Mozart size_bar = 23; rest_def = 1; break; case 3: //Beethoven size_bar = 9; rest_def = 2; break; case 4: //Opening size_bar = 4; rest_def = 1; break; } for (byte i = 0 ; i <= size_bar ; i++) { int key = pitch[phrase [rank][i][0]]; int dur = phrase [rank][i][2] * sg; if (key == N) noTone(BUZ); else { for (byte i = 0 ; i < phrase [rank][i][1] ; i++) key *= 2; tone(BUZ, key); } delay(dur); noTone(BUZ); if (!digitalRead(SW)) react = true; delay(rest_def * sg); /* Serial.print(key); Serial.print(":"); Serial.println(dur); */ } noTone(BUZ); return react; } |
お楽しみください。