Offline
Newbie
Karma: 0
Posts: 11
|
 |
« on: February 02, 2013, 12:08:31 pm » |
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  (word limit exceeded with script so any suggestions where to post?)
|
|
|
|
« Last Edit: February 02, 2013, 12:28:59 pm by fanuch »
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 47
Posts: 1404
May all of your blinks be without delay
|
 |
« Reply #1 on: February 02, 2013, 12:17:56 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #2 on: February 02, 2013, 12:30:23 pm » |
Thanks for letting me know, I've included my full code as an ".ino" and the sound tab i used for outputs. 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35478
Seattle, WA USA
|
 |
« Reply #3 on: February 02, 2013, 12:37:26 pm » |
Which Arduino are you trying to run this on? It looks to me like it takes more memory than a 328-based Arduino has.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #4 on: February 02, 2013, 12:44:16 pm » |
Really? It was running fine with Serial inputs and all until I added the last bit which would complete my code  Runnning an Uno.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35478
Seattle, WA USA
|
 |
« Reply #5 on: February 02, 2013, 12:48:31 pm » |
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" );
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #6 on: February 02, 2013, 12:57:57 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19021
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: February 02, 2013, 01:10:28 pm » |
And the F macro for the strings?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #8 on: February 02, 2013, 01:13:17 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19021
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: February 02, 2013, 01:18:25 pm » |
gave me errors for every single one that I changed , and I'm not going to show the errors or the code.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #10 on: February 02, 2013, 01:27:37 pm » |
initializer fails to determine size of '__c' if that helps
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19021
I don't think you connected the grounds, Dave.
|
 |
« Reply #11 on: February 02, 2013, 01:28:36 pm » |
Well, I suppose that's a part of what I asked for.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #12 on: February 02, 2013, 01:32:02 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19021
I don't think you connected the grounds, Dave.
|
 |
« Reply #13 on: February 02, 2013, 01:34:09 pm » |
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.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #14 on: February 02, 2013, 01:34:54 pm » |
Ahh, thanks for explaining that.
|
|
|
|
|
Logged
|
|
|
|
|
|