cannot serial.print a variable that gets incremented in a ISR

I at the moment trying to print a variable that gets incremented in a ISR.

The serial communication shows a lot of "??" and never once the number i want.. what could be the problem?

speed_profile.cpp
speed_profile.h
main.ino

What am I doing wrong?

Serial.print is not being called from the ISR but The function compute_speed_profile, which compute some init stuff, and then has a thread for it own in which it Serial.print is being called.
the thread has interrupt guards on it.. which should stop interrupt from affecting the serial.print.

The output i receive from the serial monitor is something like this 88xx�8�8�888�88��8�8888�8��

speed_profile.h (2.03 KB)

main.ino (835 Bytes)

speed_profile.cpp (7.09 KB)

Are you calling Serial.print from within an ISR? Arduino doesn't like that

Maybe it's something simple like your (crazy) baud rate Serial.begin(230400); do you have that in the monitor?

What variables are used in the ISR and in the function? Those variables need to be declared volatile. Otherwise, the compiler assumes that they will not change, and probably throws a lot of your code away.

Two obvious problems:

  1. Any variables that are accessed by both interrupt and non-interrupt code must be declared volatile.
  2. Any multi-byte variables that are accessed by both interrupt and non-interrupt code must be read in the non-interrupt code within "interrupt guards":
noInterrupts()
//read variable here, and copy to a tmp variable
interrupts();

And, since you only provided snippets, rather than a complete program, there may well be other problems in the code you didn't show...

Regards,
Ray L.

The baudrate is correct (was also my first guess)
I changed all the member variable of speed profile to volatile, and it still doesn't provide me any useable information.

I changed all the member variable of speed profile to volatile

Is the instance volatile, too?

PaulS:
Is the instance volatile, too?

The only reason why I was only providing snippets and not the whole was to make the problem clearer, and no one would ever dare to read the complete code.
I am using interrupts guards (cli(), and sei()) but i will update the post with the full code.

I just changed the instance, and that didn't work either..:frowning:

(Hate to wait 5 minutes to reply)

I attached the full code above

This is weird.. I initializes the serial to run on baudrate 9600 but it is only readable on a higher baudrate ?
57600 in this case? why?

cli();
    status_step_count.data = profile.moved_steps;
    Serial.print("profile.moved_steps: ");
    Serial.println(profile.moved_steps);
    Serial.print('\n');
    sei();

Serial printing requires interrupts to be enabled.

cli();
    status_step_count.data = profile.moved_steps;
    sei();
    Serial.print("profile.moved_steps: ");
    Serial.println(profile.moved_steps);
    Serial.print('\n');

that makes sense, but as mentioned before does it work on different baudrate, eventhough i initialized it to 9600?

57600/9600 = 6. Rats, I was thinking maybe the "divide system clock by 8" fuse bit was incorrect.

Ahh.. could have been, but i am using timer1 and not timer0... So