Hey guys,
My program is sometimes getting stuck, as it uses way too much of the dynamic memory of my Arduino Uno. Sadly I don't know how to reduce that, I hope you can give me some tips or push me in the right direction
This is what it says: Global variables use 2032 bytes (99%) of dynamic memory, leaving 16 bytes for local variables. Maximum is 2048 bytes.
My project has a potentiometer and 10 capacitive sensors, which I declared as global variables in the beginning. When you touch one of them, the Arduino plays a .wav song from an SD card module fitting the combination of the potentiometer (representing a decade) and the capacitive sensors (representing different regions.).
In order to do this, I need several libraries (which I think also use RAM, if I am correct.) and for example, the capacitive sensor library has to use long variables. I tried using int to save memory, but that did not work.
I have one big loop that does this all, but I am happy to learn how I could do this differently in a way that uses less memory.
Here is my code:
//for the SD card module:
#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"
TMRpcm tmrpcm;
//for the sensors:
#include <CapacitiveSensor.h>
char songname[ ] = "nothing";
CapacitiveSensor cs_a4_a5 = CapacitiveSensor(A4, A5); //North America
CapacitiveSensor cs_a4_a3 = CapacitiveSensor(A4, A3); //South America
CapacitiveSensor cs_a4_a2 = CapacitiveSensor(A4, A2); //Europe
CapacitiveSensor cs_a4_a1 = CapacitiveSensor(A4, A1); //Africa
CapacitiveSensor cs_a4_2 = CapacitiveSensor(A4, 2); //Middle East
CapacitiveSensor cs_a4_3 = CapacitiveSensor(A4, 3); //Russia
CapacitiveSensor cs_a4_5 = CapacitiveSensor(A4, 5); //India
CapacitiveSensor cs_a4_6 = CapacitiveSensor(A4, 6); //China
CapacitiveSensor cs_a4_7 = CapacitiveSensor(A4, 7); //Asia
CapacitiveSensor cs_a4_8 = CapacitiveSensor(A4, 8); //Oceania
//for the potentiometer:
int sensorValue = 0;
int decade = 0;
void setup() {
// Music Output:
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(6.5);
// Capacitive Sensors:
}
void loop() {
long sensor1 = cs_a4_a5.capacitiveSensor(50); //variable for region 1
long sensor2 = cs_a4_a3.capacitiveSensor(50); //variable for region 2
long sensor3 = cs_a4_a2.capacitiveSensor(50); //variable for region 3
long sensor4 = cs_a4_a1.capacitiveSensor(50); //variable for region 4
long sensor5 = cs_a4_2.capacitiveSensor(50); //variable for region 5
long sensor6 = cs_a4_3.capacitiveSensor(50); //variable for region 6
long sensor7 = cs_a4_5.capacitiveSensor(50); //variable for region 7
long sensor8 = cs_a4_6.capacitiveSensor(50); //variable for region 8
long sensor9 = cs_a4_7.capacitiveSensor(50); //variable for region 9
long sensor10 = cs_a4_8.capacitiveSensor(50); //variable for region 10
Serial.print("n:");
Serial.print(sensor1); // print sensor output
Serial.print("\ts:");
Serial.print(sensor2); // print sensor output
Serial.print("\te:");
Serial.print(sensor3); // print sensor output
Serial.print("\ta:");
Serial.print(sensor4); // print sensor output
Serial.print("\tm:");
Serial.print(sensor5); // print sensor output
Serial.print("\tr:");
Serial.print(sensor6); // print sensor output
Serial.print("\ti:");
Serial.print(sensor7); // print sensor output
Serial.print("\tc:");
Serial.print(sensor8); // print sensor output
Serial.print("\tz:");
Serial.print(sensor9); // print sensor output
Serial.print("\to:");
Serial.println(sensor10); // print sensor output
//potentiometer for decades
int sensorValue = analogRead(A0); // read the input on analog pin 0:
decade = map(sensorValue, 0, 1023, 10, 0); // map it to the range of the analog out:
Serial.print("sensor = "); // print the results to the Serial Monitor:
Serial.print(sensorValue);
Serial.print("\t decade = ");
Serial.println(decade);
if (sensor1 > 70) {
sprintf(songname, "n%d.wav", decade);
tmrpcm.play(songname);
Serial.print("now playing you a song from North America from the decade ");
Serial.print(decade);
Serial.print(": ");
Serial.println(songname);
delay(300);
}
if (sensor2 > 70) {
sprintf(songname, "s%d.wav", decade);
tmrpcm.play(songname);
Serial.print("now playing you a song from South America from the decade");
Serial.print(decade);
Serial.print(": ");
Serial.println(songname);
delay(300);
}
if (sensor3 > 70) {
sprintf(songname, "e%d.wav", decade);
tmrpcm.play(songname);
Serial.print("now playing you a song from Europe from the decade");
Serial.print(decade);
Serial.print(": ");
Serial.println(songname);
delay(300);
}
if (sensor4 > 70) {
sprintf(songname, "a%d.wav", decade);
tmrpcm.play(songname);
Serial.print("now playing you a song from Africa from the decade");
Serial.print(decade);
Serial.print(": ");
Serial.println(songname);
delay(300);
}
if (sensor5 > 70) {
sprintf(songname, "m%d.wav", decade);
tmrpcm.play(songname);
Serial.print("now playing you a song from the Middle East from the decade");
Serial.print(decade);
Serial.print(": ");
Serial.println(songname);
delay(300);
}
if (sensor6 > 70) {
sprintf(songname, "r%d.wav", decade);
tmrpcm.play(songname);
Serial.print("now playing you a song from Russia from the decade");
Serial.print(decade);
Serial.print(": ");
Serial.println(songname);
delay(300);
}
if (sensor7 > 70) {
sprintf(songname, "i%d.wav", decade);
tmrpcm.play(songname);
Serial.print("now playing you a song from India from the decade");
Serial.print(decade);
Serial.print(": ");
Serial.println(songname);
delay(300);
}
if (sensor8 > 70) {
sprintf(songname, "c%d.wav", decade);
tmrpcm.play(songname);
Serial.print("now playing you a song from China from the decade");
Serial.print(decade);
Serial.print(": ");
Serial.println(songname);
delay(300);
}
if (sensor9 > 70) {
sprintf(songname, "z%d.wav", decade);
tmrpcm.play(songname);
Serial.print("now playing you a song from Asia from the decade");
Serial.print(decade);
Serial.print(": ");
Serial.println(songname);
delay(300);
}
if (sensor10 > 70) {
sprintf(songname, "o%d.wav", decade);
tmrpcm.play(songname);
Serial.print("now playing you a song from Australia or New Zealand from the decade");
Serial.print(decade);
Serial.print(": ");
Serial.println(songname);
delay(300);
}
}
Instead of the number 70 I had a global variable threshold before, I changed that and hoped it would save memory, but I think it did not help.
Thanks for any help!