Hi Alfonso,
A correct solution can only be given after understanding your complete design flow. There are many open questions. Few are here.
1. where do you again reset the bworkPending = false in the main loop() once it set in the ISR.
2. Consider you entered into the IDLE state. After entering into the IDLE state how do you wake up? e.t.c., What is the mechanism used to go-to-sleep--->wake-up--->go-to-sleep-->
3. Don't you see any significance of having volatile type here?
If the use case is clear, you can consider using any of the synchronization mechanism such as mutex,semaphore e.t.c,
However, I have some generic suggestion.
1. You can treat the portion of code /*03/ to /*06/ as a critical section and protect it. Protect the code; Enter into the critical section complete the current task; unprotect the code; and then jump into other routines.
2. Disabling the interrupts before entering into the "IDLE state" and then enabling the interrupts may also be an option.
Regards,
shankari
-------------------------------------------------------------------------------------------------------
Please click the Verify Answerbutton on this post if it answers your question.
--------------------------------------------------------------------------------------------------------