Variables keep going back to null

I keep having a problem with my m5core2. I know what is causing it but not the reason why or how to remedy the situation. If I for example change my playwav volume from having to type it every time to having a variable that way you can change the volume on all of them together. So i assigned a variable. (int vol_con) however once I upload the code it works until it trys to find the wav file then gets stuck in a reboot cycle. I found it is being returned a null value. How and why is that happening??

decoder log:

PC: 0x401ac4bd: AudioFileSourceUnsync::getByte() at C:/Users/Administrator.MYTDS/Documents/Arduino/libraries/ESP8266Audio/src/AudioFileSourceID3.cpp line 76
EXCVADDR: 0x00000000

Decoding stack results
0x401ac4bd: AudioFileSourceUnsync::getByte() at C:/Users/Administrator.MYTDS/Documents/Arduino/libraries/ESP8266Audio/src/AudioFileSourceID3.cpp line 76
0x400f5c03: AudioGeneratorWAV::begin(AudioFileSource*, AudioOutput*) at C:/Users/Administrator.MYTDS/Documents/Arduino/libraries/ESP8266Audio/src/AudioGeneratorWAV.cpp line 296
0x400e0801: handleAlarmsInfoLine(NSinfo*) at C:/Users/Administrator.MYTDS/Documents/PlatformIO/Projects/paci_1248/src/goodnites.ino line 1739
0x400e3a62: MicroDot::setBrightness(unsigned char) at src/microdot.cpp line 158
0x400fbbd6: _GLOBAL__sub_I_loopTaskHandle() at C:/Users/Administrator.MYTDS/.platformio/packages/framework-arduinoespressif32/cores/esp32/IPAddress.h line 94
0x40091d0a: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

Show us your code.

which part? the area i declaired the variable? where im using them? I cant post the whole code as the sketch is about 3000 lines and i wont subject anyone to that torture

////////////////AUDIO/////////////
 void playWAV (char effect, vol_con)
{
  Serial.println("Entered playWAV");
  switch (effect)
  {
    case 'a':
      file = new AudioFileSourceSD("/wav/alarm.wav");
      break;

    case 'b':
      file = new AudioFileSourceSD("/wav/alarmhigh.wav");
      break;

    case 'c':
      file = new AudioFileSourceSD("/wav/alarmlow.wav");
      break;

    case 'd':
      file = new AudioFileSourceSD("/wav/error.wav");
      break;
  
    case 'e':
      file = new AudioFileSourceSD("/wav/error1.wav");
      break;
  
    case 'f':
      file = new AudioFileSourceSD("/wav/noread.wav");
      break;
      
    case 'g':
      file = new AudioFileSourceSD("/wav/noreadings.wav");
      break;
      
    case 'h':
      file = new AudioFileSourceSD("/wav/normalrange.wav");
      break;
      
    case 'i':
      file = new AudioFileSourceSD("/wav/warning.wav");
      break;

    case 'j':
      file = new AudioFileSourceSD("/wav/warninghigh.wav");
      break;
      
    case 'k':
      file = new AudioFileSourceSD("/wav/warninglow.wav");
      break;
      
    case 'l':
      file = new AudioFileSourceSD("/wav/startup.wav");
      break;  

      case 'm':
      file = new AudioFileSourceSD("/wav/startup.wav");
      break;      
      
      case 'n':
      file = new AudioFileSourceSD("/wav/update.wav");
      break;      
      }
float volumeGain = ((float)volume / 100.0) * 39.0;
  Serial.print("volumeGain:");
  Serial.println(volumeGain);
  id3 = new AudioFileSourceID3(file);
  out = new AudioOutputI2S(0, 0); // Output to builtInDAC
  out->SetPinout(12, 0, 2);
  out->SetOutputModeMono(true);
  out->SetGain(volumeGain);
  wav = new AudioGeneratorWAV();
  wav->begin(id3, out);
}


int vol_con = 3;

The entire sketch. Bits and pieces don't tell the whole story and can't be copy & pasted into a sketch for other to try...

I don't see how I can do that even

  1. It's a project for my work and don't know if I can share the whole thomg

  2. Over 3k lines would that. Even fit here?

Is it possible you can simplify the code, eliminating the non related procedures? It would be best if you could boil the code down to just showing the issue.
Even if you had to put dummy functions in place

what i posted is where the problem is. its inside the void playwav function. I have specified the variable directly below the function. you can see i have the value set but if i push it to the device it crashes and goes into a boot loop.

So it should be easy to put a few lines for setup and a loop with the above code and it will show the issue. No?

should be, you would need the following includes however...

#include "SPIFFS.h"
#include "AudioFileSourceSPIFFS.h"
#include "AudioFileSourceSD.h"
#include "AudioFileSourceID3.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2S.h"

AudioGeneratorWAV *wav;
AudioFileSourceSD *file;
AudioOutputI2S *out;
AudioFileSourceID3 *id3;
bool playing = false;
#define OUTPUT_GAIN 100```

If you don't want to show the whole code only guessings into the fog are possible:

if you declared your variables not totally global but instead inside functions this way

void myFunc() {
int myVariable = 0;

each time the function gets called new it is assigned zero
like coded

if this is not the case add serial-debug-out at all places you supsect where the resetting to zero might happen and narrow down the problem this way

another possible reason might be wrong array-boundaries

best regards Stefan

I think your onto somethjng. I thought I was putting, them global buy I didn't close the PlayWAV command fully... Maybe that is it... Wait another thing. The volume that I'm trying to control, it's listed in the function as char effect, int volume... However volume is a part of a float. Within the lower half of the code for that function
Could that be it?

float volumeGain = ((float)volume / 100.0) * 39.0;

This is also a 100% stupid question but I have to wear the dunce cap and ask, because I'm trying to is set up day night modes using. Localtimeinfo.tm_hour >= 20.blah blah
.
Can there be two sets of variables for the same function? As the way its constructed is using that line above if before 20 00 hours it's false skips it completly if equal or greater than 20 00 its true so use xyz
Basically its a if true else skip.
Would this be possible? I'm asking because const variables work fine

??? don't understand what you mean. If you use global variables you can have as much as the RAM offers.

I was tryi g to ask.... Because I can't get non static variables to work could I do something like this

If (night_mode)
Variable a. 100
Variable b. 255
On and on.

Else

Variable a 0
Variable b 10

True or false if statement and both variables are constant.... Because I can use constant variables but all
Y normal ones get reset

if you define them global you can do this

ok, this is different, I took a change because I seperate things into header files sometime if they have a lot of different void functions (like my led effects). I decided to try putting all my variables into a variables.h file, and #include "variables.h" right after include arduino. magically it is working. the variables stay after a reboot.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.