can anyone reduce the size of my code

hi. while uploading the code to my project, it says that global variables use 80 % of the dynamic memory.low memory available . stability problems may occur.
and yes , my project does not work properly.
can anyone reduce the size of my code please. i also have a sd card attached to arduino. if there is any solution , please help.

#include <ServoTimer2.h>
ServoTimer2 myservo;

#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm music;
String voice;
const int IN1 = 7;
const int IN2 = 4;
const int IN3 = 8;
const int IN4 = 2;


const int ENA = 5;
const int ENB = 6;

char *randomSounds[] = {"voice1.wav","voice2.wav", "voice3.wav", "voice4.wav", "voice5.wav", "voice6.wav", "voice7.wav", "voice8.wav", "voice9.wav", "voice10.wav", "voice11.wav", "voice12.wav", "voice13.wav", "voice14.wav", "voice15.wav", "voice16.wav", };
void setup() {

  myservo.attach(3);
music.speakerPin=9;
Serial.begin(38400);
if(!SD.begin(SD_ChipSelectPin))
{
  Serial.println("SD fail");
  return;
}
music.setVolume(5);

pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  pinMode (IN3, OUTPUT);
  pinMode (IN4, OUTPUT);
  pinMode (ENA, OUTPUT);
  pinMode (ENB, OUTPUT);
 

   const int ENA = 5;
const int ENB = 6;

}
//-----------------------------------------------------------------------//  
void loop() {
  while (Serial.available()){  //Check if there is an available byte to read
  delay(10); //Delay added to make thing stable 
  char c = Serial.read(); //Conduct a serial read
  if (c == '#') {break;} //Exit the loop when the # is detected after the word
  voice += c; //Shorthand for voice = voice + c
  }  

// if not processing a command and no sound is playing
  if( (voice.length() == 0) && (!music.isPlaying()) )
  {

    // start random sound
    music.play(randomSounds[random(sizeof(randomSounds) / sizeof(randomSounds[0]))]);
delay(7000);
  }



    
   analogWrite(ENA, 150);
  analogWrite(ENB, 150); 
  
  if (voice.length() > 0) {
    Serial.println(voice);

  if(voice == "play faded") 
  {
    music.play("faded.wav");
  } 
  
  if(voice == "play hello") 
  {
    music.play("hello.wav");
  } 
  
  if(voice == "play alone") 
  {
    music.play("alone.wav");
  } 
   if(voice == "what is your name") 
  {
    music.play("name.wav");
  } 
   if(voice == "who created you") 
  {
    music.play("creator.wav");
  } 
   if(voice == "which is his class") 
  {
    music.play("class.wav");
  } 
   if(voice == "who are you") 
  {
    music.play("robot.wav");
  } 
   if(voice == "play midnight") 
  {
    music.play("midnight.wav");
  }  
  if(voice == "play fat rat") 
  {
    music.play("fatrat.wav");
  } 
  if(voice == "play animals") 
  {
    music.play("animals.wav");
  } 

 if(voice == "voice") 
  {
    music.play("voice1.wav");
  } 
   if(voice == "stop music") 
  {
    music.stopPlayback();
  } 
if(voice == "pause") 
  {
    music.pause();
  } 
  if(voice == "resume") 
  {
    music.pause();
  } 
  if(voice == "45"){myservo.write(1250);
delay(100);}
  if(voice == "90"){myservo.write(1500);
  delay(100);}

   if(voice == "0"){myservo.write(1000);
  delay(100);}
   if(voice == "180"){myservo.write(2000);
 delay(100);}
  if(voice == "down") 
  {
     digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  } 

   if(voice == "front") 
  {
    digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  } 

   if(voice == "stop") 
  {
    digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  } 

    if(voice == "front1") 
  {
    digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  } 
  if(voice == "down1") 
  {
    digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  } 
  if(voice == "left1") 
  {
    digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  } 
  if(voice == "right1") 
  {
    digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  } 
  if(voice == "right") 
  {
    digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  } 
  if(voice == "left") 
  {
    digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  } 
voice="";}}
char *randomSounds[] = {"voice1.wav","voice2.wav", "voice3.wav", "voice4.wav", "voice5.wav", "voice6.wav", "voice7.wav", "voice8.wav", "voice9.wav", "voice10.wav", "voice11.wav", "voice12.wav", "voice13.wav", "voice14.wav", "voice15.wav", "voice16.wav", };

