Error(s) preventing return to loop when code added

Hello,

New to the Forum and Arduino so any help is much appreciated!
I’m writing a script which responds to a knock sequence from one peizo, it does a comparison of an existing array of sequence, and if there is a match, responds a tone sequence via a second peizo. My code works fine until I added an “error clause” when the wrong knock is detected. When that part of the code is commented out the script responds as it should, then returns to listening for another knock. When I un-comment the code and it is in the script it response with the correct tone but stops there and wont cycle back to the beginning!

Thanks for your patience.

The section of code in question is:

    Serial.println("*********Correct combination of notes!***********");
  Serial.println("    ");   
Serial.println("Reseting of arrays occuring.....");
    for(int i=0;i<maximumIntervals;i++){        //Reset temp array noteDurationArray
      noteDurationArray[i] = 0;
      melodyArray[i] = 0;      //Reset temp array for melody
      Serial.print(" noteDurationArray: ");
      Serial.print(noteDurationArray[i]);
      Serial.print("   melodyArray: ");
      Serial.println(melodyArray[i]);
  }
  Serial.println(responseNumber);   
  Serial.println("Transposition occuring.....");
    for(int i=0;i<maximumIntervals;i++){        //transponse stored response from array to noteDurationArray
      noteDurationArray[i] = noteDurations[responseNumber][i];
      melodyArray[i] = melody[responseNumber][i];
      Serial.print(" noteDurationArray: ");
      Serial.print(noteDurationArray[i]);
      Serial.print("   melodyArray: ");
      Serial.println(melodyArray[i]);
    }
      Serial.println("---------------------------Reached play melody part"); 
Serial.println(responseNumber);     
      // iterate over the notes of the melody:
               for (int thisNote = 0; thisNote < 8; thisNote++) {
                // to calculate the note duration, take one second 
                // divided by the note type.
                //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
                    int noteDuration = 1000/noteDurationArray[thisNote];
                    tone(10, melodyArray[thisNote],noteDuration);
                          // to distinguish the notes, set a minimum time between them.
                          // the note's duration + 30% seems to work well:
                    int pauseBetweenNotes = noteDuration * 1.30;
                    delay(pauseBetweenNotes);
                          // stop the tone playing:
                    noTone(10);
              }
         digitalWrite(redLED, HIGH);
         delay(500);
         digitalWrite(redLED, LOW);
}

This has been doing my head in as I can’t fix or work out the issue :frowning:

(word limit exceeded with script so any suggestions where to post?)

response_array_attempt__3_not_working_atm.ino (14.4 KB)

pitches.h (1.96 KB)

It would be helpful to see the full code. You can attach it to a post - look at Additional options bottom/left of the input box when posting a reply.

Thanks for letting me know, I've included my full code as an ".ino" and the sound tab i used for outputs.

:)

Which Arduino are you trying to run this on? It looks to me like it takes more memory than a 328-based Arduino has.

Really? It was running fine with Serial inputs and all until I added the last bit which would complete my code =(

Runnning an Uno.

It was running fine with Serial inputs and all until I added the last bit which would complete my code

“It was running just fine until I needed to use more SRAM. Then, I ran out, and it doesn’t work anymore. Why not?”

I think we’ve discovered the answer to that question. Look at your arrays. Do the int arrays need to be int sized? Could they be byte sized, instead, and use half the memory?

You can use the F() macro to keep constant strings out of SRAM:
Serial.print(F(“This doesn’t end up in SRAM”);

Changed the types to byte but it didn't help I'm afraid. I feel there is something in that section of code that is preventing the loop. Some kind of termination, though I can't see it no matter how hard I try and find it.

And the F macro for the strings?

gave me errors for every single one that I changed.

I don’t know much admittedly but the code loops if I omit this chunk of code and the end. Thanks for your support by the way.

// iterate over the notes of the melody:
               for (int thisNote = 0; thisNote < 8; thisNote++) {
                // to calculate the note duration, take one second 
                // divided by the note type.
                //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
                    int noteDuration = 1000/noteDurationArray[thisNote];
                    tone(10, melodyArray[thisNote],noteDuration);
                          // to distinguish the notes, set a minimum time between them.
                          // the note's duration + 30% seems to work well:
                    int pauseBetweenNotes = noteDuration * 1.30;
                    delay(pauseBetweenNotes);
                          // stop the tone playing:
                    noTone(10);
              }

gave me errors for every single one that I changed

, and I'm not going to show the errors or the code.

initializer fails to determine size of '__c'

if that helps

Well, I suppose that's a part of what I asked for.

IDE didn't like it on some of the Serial.print involving variables, no more error code. Still trying to resolve it or find a way around it. If it is SRAM isn't there a way to reset it? It is outputting the correct responses it just stops after playing a short beep response.

IDE didn’t like it on some of the Serial.print involving variables

That’s because the F macro leaves constants in program memory, but cannot put variables into program memory.

Ahh, thanks for explaining that.

I substituted my multi array with a single array and the code works,

These are the variables and the specific lines of code.

Declaired variables:

int melodyArray[10] = { 0 };            //Temp array for melody for music playback
int noteDurationArray[10] = { 0 };      //Temp array for duration of notes for music playback

Respective line of code:

 int noteDuration = 1000/noteDurationArray[thisNote];
tone(10, melodyArray[thisNote],noteDuration);

Substituted with functional variables:

 int melody[] = {
  NOTE_F4, NOTE_E4, NOTE_D4, NOTE_CS4, NOTE_AS3, NOTE_C4, NOTE_D4, NOTE_D4};
int noteDurations[] = {  4, 4, 4, 4, 4, 4, 3, 2, 4 };

Substituted with functional lines of code:

int noteDuration = 1000/noteDurations[thisNote];
tone(10, melody[thisNote],noteDuration);

That's as narrow down to the root of the problem I can ascertain. Any thing out of the ordinary or problematic?

Anyone who was following or cares, the problem was that it was dividing 1000 by 0 which caused it to go crazy and paradoxical. Added a simple break command if the denominator is 0. Solved...

Even better, add

break;

don't worry, I worked that out fine.