Hello,
I am getting this message after verification:
Sketch uses 11886 bytes (36%) of program storage space. Maximum is 32256 bytes.
Global variables use 1889 bytes (92%) of dynamic memory, leaving 159 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.
I am actually having stability problems after several hours of operation of the Arduino due to low memory. I would appreciate any help on getting the 92% Global Variable memory usage down. I'm trying to not use a Mega at this point, as there is a bit of a deadline on this project.
Using:
Arduino Uno
Adafruit Wave Shield
Green LEDs
Red LEDs
Idea: If no outside signal, things blink while the sounds go off at the same time
Code (not all of it because of the 9000 max characters allowed in a forum post, see first reply for rest):
//Wave 5 ~jULY 2017 //
#include <WaveHC.h>
#include <WaveUtil.h>
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the volumes root directory
FatReader file; // This object represent the WAV file for a pi digit or period
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
uint8_t dirLevel; // indent level for file/dir names (for prettyprinting)
dir_t dirBuf; // buffer for directory reads
uint8_t countStateGood = 0;
uint8_t countStateBad = 0;
#define error(msg) error_P(PSTR(msg))
int Good_G = 6;
int Bad_B = 7;
int GLED = 8;
int RLED = 9;
int i = 0;
int j = 0;
int buttonPusherCounterGood = 0;
int buttonStateGood = 0;
int lastButtonStateGood = 0;
int buttonPusherCounterBad = 0;
int buttonStateBad = 0;
int lastButtonStateBad = 0;
unsigned long previousMillis1 = 0;
long OnTime1 = 250;
long OffTime1 = 750;
unsigned long previousMillis2 = 0;
int TimeState1 = 0;
int prevTimeState1 = 0;
int TimeState2 = 0;
int prevTimeState2 = 0;
long intervalRandom = 180000; //30,000 = 30sec 60,000 = 1 min 120,000 = 2 min 180,000 = 3min 300,000 = 5 min
void setup()
{ Serial.begin(9600);
PgmPrintln("Wave_5");
if (!card.init()) {
error("Card init. failed!");
}
if (!vol.init(card)) {
error("No partition!");
}
if (!root.openRoot(vol)) {
error("Couldn't open dir");
}
PgmPrintln("Files found:");
root.ls();
pinMode(Good_G, INPUT_PULLUP);
pinMode(Bad_B, INPUT_PULLUP);
pinMode(GLED, OUTPUT);
pinMode(RLED, OUTPUT);
//Start Up/ Reset Acknowledgement
// Serial.println(F("Start Up Cycle"));
digitalWrite(GLED, HIGH);
playcomplete("GOOD2.WAV");
digitalWrite(GLED, LOW);
}
void loop() {
unsigned long currentTime1 = millis();
buttonStateGood = digitalRead(Good_G);
if (buttonStateGood != lastButtonStateGood){
if(buttonStateGood == LOW){
buttonPusherCounterGood++;
// Serial.println(F("Begin Good button increment"));
// Serial.print(F("Number of Count: "));
// Serial.println(buttonPusherCounterGood);
if (buttonPusherCounterGood % 3 != 0){
digitalWrite(GLED, HIGH);
// Serial.println(F("Playing Good Wav"));
playcomplete("GOOD.WAV");
digitalWrite(GLED, LOW);
}
else if (buttonPusherCounterGood % 3 == 0 && buttonStateGood == LOW){
// Serial.println(F("Randomized Good activated"));
digitalWrite(GLED, HIGH);
RandomizedGood();
digitalWrite(GLED, LOW);
}
else {
// Serial.println(F("End Good button increment"));
}
}
previousMillis1 = currentTime1;
}
lastButtonStateGood = buttonStateGood;
buttonStateBad = digitalRead(Bad_B);
if (buttonStateBad != lastButtonStateBad){
if((buttonStateBad == LOW) && (buttonStateGood == HIGH)){
buttonPusherCounterBad++;
// Serial.println(F("Begin Bad button increment"));
// Serial.print(F("Number of Count: "));
// Serial.println(buttonPusherCounterBad);
if(buttonPusherCounterBad % 3 != 0 ){
// Serial.println(F("Playing playbad void"));
playbad ("BAD.WAV");
digitalWrite(RLED, LOW);
}
else if ((buttonPusherCounterBad % 3 == 0) && (buttonStateBad == LOW) && (buttonStateGood == HIGH)){
// Serial.println(F("Randomize Bad activated"));
RandomizedBad();
digitalWrite(RLED, LOW);
}
else {
Serial.println(F("End Bad button increment"));
}
}
previousMillis1 = currentTime1;
}
lastButtonStateBad = buttonStateBad;
if ((buttonStateBad == HIGH) && (buttonStateGood == HIGH) && (currentTime1 - previousMillis1 >= intervalRandom)){
RandomizedRandom();
previousMillis1 = currentTime1;
}
if (TimeState1 != prevTimeState1){
previousMillis1 = currentTime1;
}
prevTimeState1 = TimeState1;
}
void error_P(const char *str) {
PgmPrint("Error: ");
SerialPrint_P(str);
sdErrorCheck();
while(1);
}
void sdErrorCheck(void) {
if (!card.errorCode()) return;
PgmPrint("\r\nSD I/O error: ");
Serial.print(card.errorCode(), HEX);
PgmPrint(", ");
Serial.println(card.errorData(), HEX);
while(1);
}
void playcomplete(char *name) {
playfile(name);
while (wave.isplaying);
sdErrorCheck();
}
void playfile(char *name) {
if (wave.isplaying) {
// already playing something, so stop it!
wave.stop(); // stop it
}
if (!file.open(root, name)) {
PgmPrint("Couldn't open file ");
Serial.print(name);
return;
}
if (!wave.create(file)) {
PgmPrintln("Not a valid WAV");
return;
}
// ok time to play!
wave.play();
}
void RandomizedBad(){
//Serial.println(F("Playing Randomized Bad wave via RandomizedBad void"));
i = random (3);
if ( i == 0){
// Serial.println(F("Playing BAD1"));
playbad("BAD1.WAV");
}
else if (i == 1) {
// Serial.println(F("Playing BAD2"));
playbad("BAD2.WAV");
}
else {
// Serial.println(F("Playing BAD3"));
playbad("BAD3.WAV");
}
}
void RandomizedGood(){
//Serial.println(F("Playing Randomized Good wave via RandomizedGood void"));
j = random (3);
if ( j == 0){
//Serial.println(F("Playing GOOD1"));
playcomplete("GOOD1.WAV");
}
if (j == 1) {
//Serial.println(F("Playing GOOD2"));
playcomplete("GOOD2.WAV");
}
if (j == 2) {
//Serial.println(F("Playing GOOD3"));
playcomplete("GOOD3.WAV");
}
}