[136] | 1 | /* Disco Bot
|
---|
| 2 |
|
---|
| 3 | This sketch shows you how to use the melody playing
|
---|
| 4 | feature of the robot, with some really cool 8-bit music.
|
---|
| 5 | Music will play when the robot is turned on, and it
|
---|
| 6 | will show you some dance moves.
|
---|
| 7 |
|
---|
| 8 | Circuit:
|
---|
| 9 | * Arduino Robot
|
---|
| 10 |
|
---|
| 11 | created 1 May 2013
|
---|
| 12 | by X. Yang
|
---|
| 13 | modified 12 May 2013
|
---|
| 14 | by D. Cuartielles
|
---|
| 15 |
|
---|
| 16 | This example is in the public domain
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 | #include <ArduinoRobot.h> // include the robot library
|
---|
| 20 | #include <Wire.h>
|
---|
| 21 | #include <SPI.h>
|
---|
| 22 |
|
---|
| 23 | /* Dancing steps:
|
---|
| 24 | S: stop
|
---|
| 25 | L: turn left
|
---|
| 26 | R: turn right
|
---|
| 27 | F: go forward
|
---|
| 28 | B: go backwards
|
---|
| 29 |
|
---|
| 30 | The number after each command determines how long
|
---|
| 31 | each step lasts. Each number is 1/2 second long.
|
---|
| 32 |
|
---|
| 33 | The "\0" indicates end of string
|
---|
| 34 | */
|
---|
| 35 | char danceScript[] = "S4L1R1S2F1B1S1\0";
|
---|
| 36 |
|
---|
| 37 | int currentScript = 0; // what step are we at
|
---|
| 38 |
|
---|
| 39 | int currentSong = 0; // keep track of the current song
|
---|
| 40 | static const int SONGS_COUNT = 3; // number of songs
|
---|
| 41 |
|
---|
| 42 | // an array to hold the songs
|
---|
| 43 | char musics[][11] = {
|
---|
| 44 | "melody.sqm",
|
---|
| 45 | "menu.sqm",
|
---|
| 46 | "chase.sqm",
|
---|
| 47 | };
|
---|
| 48 |
|
---|
| 49 | // variables for non-blocking delay
|
---|
| 50 | long waitFrom;
|
---|
| 51 | long waitTime = 0;
|
---|
| 52 |
|
---|
| 53 | void setup() {
|
---|
| 54 | // initialize the Robot, SD card, display, and speaker
|
---|
| 55 | Robot.begin();
|
---|
| 56 | Robot.beginSpeaker();
|
---|
| 57 | Robot.beginSD();
|
---|
| 58 | Robot.beginTFT();
|
---|
| 59 |
|
---|
| 60 | // draw "lg0.bmp" and "lg1.bmp" on the screen
|
---|
| 61 | Robot.displayLogos();
|
---|
| 62 |
|
---|
| 63 | // Print instructions to the screen
|
---|
| 64 | Robot.text("1. Use left and\n right key to switch\n song", 5, 5);
|
---|
| 65 | Robot.text("2. Put robot on the\n ground to dance", 5, 33);
|
---|
| 66 |
|
---|
| 67 | // wait for a few soconds
|
---|
| 68 | delay(3000);
|
---|
| 69 |
|
---|
| 70 | setInterface(); // display the current song
|
---|
| 71 | play(0); //play the first song in the array
|
---|
| 72 |
|
---|
| 73 | resetWait(); //Initialize non-blocking delay
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | void loop() {
|
---|
| 77 | // read the butttons on the robot
|
---|
| 78 | int key = Robot.keyboardRead();
|
---|
| 79 |
|
---|
| 80 | // Right/left buttons play next/previous song
|
---|
| 81 | switch (key) {
|
---|
| 82 | case BUTTON_UP:
|
---|
| 83 | case BUTTON_LEFT:
|
---|
| 84 | play(-1); //play previous song
|
---|
| 85 | break;
|
---|
| 86 | case BUTTON_DOWN:
|
---|
| 87 | case BUTTON_RIGHT:
|
---|
| 88 | play(1); //play next song
|
---|
| 89 | break;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | // dance!
|
---|
| 93 | runScript();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | // Dancing function
|
---|
| 97 | void runScript() {
|
---|
| 98 | if (!waiting()) { // if the previous instructions have finished
|
---|
| 99 | // get the next 2 commands (direction and duration)
|
---|
| 100 | parseCommand(danceScript[currentScript], danceScript[currentScript + 1]);
|
---|
| 101 | currentScript += 2;
|
---|
| 102 | if (danceScript[currentScript] == '\0') // at the end of the array
|
---|
| 103 | currentScript = 0; // start again at the beginning
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | // instead of delay, use this timer
|
---|
| 108 | boolean waiting() {
|
---|
| 109 | if (millis() - waitFrom >= waitTime)
|
---|
| 110 | return false;
|
---|
| 111 | else
|
---|
| 112 | return true;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | // how long to wait
|
---|
| 116 | void wait(long t) {
|
---|
| 117 | resetWait();
|
---|
| 118 | waitTime = t;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | // reset the timer
|
---|
| 122 | void resetWait() {
|
---|
| 123 | waitFrom = millis();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | // read the direction and dirstion of the steps
|
---|
| 127 | void parseCommand(char dir, char duration) {
|
---|
| 128 | //convert the scripts to action
|
---|
| 129 | switch (dir) {
|
---|
| 130 | case 'L':
|
---|
| 131 | Robot.motorsWrite(-255, 255);
|
---|
| 132 | break;
|
---|
| 133 | case 'R':
|
---|
| 134 | Robot.motorsWrite(255, -255);
|
---|
| 135 | break;
|
---|
| 136 | case 'F':
|
---|
| 137 | Robot.motorsWrite(255, 255);
|
---|
| 138 | break;
|
---|
| 139 | case 'B':
|
---|
| 140 | Robot.motorsWrite(-255, -255);
|
---|
| 141 | break;
|
---|
| 142 | case 'S':
|
---|
| 143 | Robot.motorsStop();
|
---|
| 144 | break;
|
---|
| 145 | }
|
---|
| 146 | //You can change "500" to change the pace of dancing
|
---|
| 147 | wait(500 * (duration - '0'));
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | // display the song
|
---|
| 151 | void setInterface() {
|
---|
| 152 | Robot.clearScreen();
|
---|
| 153 | Robot.stroke(0, 0, 0);
|
---|
| 154 | Robot.text(musics[0], 0, 0);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | // display the next song
|
---|
| 158 | void select(int seq, boolean onOff) {
|
---|
| 159 | if (onOff) { //select
|
---|
| 160 | Robot.stroke(0, 0, 0);
|
---|
| 161 | Robot.text(musics[seq], 0, 0);
|
---|
| 162 | } else { //deselect
|
---|
| 163 | Robot.stroke(255, 255, 255);
|
---|
| 164 | Robot.text(musics[seq], 0, 0);
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | // play the slected song
|
---|
| 169 | void play(int seq) {
|
---|
| 170 | select(currentSong, false);
|
---|
| 171 | if (currentSong <= 0 && seq == -1) { //previous of 1st song?
|
---|
| 172 | currentSong = SONGS_COUNT - 1; //go to last song
|
---|
| 173 | } else if (currentSong >= SONGS_COUNT - 1 && seq == 1) { //next of last?
|
---|
| 174 | currentSong = 0; //go to 1st song
|
---|
| 175 | } else {
|
---|
| 176 | currentSong += seq; //next song
|
---|
| 177 | }
|
---|
| 178 | Robot.stopPlayFile();
|
---|
| 179 | Robot.playFile(musics[currentSong]);
|
---|
| 180 | select(currentSong, true); //display the current song
|
---|
| 181 | }
|
---|