Go Down

Topic: Error(s) preventing return to loop when code added (Read 699 times) previous topic - next topic

fanuch

Feb 02, 2013, 06:08 pm Last Edit: Feb 02, 2013, 06:28 pm by fanuch Reason: 1
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:

Code: [Select]
   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 :(

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

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

fanuch

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

:)

PaulS

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

fanuch

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

Runnning an Uno.


PaulS

Quote
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");

fanuch

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.

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

fanuch

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.

Code: [Select]
// 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);
              }

AWOL

Quote
gave me errors for every single one that I changed
, and I'm not going to show the errors or the code.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

fanuch

Code: [Select]
initializer fails to determine size of '__c'

if that helps

AWOL

Well, I suppose that's a part of what I asked for.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

fanuch

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.

AWOL

Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

fanuch


Go Up