Nrf24l01 if else dont work

TX.ino

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

#define CE_PIN   7
#define CSN_PIN 8

const uint64_t slaveID[2] = {0xE8E8F0F0E1LL, 0xE8E8F0F0E2LL} ;
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

const byte numChars = 32;
char  dataToSend[numChars];
boolean newData = false;

float ackMessg[2]={1,2};
byte ackMessgLen = 4; // NB this 4 is the number of bytes in the 2 ints that will be recieved

void setup() {

    Serial.begin(115200);
    //Serial.println("Track Control Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.enableAckPayload();
    radio.setRetries(3,5); // delay, count
    pinMode(2, OUTPUT);
}

void loop() {
    
    recvWithEndMarker();  
    if (newData=true){
      radio.openWritingPipe(slaveID[0]); // calls the first slave
      bool rslt;
      rslt = radio.write( dataToSend, sizeof(dataToSend) );
      if ( radio.isAckPayloadAvailable() ) {
        digitalWrite(2, HIGH);   // turn the LED on (HIGH is the voltage level)
        radio.read(ackMessg,ackMessgLen);
        Serial.print("Transmit Success :");
        Serial.println(dataToSend);
        newData = false;
        digitalWrite(2, LOW);
        }
       else {
        Serial.println("Fail");}     
        }
}



void recvWithEndMarker() {
 static byte ndx = 0;
 char endMarker = '\n';
 char rc;

           while (Serial.available() > 0 && newData == false) {
 rc = Serial.read();

 if (rc != endMarker) {
 dataToSend[ndx] = rc;
 ndx++;
 if (ndx >= numChars) {
 ndx = numChars - 1;
 }
 }
 else {
 dataToSend[ndx] = '\0'; // terminate the string
 ndx = 0;
 newData = true;
 }
 }
}

RX.ino

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

#define CE_PIN   7
#define CSN_PIN 8

const uint64_t   deviceID = 0xE8E8F0F0E1LL; // Define the ID for this slave

RF24 radio(CE_PIN, CSN_PIN);
char  dataReceived[32];
char  ackData[2] = {10,21};
void setup() {
    Serial.begin(115200);
    Serial.println("Hand Controller Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1,deviceID);
    radio.enableAckPayload();
    radio.writeAckPayload(1, ackData, sizeof(ackData));
    radio.startListening();
}

void loop() {
    if ( radio.available() ) {
        radio.read( dataReceived, sizeof(dataReceived) );
        for (int x = 0; x <32; x++) {
        Serial.print(dataReceived[x]);}
        Serial.println();
        radio.writeAckPayload(1, ackData, sizeof(ackData));        
    }
}

Serial Output :

22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :
22:42:37.480 → Transmit Success :

What I want to do in the program is that after confirming the sending of the message, I make the newData value false, and the loop starts to wait for the data again, it should be like this, but it is not like this, it continues to send data without waiting for the new data. Can someone help, I need it so much

if (newData=true){
= for assignment
== for comparison

1 Like

You saved my day thank youu

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.