Thread: IF statement not working within a WHILE loop

  1. #1
    Registered User
    Join Date
    Oct 2020

    Question IF statement not working within a WHILE loop

    I have this piece of code which is triggered when a bit is set high (the bit is set high when an interrupt occurs). It works when I have a delay in place, but not without. Can anyone explain why?
    while (1) {
    //  _delay_us(1);
        if (myFlags & (1 << serialAvailable)) {
          myFlags &= ~(1 << serialAvailable);
    If it matter, this is for an AVR microcontroller.
    So if I remove those comment symbols, it works. I cannot understand why?
    The only way I can get this to work is if I change...
    if (myFlags & (1 << serialAvailable)) {
    if (1) {
    Could this just be a artefact of the Atmel micro, or is this something with the code itself?

    john.c -> I cant reply, but yes I do. That is fixed but still the issue persists
    Last edited by AirborneFluff; 10-31-2020 at 12:31 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    I assume you mean to use bitwise AND (&), not logical AND (&&).
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  3. #3
    Registered User
    Join Date
    May 2012
    Arizona, USA
    How is myFlags declared? Is it shared with an ISR? If it is, then it should be declared with volatile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-25-2016, 08:49 AM
  2. If statement not working
    By howardbc14 in forum C Programming
    Replies: 1
    Last Post: 05-07-2015, 07:51 PM
  3. The following if statement is not working!...
    By darkmagic in forum C Programming
    Replies: 5
    Last Post: 06-23-2010, 08:11 AM
  4. If/else statement not working
    By zenovy in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2006, 08:26 PM
  5. If statement not working!
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 03-02-2002, 02:45 AM

Tags for this Thread