Trying to Make Zumo Robot Play Music and Dance. Need Help!

I have been trying to make a Zumo Robot play music in the background and dance at the same time, but when it runs through the loop the code plays only one note then it runs the motors and then plays the next note runs the motors and then plays the next note…etc…

I have the music in a for loop but it seems not to be working. Any help would be appreciated. Thanks!
the code is pasted beneath:

[code]#include <ZumoBuzzer.h>
#include <Pushbutton.h>
#include <ZumoMotors.h>

/*

  • This example uses the ZumoBuzzer library to play a series of notes on
  • the buzzer. It also uses Pushbutton library to allow the user to
  • stop/reset the melody with the user pushbutton.
    */

#define MELODY_LENGTH 95
#define LED_PIN 13

// These arrays take up a total of 285 bytes of RAM (out of a limit of 1k (ATmega168), 2k (ATmega328), or 2.5k(ATmega32U4))
unsigned char note[MELODY_LENGTH] =
{
NOTE_E(5), SILENT_NOTE, NOTE_E(5), SILENT_NOTE, NOTE_E(5), SILENT_NOTE, NOTE_C(5), NOTE_E(5),
NOTE_G(5), SILENT_NOTE, NOTE_G(4), SILENT_NOTE,

NOTE_C(5), NOTE_G(4), SILENT_NOTE, NOTE_E(4), NOTE_A(4), NOTE_B(4), NOTE_B_FLAT(4), NOTE_A(4), NOTE_G(4),
NOTE_E(5), NOTE_G(5), NOTE_A(5), NOTE_F(5), NOTE_G(5), SILENT_NOTE, NOTE_E(5), NOTE_C(5), NOTE_D(5), NOTE_B(4),

NOTE_C(5), NOTE_G(4), SILENT_NOTE, NOTE_E(4), NOTE_A(4), NOTE_B(4), NOTE_B_FLAT(4), NOTE_A(4), NOTE_G(4),
NOTE_E(5), NOTE_G(5), NOTE_A(5), NOTE_F(5), NOTE_G(5), SILENT_NOTE, NOTE_E(5), NOTE_C(5), NOTE_D(5), NOTE_B(4),

SILENT_NOTE, NOTE_G(5), NOTE_F_SHARP(5), NOTE_F(5), NOTE_D_SHARP(5), NOTE_E(5), SILENT_NOTE,
NOTE_G_SHARP(4), NOTE_A(4), NOTE_C(5), SILENT_NOTE, NOTE_A(4), NOTE_C(5), NOTE_D(5),

SILENT_NOTE, NOTE_G(5), NOTE_F_SHARP(5), NOTE_F(5), NOTE_D_SHARP(5), NOTE_E(5), SILENT_NOTE,
NOTE_C(6), SILENT_NOTE, NOTE_C(6), SILENT_NOTE, NOTE_C(6),

SILENT_NOTE, NOTE_G(5), NOTE_F_SHARP(5), NOTE_F(5), NOTE_D_SHARP(5), NOTE_E(5), SILENT_NOTE,
NOTE_G_SHARP(4), NOTE_A(4), NOTE_C(5), SILENT_NOTE, NOTE_A(4), NOTE_C(5), NOTE_D(5),

SILENT_NOTE, NOTE_E_FLAT(5), SILENT_NOTE, NOTE_D(5), NOTE_C(5)
};

unsigned int duration[MELODY_LENGTH] =
{
100, 25, 125, 125, 125, 125, 125, 250, 250, 250, 250, 250,

375, 125, 250, 375, 250, 250, 125, 250, 167, 167, 167, 250, 125, 125,
125, 250, 125, 125, 375,

375, 125, 250, 375, 250, 250, 125, 250, 167, 167, 167, 250, 125, 125,
125, 250, 125, 125, 375,

250, 125, 125, 125, 250, 125, 125, 125, 125, 125, 125, 125, 125, 125,

250, 125, 125, 125, 250, 125, 125, 200, 50, 100, 25, 500,

250, 125, 125, 125, 250, 125, 125, 125, 125, 125, 125, 125, 125, 125,

250, 250, 125, 375, 500
};

ZumoBuzzer buzzer;
ZumoMotors motors;
Pushbutton button(ZUMO_BUTTON);
unsigned char currentIdx;

void setup()
{
pinMode(LED_PIN, OUTPUT);
currentIdx = 0;
button.waitForButton();
}

void loop()

{

for(currentIdx; currentIdx < MELODY_LENGTH && !buzzer.isPlaying(); currentIdx++)
{
buzzer.playNote(note[currentIdx], duration[currentIdx], 15);
motors.setSpeeds(0,0);
}

digitalWrite(LED_PIN, HIGH);
motors.setSpeeds(400,-400);
delay(400);
motors.setSpeeds(0,0);

}[/code]

Hello.

In your code, calling delay() stops everything else in the program while the motors continue to run. Removing the delay from your code will likely fix your problem. However, if you want to move the motors for some amount of time, you should implement it using non-blocking functions. An example of this can be found in the Blink Without Delay Arduino Example.

- Jeremy