nrf24 send/receive reply

im trying to write this program to make nrf1 send data to nrf2 and have nrf2 reply to nrf1 and just continue doing that over and over again. the program ive come up with keeps crashing after a couple send/receive cycles. can someone give me some insight to this problem??? the both end up being stuck in the send or receive state

this program send the first command

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

const int pinCE = 9;
const int pinCSN = 10;

float incomingArray[32];

unsigned long timeBetweenDose = 1200;
unsigned long marker3 = 0;
unsigned long wirelessUpdateInterval = 500;
unsigned long wirelessUpdateMarker = 0;

int rfState = 1;

float command = 6.65;
float sendStatus = 8888.5555; //command3
float sendStatusValue = 0.00;
unsigned long marker = 0;

RF24 wirelessSPI(pinCE, pinCSN);
const uint64_t pAddress = 0xB00B1E5000LL;

void setup()
{
  Serial.begin(9600);

  wirelessSPI.begin();
  wirelessSPI.setAutoAck(1);
  wirelessSPI.enableAckPayload();
  wirelessSPI.setRetries(5, 15);
  wirelessSPI.openReadingPipe(1, pAddress);
  wirelessSPI.openWritingPipe(pAddress);
  wirelessSPI.stopListening();
  wirelessSPI.printDetails();
  Serial.println("hello");
}

void loop() {
  if (rfState == 2) {
    wirelessSPI.startListening();
    while (wirelessSPI.available()) {
     // if (millis() - wirelessUpdateMarker > wirelessUpdateInterval) {
        wirelessSPI.read( &incomingArray, sizeof(incomingArray) );
        for (byte i = 0; i < 4; i++) {
          Serial.println(incomingArray[i]);
        //  rfState = 1;
        }

        for (byte x = 0; x < 1; x++) {
          float incomingCommand = float(incomingArray[x]);
          wirelessUpdateMarker = millis();
          if (incomingCommand == 2233.1122) {
            for (byte y = 1; y < 2; y++) {
              float commandValue = float(incomingArray[y]);
       
              Serial.print("incoming New Value: ");;
                rfState = 1;
            }
          }
        }
      }
    }
  //}
  if (rfState == 1) {
    float Value = 88.88;
    float Array[2] = {Value, command};
    // Array[1] = interger;
    wirelessSPI.stopListening();
    if (!wirelessSPI.write( &Array, sizeof(Array))) {
      Serial.println("delivery failed");
    }
    else {
      Serial.println("Send successful.");
      rfState = 2;
      // command = command + 100;
      // }
    }
  }
}

this program receives then responds.

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

const int pinCE = 9;
const int pinCSN = 10;

int state = 1;



unsigned long wirelessUpdateInterval = 500;
unsigned long wirelessUpdateMarker = 0;




float sendStatus = 8888.5555; //command3
float sendStatusValue = 0.00;
float incomingArray[32];
float returnStatus = 88.88;



unsigned long sendStatusMarker =0;
RF24 wirelessSPI(pinCE, pinCSN);
const uint64_t pAddress = 0xB00B1E5000LL;

void setup()
{
  Serial.begin(9600);
  wirelessSPI.begin();
  wirelessSPI.setAutoAck(1);
  wirelessSPI.enableAckPayload();
  wirelessSPI.setRetries(5, 15);
  wirelessSPI.openReadingPipe(1, pAddress);
  wirelessSPI.openWritingPipe(pAddress);
  wirelessSPI.startListening();
  wirelessSPI.printDetails();
  Serial.println("hello");
  pinMode(4, INPUT_PULLUP);
}

void loop() {

   if (state == 2){
  wirelessSPI.stopListening();
  sendStatusValue = 2233.1122;
   float Array[2] = {sendStatusValue};
  if (!wirelessSPI.write( &Array, sizeof(Array))) {
    Serial.println("delivery failed");
    //sendStatusNow();
  }
  else {
    Serial.println("Send successful.");
    state =  1;
  }
  sendStatusMarker = millis();
 }


if (state == 1){
    wirelessSPI.startListening();
  while (wirelessSPI.available()) {
   wirelessSPI.startListening();
    if (millis() - wirelessUpdateMarker > wirelessUpdateInterval) {
      wirelessSPI.read( &incomingArray, sizeof(incomingArray) );
      for (byte i = 0; i < 4; i++) {
        Serial.println(incomingArray[i]);
         // state = 2;
      }

      for (byte x = 0; x < 1; x++) {
        float incomingCommand = float(incomingArray[x]);
        wirelessUpdateMarker = millis();
        if (incomingCommand == returnStatus) {
          for (byte y = 1; y < 2; y++) {
            float commandValue = float(incomingArray[y]);
     
            Serial.print("sent status reply ");
            state = 2;

            delay(1000);
          //  sendStatusNow();

          }
        }
        }
    }
    }
}
}

Hi,

NRF24L01 Arduino Library examples are quite difficult to understand as they have added codes for 2 way communication. I have used this simple code (copied from NRF24L01 Arduino Interfacing), you should try it out:

