Help on using nRF24 transfer data

Hello everyone.

I am making a project that uses 2 arduino to transfer data (one asks and one answers). Right now i can make them work as i aspected. However there is a problem that i need to fix.

When i use the UNO (ask) to receive answer from the MEGA (answer) and send it on Serial, there is a blank line then my answer i want. I haved tried some value for delay() but they don't work. I want to remove the blank line so that i can use LabVIEW to read and process data correctly.

Code for UNO (Ask)

#include <SPI.h>
#include "printf.h"
#include "RF24.h"
#include "nRF24L01.h"
RF24 radio(9, 10);
uint8_t diachi[][6] = {"Kenh0","Kenh1"};
//const byte diachi[6]="12345";

void setup() {
  Serial.begin(9600);  // 
  Serial.println("Serial port is ready");
while (!radio.begin())
  { Serial.println("Hardware is error");
    }
    radio.openWritingPipe(diachi[0]); 
radio.openReadingPipe(1,diachi[1]);   
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(120);
radio.setDataRate(RF24_250KBPS);

if (!radio.available())
{
  Serial.println("Haven't connected yet");
  Serial.println("CONNECTING...");
  }
}
void loop() {
  radio.stopListening();
  if (Serial.available() > 0) {  
    int command = Serial.parseInt();  
    int abc1=1;int abc2=2;int abc3=3;
    
    if (command == 1) {
      Serial.println("1");
      radio.write(&abc1,sizeof(abc1));
    } else if (command == 2) {
      Serial.println("2");
      radio.write(&abc2,sizeof(abc2));
    } else if (command == 3) {
      Serial.println("3");
      radio.write(&abc3,sizeof(abc3));
    }
  
    delay(50);
  radio.startListening();
  byte nhan[20];

radio.read(&nhan, sizeof(nhan));
for (int i=0;i<20;i++){
Serial.print(nhan[i]);}
Serial.println();
    delay(100);
  }
}

Code for MEGA (answer)

#include <SPI.h>
#include "printf.h"
#include "RF24.h"
#include "nRF24L01.h"
RF24 radio(9, 53);
uint8_t diachi[][6] = {"Kenh0","Kenh1"};

void setup() {
  pinMode(A0, INPUT);
  Serial.begin(9600);
    while (!radio.begin())
  { Serial.println("Hardware is error");
    }
    radio.openWritingPipe(diachi[1]); 
radio.openReadingPipe(1,diachi[0]);   
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(120);
radio.setDataRate(RF24_250KBPS);

if (!radio.available())
{
  Serial.println("Haven't connected yet");
  Serial.println("CONNECTING...");
  }
}
void loop() {
  
radio.startListening();
while (!radio.available()) {}
int nhan=0;

radio.read(&nhan, sizeof(nhan));
//nhan[0]=int(nhan[0]);
Serial.println(nhan);
delay(100);
radio.stopListening();
byte abc1[20]={1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0};
byte abc2[12]={1,2,3,4,5,6,7,8,9,0,1,2}; 
byte abc3[2]={1,1};
if (nhan==1)
{radio.write(&abc1, sizeof(abc1));
for (int i=0;i<20;i++){
Serial.print(abc1[i]);
}
Serial.println();}
else if (nhan==2)
{
radio.write(&abc2, sizeof(abc2));
for (int i=0;i<12;i++){
Serial.print(abc2[i]);}
Serial.println();}
else if (nhan==3)
{
  radio.write(&abc3, sizeof(abc3));
  for (int i=0;i<2;i++){
Serial.print(abc3[i]);}
Serial.println();}
delay(100);
}

This is what i received on Serial mornitor

Thank you very much!

Welcome to the forum

In your code for the Uno you use Serial.println() after the for loop. This will add a blank line to the output

        for (int i = 0; i < 20; i++)
        {
            Serial.print(nhan[i]);
        }
        Serial.println();
        delay(100);

why is it there ?

1 Like

I use it to make a newline. I have deleted this code line but it doesn't working. New data will continue write on the same line with the old one.

I think that the first line 00000 is the array nhan[] had not received data from the line radio.read(&nhan, sizeof(nhan));. After that nhan[] received and show the data on the second line.

On the Uno side, you should probably wait for an answer back from the Mega instead of a simple delay.

I've modified the end of the loop but this is untested:

void loop() {
  radio.stopListening();
  if (Serial.available() > 0) {
    int command = Serial.parseInt();
    int abc1 = 1; int abc2 = 2; int abc3 = 3;

    if (command == 1) {
      Serial.println("1");
      radio.write(&abc1, sizeof(abc1));
    } else if (command == 2) {
      Serial.println("2");
      radio.write(&abc2, sizeof(abc2));
    } else if (command == 3) {
      Serial.println("3");
      radio.write(&abc3, sizeof(abc3));
    }

    // delay(50);
    radio.startListening();

    // wait max 2 seconds for a reply
    uint32_t start = millis() ;
    while (!radio.available() && millis() - start < 2000UL ) ;

    // if we have got something back
    if ( radio.available()) {
      byte nhan[20] = {0} ;
      radio.read(&nhan, sizeof(nhan));
      for (int i = 0; i < 20; i++) {
        Serial.print(nhan[i]);
      }
      Serial.println();
      delay(100);
    }
  }
}
1 Like

Hello vie_dkhnh

Welcome to the world's best Arduino forum ever.

I have been following the topic of using this type of wireless module and the associated software functions for some time now.

For wireless projects, I recommend using the HC12 module.

What are the advantages:

  1. no external functions from a library are needed.
  2. the development of a communication protocol is in your hands
  3. the necessary development steps can be programmed and tested without using the wireless module
  4. the radio module can be easily configured for the project requirements
  5. both transmitter and receiver are contained in one radio module.

hth

Have a nice day and enjoy coding in C++.

1 Like

Thank you for your advise. However my project's deadline is so close so it's unable to change the hardware. I will think about it in my next project.

1 Like

Thank you very much. I added your code to my ino file and it actually worked as i aspected.

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