Good afternoon friends, I am working on a project where I have 80 buttons that trigger sound clips that last less than 3 seconds. Overall the project works fine but once I start pressing multiple buttons (more than 20) sequentially i get an error message “ran out of ram memory” - then no sound plays. How can I free up the ram? We tried freeRAM() and it didn’t work.
Below is some of our code - maybe we are doing something wrong in our end?
#include <Wire.h>
#include <MusicPlayer.h>
String data = “”;
char datas;
int song = 0;
int songs[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; //Matrix for LEDs to be lit. Max 40 at the same time.
bool new_song = false;
bool end_song = true;
int i = 0;
bool da = true;
char song_name = “Da.wav”;
bool request = true;
bool check = true;
void receiveEvent(int howMany) {
data = “”;
//Serial.println(“Receiving”);
while(Wire.available())
{
datas = Wire.read();
data = data+datas;
}
song = data.toInt();
if(song>0) {
song = song-1;
add_song();
}
else {
send_all();
}
}
void send_all() {
if(songs[i]==-1){
data = data+“00”;
}
else if (songs[i]<10){
data = data+String(0);
}
if(songs[i]>-1){
data = data+String(songs[i]);
}
data.toCharArray(datas,data.length());
Wire.write(datas);
}
void addToPlayList(int ID){
switch (ID) {
case 0:
player.playOne(“a.wav”);
return;
case 1:
player.playOne(“Da.wav”);
return;
case 2:
player.playOne(“a.wav”);
return;
case 3:
player.playOne(“Du.wav”);
return;
case 4:
player.playOne(“Do.wav”);
return;
case 5:
player.playOne(“a.wav”);
return;
case 6:
player.playOne(“Da.wav”);
return;
case 7:
player.playOne(“Da.wav”);
return;
case 8:
player.playOne(“Da.wav”);
return;
case 9:
player.playOne(“Da.wav”);
return;
case 10:
player.playOne(“Da.wav”);
return;
case 11:
player.playOne(“Da.wav”);
return;
case 12:
player.playOne(“Da.wav”);
return;
case 13:
player.playOne(“Da.wav”);
return;
case 14:
player.playOne(“Da.wav”);
return;
case 15:
player.playOne(“Da.wav”);
return;
case 16:
player.playOne(“Da.wav”);
return;
case 17:
player.playOne(“Da.wav”);
return;
case 18:
player.playOne(“Da.wav”);
return;
case 19:
player.playOne(“Da.wav”);
return;
case 20:
player.playOne(“Da.wav”);
return;
case 21:
player.playOne(“Da.wav”);
return;
case 22:
player.playOne(“Da.wav”);
return;
case 23:
player.playOne(“Da.wav”);
return;
case 24:
player.playOne(“Da.wav”);
return;
case 25:
player.playOne(“Da.wav”);
return;
case 26:
player.playOne(“Da.wav”);
return;
case 27:
player.playOne(“Da.wav”);
return;
case 28:
player.playOne(“Da.wav”);
return;
case 29:
player.playOne(“Dv.wav”);
return;
case 30:
player.playOne(“Dv.wav”);
return;
case 31:
player.playOne(“Dv.wav”);
return;
case 32:
player.playOne(“Dv.wav”);
return;
case 33:
player.playOne(“Dv.wav”);
return;
case 34:
player.playOne(“Dv.wav”);
return;
case 35:
player.playOne(“Dv.wav”);
return;
case 36:
player.playOne(“Dv.wav”);
return;
case 37:
player.playOne(“Dv.wav”);
return;
case 38:
player.playOne(“Dv.wav”);
return;
case 39:
player.playOne(“Dv.wav”);
return;
case 40:
player.playOne(“Dv.wav”);
return;
case 41:
player.playOne(“Dv.wav”);
return;
case 42:
player.playOne(“Dv.wav”);
return;
case 43:
player.playOne(“Dv.wav”);
return;
case 44:
player.playOne(“Dv.wav”);
return;
case 45:
player.playOne(“Dv.wav”);
return;
case 46:
player.playOne(“Dv.wav”);
return;
case 47:
player.playOne(“Dv.wav”);
return;
case 48:
player.playOne(“Dv.wav”);
return;
case 49:
player.playOne(“Dv.wav”);
return;
case 50:
player.playOne(“Dv.wav”);
return;
case 51:
player.playOne(“Dv.wav”);
return;
case 52:
player.playOne(“Dv.wav”);
return;
case 53:
player.playOne(“Dv.wav”);
return;
case 54:
player.playOne(“Dv.wav”);
return;
case 55:
player.playOne(“Dv.wav”);
return;
case 56:
player.playOne(“Dv.wav”);
return;
case 57:
player.playOne(“Dv.wav”);
return;
case 58:
player.playOne(“Dv.wav”);
return;
case 59:
player.playOne(“Dv.wav”);
return;
case 60:
player.playOne(“Do.wav”);
return;
case 61:
player.playOne(“Do.wav”);
return;
case 62:
player.playOne(“Do.wav”);
return;
case 63:
player.playOne(“Do.wav”);
return;
case 64:
player.playOne(“Do.wav”);
return;
case 65:
player.playOne(“Do.wav”);
return;
case 66:
player.playOne(“Do.wav”);
return;
case 67:
player.playOne(“Do.wav”);
return;
case 68:
player.playOne(“Do.wav”);
return;
case 69:
player.playOne(“Do.wav”);
return;
case 70:
player.playOne(“Do.wav”);
return;
case 71:
player.playOne(“Do.wav”);
return;
case 72:
player.playOne(“Do.wav”);
return;
case 73:
player.playOne(“Do.wav”);
return;
case 74:
player.playOne(“Do.wav”);
return;
case 75:
player.playOne(“Do.wav”);
return;
case 76:
player.playOne(“Do.wav”);
return;
case 77:
player.playOne(“Do.wav”);
return;
case 78:
player.playOne(“Do.wav”);
return;
case 79:
player.playOne(“Do.wav”);
return;
case 80:
player.playOne(“Do.wav”);
return;
case 81:
player.playOne(“Do.wav”);
return;
case 82:
player.playOne(“Do.wav”);
return;
case 83:
player.playOne(“Do.wav”);
return;
case 84:
player.playOne(“Do.wav”);
return;
case 85:
player.playOne(“Do.wav”);
return;
case 86:
player.playOne(“Do.wav”);
return;
case 87:
player.playOne(“Do.wav”);
return;
case 88:
player.playOne(“Do.wav”);
return;
case 89:
player.playOne(“Do.wav”);
return;
}
}
void add_song() {
for (i=0;i<40;i++) {
if(songs[i]==-1){
songs[i]=song;
/Serial.println(“Song to add to play list”);
Serial.println(song);
Serial.println(i);
Serial.println(songs[i]);/
return;
}
}
}
void send_song() {
if (request == false) {
request = true;
data = "";
i = songs[0]+1;
if (i<10){
data = "0";
}
data = data+String(i);
Wire.write(data.c_str());
}
}
void send_zero() {
data = “0”;
Wire.write(“00”);
//Serial.println(“Zero sent!”);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin(8);
Wire.onReceive(receiveEvent);
player.begin();
player.setPlayMode(PM_REPEAT_LIST);
player.playlistInit();
player.play();
}
void loop() {
// put your main code here, to run repeatedly:
player.play();
if ((check == false) & (player.send_end_song()==true)){
check = true;
request = false;
end_song = true;
}
if((player.send_end_song()==true) & (request==true)) {
if (songs[0]>=0) {
addToPlayList(songs[0]);
end_song = false;
}
}
if((player.send_end_song()==true) & (songs[0]>=0) & (end_song==false)){
check = false;
check = false;
end_song = true;
}
if (request == true) {
Wire.onRequest(send_zero);
delay(50);
}
else {
Wire.onRequest(send_song);
delay(500);
end_song = true;
for (i=1;i<40;i++) {
if(songs[i]!=-1){
songs[i-1]=songs[i];
}
else {
songs[i-1]=songs[i];
if (songs[0]==-1){
player.playlistInit(); ///???
}
break;
}
}
}
}