Serial.println

hi folks,

i'm experiencing something quite weird.

my code works great when i print to the console some stuff and when i remove the Serial.println stuff, the code freeze at certain points in code.

i wonder if there's a memory overflow being prevented with the Serial.println instruction. maybe you guys have some tip ?

thanks

Did you mean to attach some code for someone to look at? Hard to spot the errors without seeing it.

here’s the code though i thought as a more general question but maybe you’ll find something in there :slight_smile:

Boardgame.ino (9.39 KB)

though i thought as a more general question

As a more general answer:-

i wonder if there's a memory overflow being prevented with the Serial.println instruction.

No quite the converse.

However a Serial.print takes time and the delay in the code might just make it work if you are reading things too fast.

so you think i'm actually reading code too fast ?

how can i prevent that ? delay ?

7 below will show you how to post your code using # code tags so more people will look at it.

so you think i’m actually reading code too fast

No that is not possible.

I was answering a general question. For example if you are reading a nunchuck through I2C and printing out the results it will be fine, however if you remove the print statement you will read it too fast and it will not return the right results.

If you want help with your code then it is not sufficient to just post it.
Did you read the post that said “Read this before posting a question here”? Rhetorical really because if you did you decides to ignore it.

i can't put the code within # because it exceeds max characters limit.

int beats[] = {1, 2, 2, 1, 2, 2, 1, 2, 1, 1, 2, 1}; // 1 means LOW getHitValue_flag, 2 means HIGH getHitValue_flag
int silences[] = {NOIR, NOIR, NOIR, NOIR, NOIR, NOIR, NOIR, NOIR, NOIR, NOIR, NOIR, NOIR};
int sequenceSize = 12;
int beatIndex = 0;

Do any of these NEED to be ints? Pissing memory away uselessly is NOT a good idea.

   CPUBeatsSequenceTimerStart = millis();

The millis() function is NOT a timer. It returns a time. You should not be using timer in variable names, or thinking of it as a timer.

   if (CPUTurn){
   } else if (!CPUTurn) {

It's either the CPU's turn or it isn't. There are no other choices. Testing that it isn't is silly.

         Serial.println("before setValidateHit()");
         setValidateHit();
         Serial.println("after setValidateHit()");

Do some research on the F() macro. Quit wasting SRAM.

When you run out of SRAM space what happens is that some variables get overwritten or lost entirely due to being put in memory locations that do not exist. Also there is no real place to store function return addresses so that code will appear to do some odd things. It sounds to me like you have run out of memory and that adding extra print statements just shuffles the points of lost variables around a bit. Therefore you might just shuffle them to a place where the code appears to do more. I suspect that even with your print statements in you code is giving problems. As PaulS says, try and reduce the amount of memory you burn.

thanks for those good tips, but you say not to use millis() so what is the usage for timer ?

now, the thing is that with println instructions, this code works really as it should, constantly and during a long period of time.

PaulS is not saying don't use millis(). His objection is to the name of your variable including the word Timer.

my bad :)

is it recommended to always put some sort of delay in the loop() function to avoid data flooding ?

is it recommended to always put some sort of delay in the loop() function to avoid data flooding

No. Although it is a common misconception as you see it a lot in code.

kk,

now i’ve narrowed the problem in my code ; if i comment each console print but add a delay(20) in a certain point in code (line 108), it works.

so it may be my logic rather than memory misusage though my code is now lighter thanks to you guys :slight_smile:

i attach the code in case someone wants to help me.

thanks

Boardgame.ino (9.29 KB)

k bug found !

thanks

k bug found !

And it was? Don't be embarrassed. Most of us have had to track down bugs once or twice (a day).

order of execution put the process in a stall state (a flag which never switched).