Go Down

Topic: micros() help (Read 623 times) previous topic - next topic


Here is my code.When i is 100,the result of the serial is 96.But When i is 10000,the result is still 96.If i change"a++" into "Serial.print(1)",the result changed. :~
Code: [Select]
void setup(){
void loop(){
 long i,a;


Shouldn't you be declaring "a=0" instead of just "a" ?


Feb 28, 2011, 02:40 am Last Edit: Feb 28, 2011, 02:41 am by frank26080115 Reason: 1
probably an optimization issue, the compiler probably knew that "a" was useless and didn't bother running your for-loop, but if you tell it to output "1", then it's forced to actually send that out the serial port, thus the for-loop remained in that case

solve the problem by using a NOP instruction in the for-loop, that's what I would do. Or make "i" volatile.
Freelance engineer, consultant, contractor. Graduated from UW in 2013.


I got the result after added "Serial.println(a)"and change "a++" into "a+=2".
It proves that the compiler is smart! :)
Thanks for your time!


Qualify 'a' as "volatile".
"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.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131