Code for Transmitter:

    #include <SPI.h>
    #include "nRF24L01.h"
    #include "RF24.h"
 
    RF24 radio(9,10);
 
    const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
    unsigned long Command = 1;
    void setup()
    {
    Serial.begin(57600);
 
    radio.begin();
    radio.setRetries(15,15);
    radio.openReadingPipe(1,pipes[1]);
    radio.startListening();
    radio.printDetails();
    radio.openWritingPipe(pipes[0]);
    radio.openReadingPipe(1,pipes[1]);
    radio.stopListening();
    }
 
    void loop(void)
    {
    radio.stopListening();
 
    radio.write( &Command, sizeof(unsigned long) );
 
    radio.startListening();
 
    delay(1000);
    }

Code for Receiver:

#include <SPI.h>
    #include "nRF24L01.h"
    #include "RF24.h"

    RF24 radio(9,10);

    const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

    typedef enum { role_ping_out = 1, role_pong_back } role_e;

    const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};

    role_e role = role_pong_back;

    void setup(void)
    {
    Serial.begin(57600);
    radio.begin();
    radio.setRetries(15,15);
    radio.openReadingPipe(1,pipes[1]);
    radio.startListening();
    radio.printDetails();
    radio.openWritingPipe(pipes[1]);
    radio.openReadingPipe(1,pipes[0]);
    radio.startListening();
    }

    void loop(void)
    {

    if ( radio.available() )
    {
    unsigned long data = 0;
    radio.read( &data, sizeof(unsigned long) );
    Serial.println(data);

    delay(20);
    }
    }

Now open your Serial Terminals for both transmitter & receiver & set their baud rates to 57600.

Now whatever you write in Transmitter Serial Monitor will appear in Receiving Serial Monitor.

Thanks.

ive tried to simplify the code a little more but i still cannot figure out where im going wrong. the results are strange i get delivery failed most of the time but sometimes they both end up in delivery mode and then one of them ends up leaving delivery mode sometimes it works. someone please tell me whats going on here

sending code

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

const int pinCE = 9;
const int pinCSN = 10;

float incomingArray[32];
float Value = 88.88;
unsigned long marker3 = 0;
unsigned long wirelessUpdateInterval = 500;
unsigned long wirelessUpdateMarker = 0;
unsigned long marker =0;
int rfState = 1;

float command = 6.65;
float sendStatus = 8888.5555; //command3
float sendStatusValue = 0.00;
unsigned long marker2 = 0;

RF24 wirelessSPI(pinCE, pinCSN);
const uint64_t pAddress = 0xB00B1E5000LL;

void setup()
{
  Serial.begin(9600);

  wirelessSPI.begin();
  wirelessSPI.setAutoAck(1);
  wirelessSPI.enableAckPayload();
  wirelessSPI.setRetries(5, 15);
  wirelessSPI.openReadingPipe(1, pAddress);
  wirelessSPI.openWritingPipe(pAddress);
  wirelessSPI.stopListening();
  wirelessSPI.printDetails();
  Serial.println("hello");
}

void loop() {
if (millis() - marker2 > 2000){
  if (digitalRead(6) == LOW){
    requestStatus() ;
  }
     marker2 = millis();
}
}

void readResponse() {
  wirelessSPI.startListening();
  while (wirelessSPI.available()) {
    wirelessSPI.read( &incomingArray, sizeof(incomingArray) );
    for (byte i = 0; i < 4; i++) {
      Serial.println(incomingArray[i]);
    }
  }
}

void requestStatus() {
  float Array[2] = {Value};
  // Array[1] = interger;
  wirelessSPI.stopListening();
  if (!wirelessSPI.write( &Array, sizeof(Array))) {
    Serial.println("delivery failed");
    marker = millis();
  }
  else {
    Serial.println("Send successful.");
    readResponse();
  }

}

receiving code

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

const int pinCE = 9;
const int pinCSN = 10;
int state = 1;
unsigned long wirelessUpdateInterval = 500;
unsigned long wirelessUpdateMarker = 0;
float sendStatus = 8888.5555;
float sendStatusValue = 0.00;
float incomingArray[32];
float returnStatus = 88.88;
unsigned long marker;
unsigned long sendStatusMarker = 0;
RF24 wirelessSPI(pinCE, pinCSN);
const uint64_t pAddress = 0xB00B1E5000LL;

void setup()
{
  Serial.begin(9600);
  wirelessSPI.begin();
  wirelessSPI.setAutoAck(1);
  wirelessSPI.enableAckPayload();
  wirelessSPI.setRetries(5, 15);
  wirelessSPI.openReadingPipe(1, pAddress);
  wirelessSPI.openWritingPipe(pAddress);
  wirelessSPI.startListening();
  wirelessSPI.printDetails();
  Serial.println("hello");
}

