Playing a melody on Arduino nano while moving car forward.

I'm trying to program my Arduino to play a melody using the tone function while making a small Bluetooth car move forward. The car moves, but every time I play the melody the car won't go or if the car moves it won't stop moving until the melody is over. I've tried multiple methods but nothing seems to work This is my first time using an Arduino and it's getting really frustrating.

Please post your code.

I guess that you are using delay() function. This is a blocking function. You SHOULD avoid using delay(). Use timestamp instead of delay(). You can refer to BlinkWithoutDelay example to see how to implement timestamp

#define MotorA1 6
#define MotorA2 7
#define LedA1 2
#define LedA2 12
#define LedB1 3
#define LedB2 4
#define horn 13

#define MotorB1 9
#define MotorB2 10
#include "pitches.h"

int melody[] = {
  NOTE_E7, NOTE_E7, 0, NOTE_E7,
  0, NOTE_C7, NOTE_E7, 0,
  NOTE_G7, 0, 0,  0,
  NOTE_G6, 0, 0, 0,
 
  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,
 
  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0,
 
  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,
 
  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0
};

int noteDurations[] = {
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
};

char command = 'S';
char prevCommand = 'A';
int velocity=0;

void setup() 
{       
  Serial.begin(9600);  //Set the baud rate to that of your Bluetooth module.
  

   pinMode(MotorA1 , OUTPUT);
   pinMode(MotorA2 , OUTPUT);
   pinMode(MotorB1 , OUTPUT);
   pinMode(MotorB2 , OUTPUT);
   pinMode (LedA1, OUTPUT);
  pinMode (LedA2, OUTPUT);
  pinMode (LedB1, OUTPUT);
  pinMode (LedB2, OUTPUT);
  pinMode (horn, OUTPUT);

}




void loop(){ 

  

  if(Serial.available() > 0){ 
 
    prevCommand = command;
    command = Serial.read(); 
    Serial.println(command);
    //Change pin mode only if new command is different from previous.   
    if(command!=prevCommand){
      //Serial.println(command);
      switch(command)
      {
      case 'F':  
      
   
        digitalWrite(MotorA1, HIGH);
        digitalWrite(MotorA2, LOW);
        digitalWrite(MotorB1, HIGH);
        digitalWrite(MotorB2, LOW);
   digitalWrite (LedA1, HIGH);
   digitalWrite (LedA2, LOW);
   digitalWrite (LedB1, HIGH);
   digitalWrite (LedB2, LOW); 
  
    
    
      break;
      case 'B': 
      
        digitalWrite(MotorA1, LOW);
        digitalWrite(MotorA2, HIGH);
        digitalWrite(MotorB1, LOW);
        digitalWrite(MotorB2, HIGH);
       digitalWrite (LedA1, LOW);
       digitalWrite (LedA2, HIGH);
       digitalWrite (LedB1, LOW);
      digitalWrite (LedB2, HIGH);        
        break;
      case 'L': 
      
        digitalWrite(MotorA1, HIGH);
        digitalWrite(MotorA2, LOW);
        digitalWrite(MotorB1, LOW);
        digitalWrite(MotorB2, LOW);
      digitalWrite (LedA1, HIGH);
      digitalWrite (LedA2, HIGH);
      digitalWrite (LedB1, LOW);
      digitalWrite (LedB2, LOW);        
        break;
      case 'R':
      
        digitalWrite(MotorA1, LOW);
        digitalWrite(MotorA2, LOW);
        digitalWrite(MotorB1, HIGH);
        digitalWrite(MotorB2, LOW);
       digitalWrite (LedA1, LOW);
      digitalWrite (LedA2, LOW);
      digitalWrite (LedB1, HIGH);
      digitalWrite (LedB2, HIGH);
        break;
      case 'S':  
        
        digitalWrite(MotorA1, LOW);
        digitalWrite(MotorA2, LOW);
        digitalWrite(MotorB1, LOW);
        digitalWrite(MotorB2, LOW);
        digitalWrite (LedA1, LOW);
       digitalWrite (LedA2, LOW);
       digitalWrite (LedB1, LOW);
       digitalWrite (LedB2, LOW);
        break;
      
    case 'V': 
        digitalWrite(horn, HIGH);
         for (int thisNote = 0; thisNote < 60; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(13, melody[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(13);
   }
       break; 
   
        case 'v':  
        digitalWrite(horn, LOW);
        break; 


      }
    }
  }
}

Here is the code.

IoT_hobbyist: Please post your code.

I guess that you are using delay() function. This is a blocking function. You SHOULD avoid using delay(). Use timestamp instead of delay(). You can refer to BlinkWithoutDelay example to see how to implement timestamp

I've tried timestamp, now the car moves with the sound but the sound won't turn off.