mBot Melody Picker: Press the Button, Change the Tune
Every press of the button on the mBot's board makes the buzzer play the next melody. There are three short melodies; after the third it goes back to the first. No extra parts are needed. The melodies live in one table: one melody per row, eight beats each. Pressing the button while a melody plays changes nothing, because tone holds the code until the note is over.

Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// mBot Melody Picker: Press the Button, Change the Tune
4// ─────────────────────────────────────────
5// Every press of the button on the mBot's board makes the buzzer play the
6// next melody. There are three short melodies; after the third it goes
7// back to the first. Pressing the button while a melody plays changes
8// nothing, because the code waits until each note is over. No extra parts
9// are needed.
10//
11// Pins:
12// D8 -> Onboard buzzer
13// A7 -> Onboard button
14
15#include <MeMCore.h>
16
17const uint8_t PIN_BUTTON = A7; // Onboard button
18
19// Note frequencies (Hz): C, D, E, F, G, A.
20const uint16_t NOTE_C4 = 262;
21const uint16_t NOTE_D4 = 294;
22const uint16_t NOTE_E4 = 330;
23const uint16_t NOTE_F4 = 349;
24const uint16_t NOTE_G4 = 392;
25const uint16_t NOTE_A4 = 440;
26const uint16_t REST = 0; // Rest: no sound
27
28// ============ SETTINGS ============
29const unsigned long BEAT_MS = 400; // Length of one beat (ms)
30const unsigned long GAP_MS = 50; // Silence between two notes (ms)
31const int PRESS_LIMIT = 100; // Below this value means pressed
32const unsigned long DEBOUNCE_MS = 30; // Wait for button bounce (ms)
33
34// The melodies: one melody per row, eight beats each. REST means silence on that beat.
35const uint8_t MELODY_COUNT = 3;
36const uint8_t NOTE_COUNT = 8;
37const uint16_t MELODIES[MELODY_COUNT][NOTE_COUNT] = {
38 {NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, REST},
39 {NOTE_E4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4},
40 {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_C4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_C4},
41};
42
43MeBuzzer buzzer;
44uint8_t melodyIndex = 0; // The melody that plays next
45
46// Returns true while the button is down. The button pulls A7 to GND, so the reading is small while
47// pressed.
48bool isPressed() {
49 return analogRead(PIN_BUTTON) < PRESS_LIMIT;
50}
51
52// Plays the eight beats of the chosen melody in order. For a rest tone is not called, the code just
53// waits.
54void playMelody(uint8_t index) {
55 for (uint8_t i = 0; i < NOTE_COUNT; i++) {
56 uint16_t note = MELODIES[index][i];
57 if (note == REST) {
58 delay(BEAT_MS);
59 } else {
60 buzzer.tone(note, BEAT_MS - GAP_MS);
61 delay(GAP_MS);
62 }
63 }
64}
65
66void setup() {
67 // The buzzer and the button need no setup.
68}
69
70void loop() {
71 if (isPressed()) {
72 playMelody(melodyIndex);
73 melodyIndex = (melodyIndex + 1) % MELODY_COUNT;
74 // If the button is still down, wait until it is released; otherwise the next melody would start
75 // at once.
76 while (isPressed()) {
77 delay(DEBOUNCE_MS);
78 }
79 }
80 delay(DEBOUNCE_MS);
81}
82FAQ
I pressed the button while a melody was playing. Why did nothing happen?
The tone command that drives the buzzer waits until the note is over, and during that time the code cannot look at the button. One melody lasts 3.2 seconds; presses inside that time are not seen. Press again when the melody ends. If you hold the button through the melody, the program first waits for you to let go and then counts a new press.
How do I add a fourth melody?
Add a new row of eight values to the MELODIES table and set MELODY_COUNT to 4. Every row must have exactly eight values; if your melody is shorter, write REST on the remaining beats. Six notes from C to A are ready; if you need another one, define its frequency as a new constant.
Which board do I pick for the mBot in the Arduino IDE?
The mBot's board, the mCore, uses the same processor as the Arduino Uno (ATmega328P). Choose Arduino Uno under Tools > Board and the robot's port under Tools > Port. For the code to compile, the MakeBlock Drive Updated library must be installed from the Library Manager; the code pulls it in under the name MeMCore.h.
Does uploading this code erase the factory program on the robot?
Yes. The board holds one program at a time, so your code replaces the factory program and the ready-made modes, such as driving with the remote or line following, stop working. Bringing them back is easy: open mBlock, connect the mBot over USB and load the factory firmware again from the Firmware Update section.
