Function ends in SOS Heap Memory Error

This probably has nothing to do with my Photon, but every time this function is called over a webhook, the tone plays, finishes, and then SOS led, reset, then breathing led (and I can play it again). I’m guessing it’s my C++ noobishness.

#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523

 
int melodyPin = A5;

int pokemonMelody[] = {
    NOTE_A4, NOTE_A4, NOTE_A4, NOTE_A4, NOTE_A4,
    NOTE_G4, NOTE_E4, NOTE_C4, 0, NOTE_C4, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4
};


int pokemonTempo[] = {
12,12,12,6,7,6,6,6,12,9,6,6,6,16,3
};



void setup() {
    Particle.function("play-song", playSong);
}
void loop() {
    
}


int  playSong(String command)
{
for(int thisNote = 0; thisNote < sizeof(pokemonTempo); thisNote++){
       int noteDuration = 1500/pokemonTempo[thisNote];
       tone(melodyPin, pokemonMelody[thisNote], noteDuration);
 
       int pause = noteDuration * 1.33;
       delay(pause);
 
       noTone(melodyPin);
   };
   return 1;
}

This line is the problem; that sizeof function will return 60 (15 numbers x 4 bytes per number), so you’re going out of bounds on your array. That line should be,

for(int thisNote = 0; thisNote < sizeof(pokemonTempo)/sizeof(pokemonTempo[0]); thisNote++)

Also, Particle.function should return relatively quickly, so you might want to put the code you have in there in loop, and just set a flag in the Particle.function that you test for in loop.

2 Likes

in addition to fixing the for loop as @Ric suggested, try just setting a flag in your Particle function like this (not tested):

#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523

 
int melodyPin = A5;

int pokemonMelody[] = {
    NOTE_A4, NOTE_A4, NOTE_A4, NOTE_A4, NOTE_A4,
    NOTE_G4, NOTE_E4, NOTE_C4, 0, NOTE_C4, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4
};


int pokemonTempo[] = {
12,12,12,6,7,6,6,6,12,9,6,6,6,16,3
};

bool goSong = false;

void setup() {
    Particle.function("play-song", playSong);
}
void loop() 
{
  if(goSong)
    {
      for(int thisNote = 0; thisNote < sizeof(pokemonTempo)/sizeof(pokemonTempo[0]); thisNote++)
      {
       	noteDuration = 1500/pokemonTempo[thisNote];
	tone(melodyPin, pokemonMelody[thisNote], noteDuration);

	int pause = noteDuration * 1.33;
	delay(pause);

	noTone(melodyPin);
	goSong = false;
   } 
}


int  playSong(String command)
{
    goSong = true;
    return 1;
}

and use caution with variable names using this, as later on you will see that is an important keyword in C++!!!

1 Like

Man thank you guys both so so much! My kid and I are having a blast with our Photons and one Electron. This was his desire :wink: I see my PHP/Ruby/JS background getting in the way of how I approach C++. But MAN is the Particle Ecosystem a blast to work with!

5 Likes