回答例
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 |
#include <Adafruit_Microbit.h> Adafruit_Microbit_Matrix microbit; int button_A = 0; // ボタンAが押されたことを検知 int button_B = 0; // ボタンBが押されたことを検知 int my_position = 2; // グローバル変数(0〜4で変化する) void setup() { Serial.begin(9600); while (!Serial); // ボタンを初期化 pinMode(PIN_BUTTON_A, INPUT_PULLUP); pinMode(PIN_BUTTON_B, INPUT_PULLUP); // 割り込み設定 attachInterrupt(PIN_BUTTON_A, irq_by_A_button, FALLING); attachInterrupt(PIN_BUTTON_B, irq_by_B_button, FALLING); microbit.begin(); microbit.fillScreen(LED_OFF); } // 割り込みハンドラ void irq_by_A_button() { button_A = 1; } void irq_by_B_button() { button_B = 1; } void loop(){ int y = 4; if (button_A) { button_A = 0; // 3ms待ってボタンAが立ち下がってなければチャタリングと判断 delay(3); if (digitalRead(PIN_BUTTON_A) == HIGH) { Serial.println("CHATTERING!"); return; } my_position--; if (my_position<0) my_position=0; Serial.print("my_position="); Serial.println(my_position); } else if (button_B) { button_B = 0; // 3ms待ってボタンBが立ち下がってなければチャタリングと判断 delay(3); if (digitalRead(PIN_BUTTON_B) == HIGH) { Serial.println("CHATTERING!"); return; } my_position++; if (4<my_position) my_position=4; Serial.print("my_position="); Serial.println(my_position); } microbit.drawPixel(my_position, y, LED_ON); delay(100); microbit.drawPixel(my_position, y, LED_OFF); } |