NRF24L01: Sending values in setup void not always working

My Atmega328 is placed on a breadboard and connected to a 5V power source.
The NRF24L01 is getting the power from a voltage regulator.

I need to send a value from the NRF24L01 in the setup void and it is not working.

This is the code which doesnt work:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <EEPROM.h>


RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
char txRxByte = "0";

void setup(){
  
  radio.begin();
  radio.write(&txRxByte, sizeof(txRxByte));
  radio.setPayloadSize(1);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(126);
  radio.openWritingPipe(pipe);
  radio.openReadingPipe(1,pipe);
  radio.powerUp();
  delay(100);
  radio.write(&txRxByte, sizeof(txRxByte));
}
  
void loop(){
  
}

This is the code which works fine:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <EEPROM.h>


RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
char txRxByte = "0";

void setup(){
  
  radio.begin();
  radio.write(&txRxByte, sizeof(txRxByte));
  radio.setPayloadSize(1);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(126);
  radio.openWritingPipe(pipe);
  radio.openReadingPipe(1,pipe);
  radio.powerUp();
  delay(100);
}
  
void loop(){
  delay(5000);
  radio.write(&txRxByte, sizeof(txRxByte));
}

Do you have an idea why sending a value from the NRF24L01 in the setup void is not working?

Thanks in advance
mtdshare

Could it be something to do with the five second delay?

(It's a function, not a void)

AWOL:
Could it be something to do with the five second delay?

(It's a function, not a void)

This delay is only for testing, it does work aswell without it (in the loop function).

I already tried to add a large delay (10 seconds) to the setup function but nothing changed.

Why do you do a write before fully initialising the unit?

AWOL:
Why do you do a write before fully initialising the unit?

I want to register to my receiver (sending an id) and I want to do this only once.
Because of that i placed the first write in the setup function.

mtdshare:
This delay is only for testing, it does work aswell without it (in the loop function).

I already tried to add a large delay (10 seconds) to the setup function but nothing changed.

Do you know if the call to radio.write() works the first time in loop()? Maybe it is only working because it is being called more than once.

Otherwise I can't see why it would work in loop() and not in setup(). Maybe you could put a short FOR loop inn setup() that calls radio.write() a few times. And why not check that it has actually transmitted properly?

Of course the problem might also lie in the receiver code which we have not seen.

...R

I want to register to my receiver (sending an id) and I want to do this only once.

There are ways of doing this without putting it in setup().

Create a boolean variable and set it to false. Run the required code you want in loop() only when the variable is false. When you run the code set the boolean to true to prevent it running again.

Robin2:
Do you know if the call to radio.write() works the first time in loop()? Maybe it is only working because it is being called more than once.

Otherwise I can't see why it would work in loop() and not in setup(). Maybe you could put a short FOR loop inn setup() that calls radio.write() a few times. And why not check that it has actually transmitted properly?

Of course the problem might also lie in the receiver code which we have not seen.

...R

Thank you for your answer. I now put a for loop in the setup and it really sends all values except the first one.
In this case i received 4 values and not 5.
Do you got an idea why this happens?

void setup(){
  //Serial.begin(4800);
  pinMode(8, INPUT_PULLUP);
  radio.begin();
  radio.setPayloadSize(1);
  radio.openWritingPipe(pipe);
  radio.openReadingPipe(1,pipe);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(126);
  delay(500);
  for(int i=0; i<=4; i++) {
    char txRxByte = generateByte(nodeId, OK, NOTOK);
    bool ack = radio.write(&txRxByte, sizeof(txRxByte));
    delay(100);
  }
}

I always checked at the receiver if the values are sent correctly.

Here is my receiver code:

#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#include <EEPROM.h>

char txRxByte;
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(void){
  EEPROM.write(0, 255);
  Serial.begin(9600);
  radio.begin();
  radio.setPayloadSize(1);
  radio.openReadingPipe(1,pipe);
  radio.openWritingPipe(pipe);
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(126);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
  Serial.print("Selected Power Level: ");
  Serial.println(radio.getPALevel());
  Serial.println(getRegisteredNodes());
  delay(100);
}

void loop(void){
  if (radio.available()){
    //read data from nrf24l01 buffer
    Serial.println("data received");
    radio.read(&txRxByte, sizeof(txRxByte));
      printBits(txRxByte);
   }
}

void printBits(byte myByte) {
  for (byte mask = 0x80; mask; mask >>= 1) {
    if (mask  & myByte)
      Serial.print('1');
    else
      Serial.print('0');
  }
  Serial.println("");
}

UKHeliBob:
There are ways of doing this without putting it in setup().

Create a boolean variable and set it to false. Run the required code you want in loop() only when the variable is false. When you run the code set the boolean to true to prevent it running again.

I think this could be a workaround. I will try if this works stable.
But anyway it would be interesting why we could not send the first value in the setup function.

mtdshare:
I always checked at the receiver if the values are sent correctly.

That is wrong. At the receiver end all you can do is check if they are received correctly.

I meant that you should have code in your Tx program to check if the Tx works.

it really sends all values except the first one.
In this case i received 4 values and not 5.

Is the receiver powered up before the transmitter?

...R

Robin2:
That is wrong. At the receiver end all you can do is check if they are received correctly.

I meant that you should have code in your Tx program to check if the Tx works.
Is the receiver powered up before the transmitter?

...R

The receiver is always powered up before the transmitter.
I tried a few things again and noticed that the first transmission only fails if i remove the power from the tx unit and give it back again. The rx unit stays powered on at this time.
If i restart the rx unit the first message is received normally again.

If i add "radio.setAutoAck(false);" all transmissions are working always properly.
But i need to use the autoAck feature.

If the first transmission is not reliable then just ignore it and always send the first message twice.

If that might cause confusion you could include a '1' in the first message sent and a '2' in the second message. That way the receiver can tell which one it has received even if they are otherwise the same.

...R