There is no reason to replicate "voice" and ".wav" in all these names. Use sprintf() to create the name based on the random number.

   char randomFile[13] = "";
   sprintf(randomFile, "voice%d.wav", randomNumber);

Save 165 bytes of SRAM.

  Serial.println("SD fail");

Use the F() macro consistently.

Serial.println(F("SD fail"));

   const int ENA = 5;
const int ENB = 6;

}

What is the point of local variables that immediately go out of scope?

Ditch the String class. Learn to use strings - NULL terminated arrays of chars. You've put off learning to use strings for too long.

char randomFile[16] = "";
   sprintf(randomFile, "voice%d.wav", randomNumber);

can you please add this code in the original code.

can you please add this code in the original code.

~~ // start random sound~~
~~ music.play(randomSounds[random(sizeof(randomSounds) / sizeof(randomSounds[0]))]);~~

byte randomNumber = random(0, 17);
char randomFile[16] = “”;
sprintf(randomFile, “voice%d.wav”, randomNumber);
music.play(randomFile);

 Serial.println(voice);

can i use the f() macro here?

can i use the f() macro here?

You could have tried it faster than posting the question here.

The answer is no.

after all the changes , the dynamic memory came to 70%. However now there is a lag in between in receiving of text from the mobile bluetooth. that is after i finish giving command , it takes time to respond through speakers.however, previously efore adding the randomfile code everything was perfect.
i think the randomfile code is causing delay in receiving of command.

i found the reason . the delay of 7 seconds in between random sounds is also causing delay in the receiving of command. when i removed the delay code , the commands were received without any delay.
so how do i add delay in between the random sounds without causing delay in the receiving of command?

so how do i add delay in between the random sounds without causing delay in the receiving of command?

You can't. What you can do is look at the blink without delay example. On every pass through loop(), compare the current time to the last time you did whatever needs doing. If it has been long enough, do whatever needs doing and reset the time that you last did whatever needs doing. currentMillis and previousMillis are crappy names. Do NOT use them.

thanks for helping so much. however i couldnt understand what you said above

Anvay:
thanks for helping so much. however i couldnt understand what you said above

Translation

I also found 1 easy solution. That is adding a 5sec blank time at the start of each sound.that will act as delay only.

Anvay:
I also found 1 easy solution. That is adding a 5sec blank time at the start of each sound.that will act as delay only.

Someday you will want to have multiple timed events in your program without bringing something to a screeching dead halt.

We need a page on this forum specifically about String and C strings.

PaulMurrayCbr:
We need a page on this forum specifically about String and C strings.

And one about serial input/output.
And one about delay() vs. millis().
And one about how to (mis)use interrupts.

But, why? No one would post there.

PaulS:
But, why? No one would post there.

I agree. The need is not so much having more information available it is making people read it.

I am in the process of putting together a “millis() for beginners” thread but the people that I shared an early draft with have queried how I would get people to read it, to which I have no answer.

I am in the process of putting together a "millis() for beginners" thread but the people that I shared an early draft with have queried how I would get people to read it, to which I have no answer.

People who answer questions here could bookmark it, like Robin2's Serial Input Basics thread, and then refer people that can't understand the simple concept to it.

PaulS:
People who answer questions here could bookmark it, like Robin2’s Serial Input Basics thread, and then refer people that can’t understand the simple concept to it.

That was my thought. Just as the routine answer for “I want to do this other thing while my sketch is in a delay” is “read the many-things-at-the-same-time post”, people who post here regularly would have a resource to point people at when they put String in their sketches.

OK. I may polish up my tutorial thread and post it.

UKHeliBob:
I am in the process of putting together a “millis() for beginners” thread

After my quick and dirty reply #5 here, I was thinking of doing something similar. (In fact your reply #4 there may well be what prompted you to write yours.)

UKHeliBob:
OK. I may polish up my tutorial thread and post it.

Do that: it will be very useful.