Erratic Arduino/Sketch behavior with unused volatile variable

Hi,

I am facing strange problem with one of my sketches. In order to investigate, I removed most of my application logic and narrowed to the below test code. I have TX and RX side using NRF24. Its a straightforward code.

The TX code displays strange behavior, it has an unused volatile variable, with this variable there are no transmissions, if the volatile keyword is removed or the variable is removed altogether, then the TX part works fine.

Here is the TX part

#include <RF24.h>
RF24 radio(7, 8);
typedef unsigned long  Address; 
Address  hh_address = 0xeeddeedd;

/////////BELOW VARIABLE CAUSES PROBLEM,IF DECLARED AS VOLATILE/////////////
volatile bool first_time= true;

char buff[32];

void test()
{
  radio.write(buff, 5);
  delay(250);
}

void setup() 
{
    Serial.begin(19200);
  
    radio.begin();

    radio.setDataRate(RF24_250KBPS);
    radio.setAutoAck(true);
    radio.enableAckPayload();   
    radio.setRetries(3,0); 
    radio.setAddressWidth(4);    
    
    radio.setChannel(120);
    radio.setPALevel(RF24_PA_HIGH);
    radio.openWritingPipe(&hh_address);

}

void loop()
{
    Serial.println("Sending data");
    test();
    delay(250);
}

Here is the RX side

#include <RF24.h>

RF24 radio(7, 8);

typedef unsigned long int Address;
Address  hh_address = 0xeeddeedd;

void setup() {
  Serial.begin(19200);
  // put your setup code here, to run once:
  radio.begin();

  radio.setDataRate(RF24_250KBPS);
  radio.setAutoAck(true);
  radio.enableAckPayload();   
  radio.setRetries(3,0);  
  radio.setAddressWidth(4);    
  
  radio.setChannel(120);
  radio.setPALevel(RF24_PA_HIGH);

  radio.openReadingPipe(1, &hh_address);
  radio.startListening();
}

void loop() {
  // put your main code here, to run repeatedly:
  char buff[32]; 
  static unsigned int i=0;
  if( radio.available())
  {
    Serial.print("Data:");
    Serial.println(i);
    i++;
    radio.read(buff, 32);
  }
}

Please explain the behavior.

Thanks and Regards,
WonderfulIOT

radio.openWritingPipe(&hh_address);

.openWritingPipe() expects a uint64_t, not a pointer to a unsigned long.... Same for .openReadingPipe()

septillion:

radio.openWritingPipe(&hh_address);

.openWritingPipe() expects a uint64_t, not a pointer to a unsigned long… Same for .openReadingPipe()

If you are using the newer NRF library, the uint64_t parameter function is deprecated and there is a new function getting a const uint8_t *address.

openpipes.png

Anyway, the 5 bytes used have to be the same for sender and receiver.

Ahh, didn’t know, wasn’t the first I found. And I do think a pointer to an address is a bit weird but alright. But that way the compiler is dumping most of the info pointed to… Because he uses a uint64_t…

@wonderfuliot, if we need to explain the behaviors maybe you can explain what happens?

You're sending an uninitialized array. What do you expect to happen?