First Melody with mBot: Play Notes on the Buzzer
The buzzer on the mBot's board plays the first two lines of a well-known children's song (Twinkle Twinkle Little Star), waits 2 seconds and starts over. No extra parts are needed. Every note is a frequency (Hz) and a length; the melody lives in two lists, one with the notes and one with the beat counts. The tone command plays a note and waits until it is over.

Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// First Melody with mBot: Play Notes on the Buzzer
4// ─────────────────────────────────────────
5// The buzzer on the mBot's board plays the first two lines of "Twinkle
6// Twinkle Little Star", waits 2 seconds and starts over. Every note is a
7// frequency and a length; the melody lives in two lists. No extra parts
8// are needed.
9//
10// Pins:
11// D8 -> Onboard buzzer
12
13#include <MeMCore.h>
14
15// Note frequencies (Hz): C, D, E, F, G, A.
16const uint16_t NOTE_C4 = 262;
17const uint16_t NOTE_D4 = 294;
18const uint16_t NOTE_E4 = 330;
19const uint16_t NOTE_F4 = 349;
20const uint16_t NOTE_G4 = 392;
21const uint16_t NOTE_A4 = 440;
22
23// ============ SETTINGS ============
24const unsigned long BEAT_MS = 400; // Length of one beat (ms)
25const unsigned long GAP_MS = 50; // Silence between two notes (ms)
26const unsigned long REPEAT_MS = 2000; // Wait after the melody (ms)
27
28// The melody: the frequency of every note and how many beats it lasts. Both lists must have the
29// same number of items.
30const uint16_t MELODY[] = {NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4,
31 NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4};
32const uint8_t BEATS[] = {1, 1, 1, 1, 1, 1, 2,
33 1, 1, 1, 1, 1, 1, 2};
34const uint8_t NOTE_COUNT = sizeof(MELODY) / sizeof(MELODY[0]);
35
36MeBuzzer buzzer;
37
38void setup() {
39 // The buzzer needs no setup; the library prepares the pin itself.
40}
41
42void loop() {
43 for (uint8_t i = 0; i < NOTE_COUNT; i++) {
44 // tone waits until the note is over. The short silence after it keeps the notes apart.
45 buzzer.tone(MELODY[i], BEATS[i] * BEAT_MS - GAP_MS);
46 delay(GAP_MS);
47 }
48 delay(REPEAT_MS);
49}
50FAQ
How do I write my own melody?
Put the notes in the MELODY list and, in the same order, how many beats each one lasts in the BEATS list; both lists must have the same number of items. The code defines six notes, from C to A (NOTE_C4 to NOTE_A4). If you need another note, add its frequency as a new constant: B is 494 Hz and high C is 523 Hz.
How do I make the melody faster?
BEAT_MS is the length of one beat; 400 ms means 150 beats a minute. Set it to 300 and the melody speeds up, set it to 600 and it slows down. GAP_MS is the silence between notes and must stay smaller than BEAT_MS; when the same note comes twice in a row it keeps the two apart.
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.
