jeu-boutons.ino

Arduino Esplora — Board Fiche · File : jeu-boutons.ino · Language : Arduino · Use case : Lire les quatre boutons en losange comme un pad directionnel

The four diamond buttons are read by the Esplora library as a D-pad — ten analog sensors share one channel through a multiplexer, and the library handles it. The base of every Esplora mini-game.

Hardware required

An Arduino Esplora (ATmega32U4, 5 V) with its onboard sensors: joystick, 4 buttons, slider, RGB LED, buzzer, light, temperature, sound and accelerometer. None.

Wiring

Nothing to wire.

How it works

readButton(SWITCH_x) returns PRESSED when the button's voltage divider is selected on the internal multiplexer (pin A4). In the Arduino IDE, select « Arduino Esplora »; add the « Esplora » library from the Library Manager to reach the onboard sensors.

Going further

Combine with the buzzer for a five-key piano, or time presses for a reflex game.

jeu-boutons.ino

// Les 4 boutons en losange + bouton du joystick, facon D-pad.
#include <Esplora.h>

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Esplora.readButton(SWITCH_UP) == PRESSED)    { Esplora.writeRGB(0, 64, 0);    Serial.println(F("Haut")); }
  if (Esplora.readButton(SWITCH_DOWN) == PRESSED)  { Esplora.writeRGB(64, 0, 0);    Serial.println(F("Bas")); }
  if (Esplora.readButton(SWITCH_LEFT) == PRESSED)  { Esplora.writeRGB(0, 0, 64);    Serial.println(F("Gauche")); }
  if (Esplora.readButton(SWITCH_RIGHT) == PRESSED) { Esplora.writeRGB(64, 64, 0);   Serial.println(F("Droite")); }
  if (Esplora.readButton(SWITCH_1) == PRESSED)     { Esplora.writeRGB(64, 0, 64);   Serial.println(F("Joystick")); }
  delay(100);
}

← Back to the board