Go Down

Topic: can anyone reduce the size of my code (Read 380 times) previous topic - next topic

Anvay

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.
Code: [Select]

#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="";}}








PaulS

Code: [Select]
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.
Code: [Select]
   char randomFile[13] = "";
   sprintf(randomFile, "voice%d.wav", randomNumber);


Save 165 bytes of SRAM.

Code: [Select]
  Serial.println("SD fail");
Use the F() macro consistently.

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

Code: [Select]
   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.
The art of getting good answers lies in asking good questions.

Anvay

Code: [Select]

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


 can you please add this code in the original code.

PaulS

Quote
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);
The art of getting good answers lies in asking good questions.

Anvay

Code: [Select]

 Serial.println(voice);


can i use the f() macro here?

UKHeliBob

Quote
can i use the f() macro here?
You could have tried it faster than posting the question here.

The answer is no.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Anvay

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.

Anvay

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?

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

Anvay

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

dougp

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

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.

aarg

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.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

PaulMurrayCbr

We need a page on this forum specifically about String and C strings.
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

PaulS

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.
The art of getting good answers lies in asking good questions.

Go Up