Sound and movement at the same time?

Hi,

I have a final project where we built robots with Arduinos. It’s hooked up with wheels and it can move back and forth. I have a TR-057F-R speaker and have coded a melody and want to make my robot dance to it. However, it only does one thing at a time; the wheels move and then the sound plays, or the sound plays and then the wheels move. Is it possible to have it work together?

This is my code right now.

#include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_CS5, NOTE_AS4, NOTE_AS4, NOTE_CS5, NOTE_AS4, NOTE_AS4, NOTE_AS4, 0,
  NOTE_CS5, NOTE_AS4, NOTE_AS4, NOTE_CS5, NOTE_AS4, NOTE_AS4, NOTE_AS4, 0,
  NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, 0,
  NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_CS5, 
  
  NOTE_CS5, NOTE_AS4, NOTE_CS5, NOTE_C5, NOTE_AS4, NOTE_GS4, NOTE_F4, NOTE_FS4, NOTE_GS4, NOTE_CS5, 0, NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_C5, NOTE_CS5, NOTE_FS5, NOTE_F5, 
  NOTE_CS5, NOTE_AS4, NOTE_CS5, NOTE_DS5, NOTE_CS5, NOTE_DS5, NOTE_F5, NOTE_DS5, NOTE_CS5, NOTE_CS5, 0, NOTE_GS4, NOTE_AS4, NOTE_F5, NOTE_DS5, NOTE_F5, NOTE_DS5, NOTE_CS5, 0,
  NOTE_CS5, NOTE_AS4, NOTE_CS5, NOTE_C5, NOTE_AS4, NOTE_GS4, NOTE_F4, NOTE_FS4, NOTE_GS4, NOTE_CS5, 0, NOTE_AS4, NOTE_AS4, NOTE_AS4, NOTE_C5, NOTE_CS5, NOTE_FS5, NOTE_F5,
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  
  4, 4, 4, 4, 4, 4, 4, 4,
  4, 4, 4, 4, 4, 4, 4, 4,
  4, 4, 4, 4, 4, 4, 4, 4,
  4, 8, 8, 4, 4, 4, 1,
  
  1, 4, 4, 1, 4, 4, 4, 8, 8, 1, 4, 4, 4, 4, 8, 8, 1, 1, 
  1, 4, 4, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 2,
  1, 4, 4, 1, 4, 4, 4, 8, 8, 1, 4, 4, 4, 4, 8, 8, 1, 1,
};

#include <Servo.h>

// motor variables
// digital outs

int blue = 5;
int green = 4;
int red = 9;
int yellow = 8;

int leftSpeedPin =   6;
int rightSpeedPin = 11;

 
// variables
int currentLeftSpeed=0;
int currentRightSpeed=0;

// Analog Input Pins
int leftEye = A3;
int rightEye = A5;

int leftEyeValue, rightEyeValue;

int ledPin = 2;
int buttonPin = 3;

Servo myservo; 
int servoPin = 13;
int pos = 0;

int DOWN = 90;
int UP = 180;


void setup() {

  
  // put your setup code here, to run once:
    myservo.attach(servoPin);  t
    servoMove(UP);
    
    Serial.begin(9600);
    Serial.println("_12_CHECK-UP\n");
    
    //direction controls
    pinMode (blue,   OUTPUT);
    pinMode (green,  OUTPUT);
    pinMode (red,    OUTPUT);
    pinMode (yellow, OUTPUT);

    // speed controls
    pinMode(leftSpeedPin, OUTPUT);
    pinMode(rightSpeedPin, OUTPUT);
    // HCI
    pinMode(buttonPin, INPUT);
    pinMode(ledPin,    OUTPUT);

    readEyes();
    printEyes();
    
    waitForButtonPress(); 
      
    stop(); 
}

void loop() {

     for (int thisNote = 0; thisNote < 86; thisNote++) {
      
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(7, melody[thisNote], noteDuration);

    int pauseBetweenNotes = noteDuration * 1.20;
    delay(pauseBetweenNotes);
   
    noTone(86);
     } 
     
    setSpeed(127,127);
      forward();
      delay(1500);
      
      stop();  
      servoChop();
      delay(1000);
      
      backward(255,255);
      delay(1000);
      
      stop(1000);
      
      forward(255,255,1000); 
      
      stop(-1);  // ends
  
   



  

}
      delay(1500);

The delay() function would be named better if it was called blockYourEarsAndYourEyesForALongTime()

Open up the blink-without-delay example in your copy of Arduino. Study it well.