boite-musique.ino
The onboard buzzer (pin D6) answers Esplora.tone(): four buttons, four notes, and the slider picks the octave. The Esplora becomes a playable instrument with zero wiring.
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
Esplora.tone(frequency, duration) drives the internal piezo; the slider maps octave 1 to 4 by frequency multiplication. In the Arduino IDE, select « Arduino Esplora »; add the « Esplora » library from the Library Manager to reach the onboard sensors.
Going further
Record a melody (note array) and replay it with the joystick as a play head.
boite-musique.ino
// Boutons = notes, slider = octave : boite a musique embarquee.
#include <Esplora.h>
const int NOTES[] = {262, 330, 392, 494}; // do mi sol si
void setup() {}
void loop() {
int octave = map(Esplora.readSlider(), 0, 1023, 1, 4);
if (Esplora.readButton(SWITCH_UP) == PRESSED) Esplora.tone(NOTES[0] * octave, 150);
if (Esplora.readButton(SWITCH_RIGHT) == PRESSED) Esplora.tone(NOTES[1] * octave, 150);
if (Esplora.readButton(SWITCH_DOWN) == PRESSED) Esplora.tone(NOTES[2] * octave, 150);
if (Esplora.readButton(SWITCH_LEFT) == PRESSED) Esplora.tone(NOTES[3] * octave, 150);
delay(50);
}