void loop() {
  if (state == 2) {
    wirelessSPI.stopListening();
    sendStatusValue = 2233.1122;
    float Array[2] = {sendStatusValue};
    if (!wirelessSPI.write( &Array, sizeof(Array))) {
      Serial.println("delivery failed");
    }
    else {
      Serial.println("Send successful.");
     state = 1;
    }
    sendStatusMarker = millis();
  }

  if (state == 1) {
    wirelessSPI.startListening();
    while (wirelessSPI.available()) {
      wirelessSPI.startListening();
      wirelessSPI.read( &incomingArray, sizeof(incomingArray) );
      for (byte i = 0; i < 4; i++) {
        Serial.println(incomingArray[i]);
      }

      for (byte x = 0; x < 1; x++) {
        float incomingCommand = float(incomingArray[x]);

        if (incomingCommand == returnStatus) {
          for (byte y = 1; y < 2; y++) {
            float commandValue = float(incomingArray[y]);
            Serial.print("sent status reply ");
            state = 2;

          }
        }
      }
    }
  }
}

i ended up coming up with this sorta working version. if anyone knows how to improve this let me know thanks

sending code

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

const int pinCE = 9;
const int pinCSN = 10;

float incomingArray[32];
float Value = 88.88;
unsigned long marker3 = 0;
unsigned long wirelessUpdateInterval = 500;
unsigned long wirelessUpdateMarker = 0;
unsigned long marker =0;
unsigned long sendStateMarker = 0;
int rfState = 1;
int sendState = 1;
float command = 6.65;
float sendStatus = 8888.5555; //command3
float sendStatusValue = 0.00;
unsigned long marker2 = 0;

RF24 wirelessSPI(pinCE, pinCSN);
const uint64_t pAddress = 0xB00B1E5000LL;

void setup()
{
  Serial.begin(9600);

  wirelessSPI.begin();
  wirelessSPI.setAutoAck(1);
  wirelessSPI.enableAckPayload();
  wirelessSPI.setRetries(5, 15);
  //wirelessSPI.openReadingPipe(1, pAddress);
  wirelessSPI.openWritingPipe(pAddress);
  wirelessSPI.stopListening();
  Serial.println("hello");
}

void loop() {
if (sendState == 1){
  readResponse();
 if (millis() - sendStateMarker > 5000){
  sendState = 0;
  requestStatus();
 }
}
}

void readResponse() {
  wirelessSPI.startListening();
  while (wirelessSPI.available()) {
    wirelessSPI.read( &incomingArray, sizeof(incomingArray) );
    for (byte i = 0; i < 4; i++) {
      Serial.println(incomingArray[i]);
      Serial.println("hi");
    }
  }
}

void requestStatus() {
   wirelessSPI.stopListening();
  float Array[1] = {Value};
//  Array[1] = interger;
  if (!wirelessSPI.write( &Array, sizeof(Array))) {
    Serial.println("delivery failed");
 requestStatus();
  }
  else {
    Serial.println("Send successful.");
    sendStateMarker = millis();
    sendState = 1;
  }

}

receiving code

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int sendState = 0;
const int pinCE = 9;
const int pinCSN = 10;
int rcvState = 1;
unsigned long wirelessUpdateInterval = 500;
unsigned long wirelessUpdateMarker = 0;
unsigned long respondDelay = 0;
float sendStatusValue = 0.00;
float incomingArray[32];
float returnStatus = 88.88;
unsigned long marker;
unsigned long sendStatusMarker = 0;
RF24 wirelessSPI(pinCE, pinCSN);
const uint64_t pAddress = 0xB00B1E5000LL;

void setup()
{
  Serial.begin(9600);
  wirelessSPI.begin();
  wirelessSPI.setAutoAck(1);
  wirelessSPI.enableAckPayload();
  wirelessSPI.setRetries(5, 15);
  wirelessSPI.openReadingPipe(1, pAddress);
  //wirelessSPI.openWritingPipe(pAddress);
  wirelessSPI.startListening();
  Serial.println("hello");
}

void loop() {
  if (rcvState == 1) {
    readIncoming();
  }
  
  if (sendState == 1) {
    if (millis() - respondDelay > 3000) {
      sendStatus();
      Serial.println("response sent");
      sendState = 0;
    }
  }
}

void readIncoming() {
  wirelessSPI.startListening();
  while (wirelessSPI.available()) {
    wirelessSPI.read( &incomingArray, sizeof(incomingArray) );
    for (byte i = 0; i < 4; i++) {
      Serial.println(incomingArray[i]);
    }
    for (byte x = 0; x < 1; x++) {
      float incomingCommand = float(incomingArray[x]);
      if (incomingCommand == returnStatus) {
        for (byte y = 1; y < 2; y++) {
          float commandValue = float(incomingArray[y]);
          Serial.print("Got Status Command Waiting to send response ");
          respondDelay = millis();
          sendState = 1;

        }
      }
    }
  }
}

void sendStatus() {
  wirelessSPI.stopListening();
  sendStatusValue = 2233.1122;
  float Array[1] = {sendStatusValue};
  if (!wirelessSPI.write( &Array, sizeof(Array))) {
    Serial.println("delivery failed");
  }
  else {
    Serial.println("Send successful.");
  }
}

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

...R