while(!something) loop is not working

I am having a heck of a time getting a nRF24L01 working on my board so I figured I would add some debugging statements to track down the problem. The code segment is this:

  while(!radio.begin());  // start radio
    {
      Serial.println("Radio Not Responding");
      delay(100);
      }

On the serial monitor it prints the statement "Radio Not Responding" and then continues on with the code. I would have expected it to try again. Just in the off chance that it was trying again and getting a true response on the second time I commented out that segment and just added a simple:

  Serial.println(radio.begin());

Sure enough, radio.begin is returning a "1". Why would the code within my while loop execute if radio.begin is returning a "1"?

What does radio.begin() do?

Doesn't it just start up the actual radio module? .

LarryD: What does radio.begin() do?

Does it just start up the actual radio module? .

I'm using this library:

https://github.com/tmrh20/RF24

radio.begin is defined in the RF24.cpp file.

I believe it just initializes the radio module. My board works fine using the GettingStarted example code. I'm trying to add a sleep function to save on power as the board will be powered from a battery. The radio module works fine on the first iteration of the loop, but then it doesn't work subsequent to the board being put to sleep and then woken back up.

  bool begin(void);

  /**
   * Start listening on the pipes opened for reading.
   *
   * 1. Be sure to call openReadingPipe() first.

Did you?

Did you really want to do this:

  while(!radio.begin())
      ;  // start radio

  {
      Serial.println("Radio Not Responding");
      delay(100);
  }

aarg: ```   bool begin(void);

  /**   * Start listening on the pipes opened for reading.   *   * 1. Be sure to call openReadingPipe() first.




Did you?

I think you may have read that wrong. The description is the block above the function prototype. So begin is:

/**
   * Begin operation of the chip
   * 
   * Call this in setup(), before calling any other methods.
   * @code radio.begin() @endcode
   */
  bool begin(void);

The description that you are showing is for the next function prototype:

/**
   * Start listening on the pipes opened for reading.
   *
   * 1. Be sure to call openReadingPipe() first.  
   * 2. Do not call write() while in this mode, without first calling stopListening().
   * 3. Call available() to check for incoming traffic, and read() to get it. 
   *  
   * @code
   * Open reading pipe 1 using address CCCECCCECC
   *  
   * byte address[] = { 0xCC,0xCE,0xCC,0xCE,0xCC };
   * radio.openReadingPipe(1,address);
   * radio.startListening();
   * @endcode
   */
  void startListening(void);

econjack: Did you really want to do this:

  while(!radio.begin());  // start radio

  {       Serial.println("Radio Not Responding");       delay(100);   }

I did that in an attempt to verify that the radio was actually responding to the initialization command. It was although for some reason that while loop was still executing code in the brackets.

Playing around with the example code that I know works, it seems that executing the following block in setup() works fine, but executing it in loop() doesn't work. Isn't loop() identical to while(1)? Why wouldn't it work in loop() but it does work in setup()?

radio.begin();

  // Set the PA Level low to prevent power supply related issues since this is a
 // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  radio.setPALevel(RF24_PA_LOW);
  
  // Open a writing and reading pipe on each radio, with opposite addresses
  if(radioNumber){
    radio.openWritingPipe(addresses[1]);
    radio.openReadingPipe(1,addresses[0]);
  }else{
    radio.openWritingPipe(addresses[0]);
    radio.openReadingPipe(1,addresses[1]);
  }
  
  // Start the radio listening for data
  radio.startListening();
while(!radio.begin());  // start radio
    { ...
    }

Remove the semicolon. Now the body of the while loop is an empty statement.

jerseyguy1996: in setup() works fine, but executing it in loop() doesn't work. Isn't loop() identical to while(1)? Why wouldn't it work in loop() but it does work in setup()?

Because initializations should only be done once, not over and over again.

By placing the above code in loop, you initialize the chip, set the non-standard settings, startListening and immediately after that you restart that process.

oqibidipo: while(!radio.begin());  // start radio     { ...     }

Remove the semicolon. Now the body of the while loop is an empty statement.

ooh....good catch my friend! I haven't tested it but I'm sure that was the problem.

Whandall: Because initializations should only be done once, not over and over again.

By placing the above code in loop, you initialize the chip, set the non-standard settings, startListening and immediately after that you restart that process.

In this case though I'm shutting the radio module down, actually cutting all power to it to save power so when I turn it back on I would assume that I would need to go through the initialization process again.

Don't assume, read the datasheet.

jerseyguy1996: ooh....good catch my friend! I haven't tested it but I'm sure that was the problem.

What do you think post #4 was saying, yet your response was:

I did that in an attempt to verify that the radio was actually responding to the initialization command. It was although for some reason that while loop was still executing code in the brackets.

So, which is it, a good catch or an initialization command?

econjack: What do you think post #4 was saying, yet your response was:

So, which is it, a good catch or an initialization command?

Sorry I missed that it was two people that caught that mistake. I for some reason thought that comment #8 and comment #4 were written by the same person.