LCD+nRF24L01+Arduino

Hello,

Problem sending serial data to other arduino.

Any help is appreciated.

Transmitter Code:

#include <SoftwareSerial.h>  //header file of software serial port
#include <Servo.h>
#include <NewPing.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define TRIGGER_PIN A1
#define ECHO_PIN A0
#define MAX_DISTANCE 200
Servo servo;
SoftwareSerial Serial1(8,9); //define software serial port name as Serial1 and define pin8 as RX and pin9 as TX
RF24 radio(11, 12); // CE, CSN
const byte address[1] = "00001";
//Ultrasonic Variables
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
boolean object;
float time = 0;
long duration, cm;
int distance = 0; //set distance value to 0 
int pos = 0;
// Motor A connections
int enA = 7;
int in1 = 5;
int in2 = 6;
// Motor B connections
int enB = 2;
int in3 = 3;
int in4 = 4;
// Lidar Variables
int dist;	//actual distance measurements of LiDAR
int strength;	//signal strength of LiDAR
int check;	//save check value
int i;
int uart[11];	//save data measured by LiDAR
const int HEADER=0x59;	//frame header of data package


void setup() 
{
  //
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();
  //
  Serial.begin(9600);
  Serial1.begin(115200);	//set bit rate of serial port connecting LiDAR with Arduino
  servo.attach(10);
	pinMode(in1, OUTPUT);
	pinMode(in2, OUTPUT);
	pinMode(in3, OUTPUT);
	pinMode(in4, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(TRIGGER_PIN,OUTPUT); //sets TRIGGER_PIN to OUTPUT
  pinMode(ECHO_PIN,INPUT); //sets ECHO_PIN to INPUT

  digitalWrite(in1, LOW);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, LOW);

}

void loop() 
//Lidar Code - TF-Mini
{
    if (Serial1.available())                //check if serial port has data input
  {
    if (Serial1.read() == HEADER)        //assess data package frame header 0x59
    {
      uart[0] = HEADER;
      if (Serial1.read() == HEADER)      //assess data package frame header 0x59
      {
        uart[1] = HEADER;
        for (i = 2; i < 9; i++)         //save data in array
        {
          uart[i] = Serial1.read();
        }
        check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
        if (uart[8] == (check & 0xff))        //verify the received data as per protocol
        {
          dist = uart[2] + uart[3] * 256;     //calculate distance value
          strength = uart[4] + uart[5] * 256; //calculate signal strength value
          Serial.print("distance = ");
          Serial.print(dist);                 //output measure distance value of LiDAR
          Serial.print('\t');
          Serial.print("strength = ");
          Serial.print(strength);             //output signal strength value
          Serial.print('\n');
        }
      }
    }
  }

radio.write(&dist, sizeof(dist));

//Get Ultrasonic Distance
float duration, distance;
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration / 2) / 29.1 ; // calculate distance in CM

   if(distance >= 30) //Move Forward 
  {
	digitalWrite(in1, LOW);
	digitalWrite(in2, HIGH);
	digitalWrite(in3, LOW); 
	digitalWrite(in4, HIGH);
  analogWrite(enA, 130); //enA speed variable - Right Wheel
	analogWrite(enB, 130); //enB speed variable - Left Wheel
  delay(200);
  }
  if(distance <= 25) //Move Right
  {
  digitalWrite(in1, HIGH);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, HIGH); 
  analogWrite(enA, 170); //enA speed variable - Right Wheel
	analogWrite(enB, 130); //enB speed variable - Left Wheel
  delay(200);
  }
  if(distance < 20) //Move Backward
  {
	digitalWrite(in1, HIGH); 
	digitalWrite(in2, LOW); 
	digitalWrite(in3, HIGH); 
	digitalWrite(in4, LOW); 
        analogWrite(enA, 130); //enA speed variable - Right Wheel
	analogWrite(enB, 130); //enB speed variable - Left Wheel
       delay(200);
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////

Reciever Code:

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

RF24 radio(9, 8); // CE, CSN
const byte address[1] = "00001";
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() 
{
  lcd.begin(16, 2);
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop()
{
  lcd.setCursor(0, 0);
  if (radio.available()) {
    char text[32] = {0};
    radio.read(&text, sizeof(text));
      lcd.print("Distance= ");
      lcd.print(text);
      delay(500);
}
}

Reciever prints Distance= - but does not recieve the distance from the sensor which is sent by the transmitter.

Thank You.`

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

1 Like

You send a single int, and try to receive a char array.
Try receiving an int.

You would probably have an answer to your question had you followed the forum guidelines. Your code is just gibberish. The forum guidelines request you post your code using code tags. Doing so makes it readable for all of us that can help you.

Your topic has been moved to a more suitable location on the forum as you don't seem to have a problem with IDE 1.x.

Hello,

Thank you for the fast response.

I would therefore change the Reciever Code from

radio.read(&text, sizeof(text));

to what?

Sorry not had to use the NRF24I01 before.

Kind Regards

I probably only support laziness,
but I feel for you if you are really not able to find a solution on your own.

  int dist;
  radio.read(&dist, sizeof(dist));

Hello when i have done

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

RF24 radio(9, 8); // CE, CSN
const byte address[1] = "00001";
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() 
{
  lcd.begin(16, 2);
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop()
{
  lcd.setCursor(0, 0);
  if (radio.available()) {
    char text[32] = {0};
    int dist;
    radio.read(&dist, sizeof(dist));
      lcd.print("Distance= ");
      lcd.print(dist);
      delay(500);
}
}

Now the LCD does not print anything.

Is this therefore a wiring issue with the nrf24l01?

Any help is appreciated.

Kind Regards

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