RobotDiscoBot

Turn your robot into an 8-bit jukebox and dance to the beat.

Disco bot

This sketch demonstrates how to play a melody with the robot, by playing back some sound files.

You can choose between three melodies specially composed for the Arduino Robot using the buttons on the control board. Once you get the music you like, put the robot on the floor and it will dance for you.

Hardware Required

  • Arduino Robot

  • Your best dance outfit

Instruction

  1. Upload the example, unplug USB and turn on power.

  2. Place the robot on the ground

  3. After the starting screen, the robot will be dancing with music playing and flashing colors on the screen

  4. You can press left/right button to switch music

  5. If you're not satisfied with the dancing moves, change the example to make it dance differently :

  6. Locate the line

    char danceScript[]="S4L1R1S2F1B1S1\0"

  7. S: stop, L: turn left, R: turn right, F: go forward, B: go backwards

  8. The number after each command decides how long it lasts. 1 = 500ms, 2=1000ms, 3=1500ms, and so forth.

Try it out

Choose a tune
Choose a tune

Code

1/* Disco Bot
2
3 This sketch shows you how to use the melody playing
4
5 feature of the robot, with some really cool 8-bit music.
6
7 Music will play when the robot is turned on, and it
8
9 will show you some dance moves.
10
11 Circuit:
12
13 * Arduino Robot
14
15 created 1 May 2013
16
17 by X. Yang
18
19 modified 12 May 2013
20
21 by D. Cuartielles
22
23 This example is in the public domain
24
25 */
26
27#include <ArduinoRobot.h> // include the robot library
28#include <Wire.h>
29
30/* Dancing steps:
31
32 S: stop
33
34 L: turn left
35
36 R: turn right
37
38 F: go forward
39
40 B: go backwards
41
42 The number after each command determines how long
43
44 each step lasts. Each number is 1/2 second long.
45
46 The "\0" indicates end of string
47
48*/
49char danceScript[] = "S4L1R1S2F1B1S1\0";
50
51int currentScript = 0; // what step are we at
52
53int currentSong = 0; // keep track of the current song
54static const int SONGS_COUNT = 3; // number of songs
55
56// an array to hold the songs
57char musics[][11] = {
58
59 "melody.sqm",
60
61 "menu.sqm",
62
63 "chase.sqm",
64};
65
66// variables for non-blocking delay
67long waitFrom;
68long waitTime = 0;
69
70void setup() {
71
72 // initialize the Robot, SD card, display, and speaker
73
74 Robot.begin();
75
76 Robot.beginSpeaker();
77
78 Robot.beginSD();
79
80 Robot.beginTFT();
81
82 // draw "lg0.bmp" and "lg1.bmp" on the screen
83
84 Robot.displayLogos();
85
86 // Print instructions to the screen
87
88 Robot.text("1. Use left and\n right key to switch\n song", 5, 5);
89
90 Robot.text("2. Put robot on the\n ground to dance", 5, 33);
91
92 // wait for a few soconds
93
94 delay(3000);
95
96 setInterface(); // display the current song
97
98 play(0); //play the first song in the array
99
100 resetWait(); //Initialize non-blocking delay
101}
102
103void loop() {
104
105 // read the buttons on the robot
106
107 int key = Robot.keyboardRead();
108
109 // Right/left buttons play next/previous song
110
111 switch (key) {
112
113 case BUTTON_UP:
114
115 case BUTTON_LEFT:
116
117 play(-1); //play previous song
118
119 break;
120
121 case BUTTON_DOWN:
122
123 case BUTTON_RIGHT:
124
125 play(1); //play next song
126
127 break;
128
129 }
130
131 // dance!
132
133 runScript();
134}
135
136// Dancing function
137void runScript() {
138
139 if (!waiting()) { // if the previous instructions have finished
140
141 // get the next 2 commands (direction and duration)
142
143 parseCommand(danceScript[currentScript], danceScript[currentScript + 1]);
144
145 currentScript += 2;
146
147 if (danceScript[currentScript] == '\0') { // at the end of the array
148
149 currentScript = 0; // start again at the beginning
150
151 }
152
153 }
154}
155
156// instead of delay, use this timer
157
158bool waiting() {
159
160 if (millis() - waitFrom >= waitTime) {
161
162 return false;
163
164 } else {
165
166 return true;
167
168 }
169}
170
171// how long to wait
172void wait(long t) {
173
174 resetWait();
175
176 waitTime = t;
177}
178
179// reset the timer
180void resetWait() {
181
182 waitFrom = millis();
183}
184
185// read the direction and dirstion of the steps
186void parseCommand(char dir, char duration) {
187
188 //convert the scripts to action
189
190 switch (dir) {
191
192 case 'L':
193
194 Robot.motorsWrite(-255, 255);
195
196 break;
197
198 case 'R':
199
200 Robot.motorsWrite(255, -255);
201
202 break;
203
204 case 'F':
205
206 Robot.motorsWrite(255, 255);
207
208 break;
209
210 case 'B':
211
212 Robot.motorsWrite(-255, -255);
213
214 break;
215
216 case 'S':
217
218 Robot.motorsStop();
219
220 break;
221
222 }
223
224 //You can change "500" to change the pace of dancing
225
226 wait(500 * (duration - '0'));
227}
228
229// display the song
230void setInterface() {
231
232 Robot.clearScreen();
233
234 Robot.stroke(0, 0, 0);
235
236 Robot.text(musics[0], 0, 0);
237}
238
239// display the next song
240void select(int seq, bool onOff) {
241
242 if (onOff) { //select
243
244 Robot.stroke(0, 0, 0);
245
246 Robot.text(musics[seq], 0, 0);
247
248 } else { //deselect
249
250 Robot.stroke(255, 255, 255);
251
252 Robot.text(musics[seq], 0, 0);
253
254 }
255}
256
257// play the selected song
258void play(int seq) {
259
260 select(currentSong, false);
261
262 if (currentSong <= 0 && seq == -1) { //previous of 1st song?
263
264 currentSong = SONGS_COUNT - 1; //go to last song
265
266 } else if (currentSong >= SONGS_COUNT - 1 && seq == 1) { //next of last?
267
268 currentSong = 0; //go to 1st song
269
270 } else {
271
272 currentSong += seq; //next song
273
274 }
275
276 Robot.stopPlayFile();
277
278 Robot.playFile(musics[currentSong]);
279
280 select(currentSong, true); //display the current song
281}

Explore further

The melodies are stored on the SD card. They are created with a tracker program.

To write your own music, use a tracker program as explained in the Squawk library reference.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.