Button Test
ButtonTest.ino
starts the USB Serial Port, then inits the button pins as inputs with pullup. The state of each pin is printed every 100ms.
A button is pressed when digitalRead()
returns 0
.
Open the Arduino Serial Monitor to see the output after uploading the sketch.
#define BTN_UP 14#define BTN_DOWN 15#define BTN_LEFT 16#define BTN_RIGHT 17#define BTN_A 18#define BTN_B 21
void setup() { Serial.begin(115200); while(!Serial); Serial.println("STARTED");
pinMode(BTN_UP, INPUT_PULLUP); pinMode(BTN_DOWN, INPUT_PULLUP); pinMode(BTN_LEFT, INPUT_PULLUP); pinMode(BTN_RIGHT, INPUT_PULLUP); pinMode(BTN_A, INPUT_PULLUP); pinMode(BTN_B, INPUT_PULLUP);}
void loop() { Serial.printf("UP %d | DOWN %d | LEFT %d | RIGHT %d | A %d | B %d \n", digitalRead(BTN_UP), digitalRead(BTN_DOWN), digitalRead(BTN_LEFT), digitalRead(BTN_RIGHT), digitalRead(BTN_A), digitalRead(BTN_B) ); delay(100);}