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