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
(word limit exceeded with script so any suggestions where to post?)
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.
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);
}
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.
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);
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...