0

I have a functionality on my mega 2560 to connect with other devices. Sometimes, devices can't answer. I don't want to wait for a longwhile. So, how can I interrupt the chunk of program after the specific amount of time?

My code for receiving messages is:

while (!(UCSR1A & (1<<RXC1)));

so, millis can't solve my problem because I haven't ability to check for conditional loops like that

if(currentMillis - previousMillis > interval) {
2
  • It's often better to mention why you would want to do such a thing. I'm guessing that you want to receive your messages every once in a while. It might be better to handle serial events in interrupts (for fastest receiving). Or to handle it in a timer interrupt (better timing). – Paul Nov 2 '15 at 20:33
  • The question might be better titled "How to break out of a loop with a timeout", since interrupts and delay have completely different meanings that you are using here. – Dave X Dec 2 '15 at 20:42
1

OMG, I was stupid. I can just place millis in this while condition.

1
  • 'if(currentMillis - previousMillis > interval) break;' may work as well. -- If the logic could become more complicated, a separate break statement for the conditions might make the code clearer. – Dave X Dec 2 '15 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.