0

I'm trying to use NRFlite library to facilitate a simple wireless remote incorporating a 3x4 keypad and nRF24L01 modules. I have it all working well except for one programming puzzle.

All of the example NRFLite receiver sketches that I can find online show the entire main program loop within a while statement: while (_radio.hasData())

Such as:

void loop() {



while (_radio.hasData())

{

    _radio.readData(&_radioData); // Note how '&' must be placed in front of the variable name.



    String msg = "Radio ";

    msg += _radioData.FromRadioId;

    msg += ", ";

    msg += _radioData.OnTimeMillis;

    msg += " ms, ";

    msg += _radioData.FailedTxCount;

    msg += " Failed TX";



    Serial.println(msg); }}

But my sketch is obviously much more complicated than these examples. I would like to pass the data received to a variable to use outside of this "while" loop to do a bunch of other stuff not related to the wireless remote. However, whenever I try to do that, the conditional argument of this "while" function "(_radio.hasData())" seems to test as TRUE, and the old data just received is treated again as new data with each pass through the program loop.

How can I pass the data packet outside of the "while (_radio.hasData())" function and get it to subsequently ignore that last data packet received?

Looking at the cpp file, it seems as if perhaps just calling the function "_radio.readData(&_radioData)" is intended to do just that by reseting a "data received flag", which makes sense as a way to accomplish just what I need. But it doesn't seem to work that way. Here is that "_radio.readData(&_readData)" function from the .cpp file:

void NRFLite::readData(void *data) {



// Determine length of data in the RX FIFO buffer and read it.

uint8_t dataLength;

spiTransfer(READ_OPERATION, R_RX_PL_WID, &dataLength, 1);

spiTransfer(READ_OPERATION, R_RX_PAYLOAD, data, dataLength);



// Clear data received flag.

uint8_t statusReg = readRegister(STATUS_NRF);

if (statusReg & _BV(RX_DR))

{

    writeRegister(STATUS_NRF, statusReg | _BV(RX_DR));
}
}

I would sure appreciate any suggestions, because otherwise, the NRFlite library seems very efficient for my simple need.

  • Seems like you need to add your code at the end of the while, not after it. PS If you also need to add some code that isn't packet-data dependent, you might want to change the while to an if – Gerben Feb 23 at 19:33
  • Gerben, thanks for the suggestions. I previously tried replacing the "while" with an "if" condition, but the result is the same. My sketch does kind of 'work' if it is all within the 'while' condition, but that isn't really workable because it would mean that the sketch would be dead until the first packet of data is received, and there are lots of other things the sketch does that don't depend on the wireless remote. – user54261 Feb 23 at 20:00
  • Could you add the sender side sketch or at least add how often packages are sent. – Mikael Patel Feb 24 at 10:47
  • Put the stuff that depend on the incoming packets inside the while, and the rest outside of the while. – Gerben Feb 24 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy