NRF sensor  - Page 1
Forum section: electronics / rf-microwave

author: radhika
author_rank: Regular Contributor
timestamp: May 07, 2020, 10:44:37 am
content: Hello,I am working on communication project. where I used Arduino Mega, 16*2 LCD and NRF sensor. When I check it on the circuit, I am not getting anything. It just read the value detected by ultrasonic sensor.I am not sure if this code is write or not. Please guide me to solve it. Aim: Nrf is acting as transmitter as well as receiver both in this project.TransmitterCode:#include <LiquidCrystal.h>#include <SPI.h>#include <nRF24L01.h>#include <RF24.h>#define MAX_DISTANCE 400 // Maximum distance (in cm) to ping.#define CE_PIN 22#define CSN_PIN 24LiquidCrystal lcd(8,9,4,5,6,7);RF24 radio(22, 24); // CE, CSNconst byte addresses[] = {"00001", "00002"};char dataToSendforcollision1[] = "Collision Alert";char dataToSendforcollision2[] = "Lower the speed!";char dataToSendforovertake1[] = " Car Overtaking ";char dataToSendforovertake2[] = "Plan to overtake";char dataReceived; // this must match dataToSend in the TX//ultrasonic 1int echoPin1 =47;int initPin1 =46;int distance1 =0;//ultrasonic 2int echoPin2 =49;int initPin2 =48;int distance2 =0;//ultrasonic 3int echoPin3 =44;int initPin3 =45;int distance3 =0;void setup() {lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.lcd.clear();lcd.setCursor(0,0); // set cursor to column 0, row 1lcd.print("Dist(cm): "); // Writing on LCDpinMode(initPin1, OUTPUT);pinMode(echoPin1, INPUT);pinMode(initPin2, OUTPUT);pinMode(echoPin2, INPUT);pinMode(initPin3, OUTPUT);pinMode(echoPin3, INPUT);Serial.begin(9600);radio.begin();radio.setDataRate( RF24_250KBPS );radio.openWritingPipe(addresses); // 00002radio.openReadingPipe(1, addresses); // 00001radio.setPALevel(RF24_PA_MIN);}void loop() {distance1 = getDistance(initPin1, echoPin1);lcd.setCursor(12,0); // set cursor to column 12, row 0 (the first row)lcd.print("L: "); // change this text to whatever you like. keep it clean.lcd.setCursor(14,0);lcd.print(distance1);delay(10);distance2 = getDistance(initPin2, echoPin2);lcd.setCursor(0,1); // set cursor to column 0, row 1 (the first row)lcd.print("F: "); // change this text to whatever you like. keep it clean.lcd.setCursor(3,1);lcd.print(distance2);delay(10);distance3 = getDistance(initPin3, echoPin3);lcd.setCursor(12,1); // set cursor to column 12, row 0 (the first row)lcd.print("R: "); // change this text to whatever you like. keep it clean.lcd.setCursor(14,1);lcd.print(distance3);delay(10);send(); // to get things startedgetData();}int getDistance (int initPin, int echoPin){digitalWrite(initPin, HIGH);delayMicroseconds(10);digitalWrite(initPin, LOW);unsigned long pulseTime = pulseIn(echoPin, HIGH);int distance = pulseTime/58;return distance;}//====================void send() {radio.stopListening();//Front Sensor for Safe distance and collision alertif(distance1 < 50){ radio.write( &dataToSendforcollision1, sizeof(dataToSendforcollision1) );} elseif( (distance1 >= 50) && (distance1 <= 100) ){ radio.write( &dataToSendforcollision2, sizeof(dataToSendforcollision2 ) );}//Left Sensor detect the overtaking situation by carif(distance2 < 30){ radio.write( &dataToSendforovertake1, sizeof(dataToSendforovertake1) );} elseif( (distance2 >=30) && (distance2 <= 60) ){radio.write( &dataToSendforcollision2, sizeof(dataToSendforcollision2) );}//Right Sensor detect the overtaking situation by carif(distance3 < 30){ radio.write( &dataToSendforovertake1, sizeof(dataToSendforovertake1) );} elseif( (distance3>=50) && (distance3<= 80) ){ radio.write( &dataToSendforcollision2, sizeof(dataToSendforcollision2) );}}//get data ==============void getData() {if (radio.available()) {radio.read(&dataReceived, sizeof(dataReceived));lcd.clear();lcd.setCursor(0,0);lcd.print(dataReceived); // change this text to whatever you like. keep it clean.delay(500);}}ReceiverCode:#include <LiquidCrystal.h>#include <SPI.h>#include <nRF24L01.h>#include <RF24.h>#define MAX_DISTANCE 400 // Maximum distance (in cm) to ping.#define CE_PIN 22#define CSN_PIN 24LiquidCrystal lcd(8,9,4,5,6,7);RF24 radio(22, 24); // CE, CSNchar dataReceived; // this must match dataToSend in the TXconst byte addresses[] = {"00001", "00002"};char dataToSendforcollision1[] = "Collision Alert";char dataToSendforcollision2[] = "Lower the speed!";char dataToSendforovertake1[] = " Car Overtaking ";char dataToSendforovertake2[] = "Plan to overtake";//ultrasonic 1int echoPin1 =47;int initPin1 =46;int distance1 =0;//ultrasonic 2int echoPin2 =49;int initPin2 =48;int distance2 =0;//ultrasonic 3int echoPin3 =44;int initPin3 =45;int distance3 =0;void setup(){lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.lcd.clear();lcd.setCursor(0,0); // set cursor to column 0, row 1lcd.print("Dist(cm): "); // Writing on LCDpinMode(initPin1, OUTPUT);pinMode(echoPin1, INPUT);pinMode(initPin2, OUTPUT);pinMode(echoPin2, INPUT);pinMode(initPin3, OUTPUT);pinMode(echoPin3, INPUT);Serial.begin(9600);radio.begin();radio.openWritingPipe(addresses); // 00001radio.openReadingPipe(1, addresses); // 00002radio.setPALevel(RF24_PA_MIN);}void loop(){distance1 = getDistance(initPin1, echoPin1);lcd.setCursor(12,0); // set cursor to column 12, row 0 (the first row)lcd.print("L: "); // change this text to whatever you like. keep it clean.lcd.setCursor(14,0);lcd.print(distance1);delay(10);distance2 = getDistance(initPin2, echoPin2);lcd.setCursor(0,1); // set cursor to column 0, row 1 (the first row)lcd.print("F: "); // change this text to whatever you like. keep it clean.lcd.setCursor(3,1);lcd.print(distance2);delay(10);distance3 = getDistance(initPin3, echoPin3);lcd.setCursor(12,1); // set cursor to column 12, row 0 (the first row)lcd.print("R: "); // change this text to whatever you like. keep it clean.lcd.setCursor(14,1);lcd.print(distance3);delay(10);getData();send();}// get distance ===========int getDistance (int initPin, int echoPin){digitalWrite(initPin, HIGH);delayMicroseconds(10);digitalWrite(initPin, LOW);unsigned long pulseTime = pulseIn(echoPin, HIGH);int distance = pulseTime/58;return distance;}//get data ==============void getData() {if (radio.available()) {radio.read(&dataReceived, sizeof(dataReceived));lcd.clear();lcd.setCursor(0,0);lcd.print(dataReceived); // change this text to whatever you like. keep it clean.delay(500);}}//send data =============//====================void send() {radio.stopListening();//Front Sensor for Safe distance and collision alertif(distance1 < 50){ radio.write( &dataToSendforcollision1, sizeof(dataToSendforcollision1) );} elseif( (distance1 >= 50) && (distance1 <= 100) ){ radio.write( &dataToSendforcollision2, sizeof(dataToSendforcollision2 ) );}//Left Sensor detect the overtaking situation by carif(distance2 < 30){ radio.write( &dataToSendforovertake1, sizeof(dataToSendforovertake1) );} elseif( (distance2 >=30) && (distance2 <= 60) ){radio.write( &dataToSendforcollision2, sizeof(dataToSendforcollision2) );}//Right Sensor detect the overtaking situation by carif(distance3 < 30){ radio.write( &dataToSendforovertake1, sizeof(dataToSendforovertake1) );} elseif( (distance3>=50) && (distance3<= 80) ){ radio.write( &dataToSendforcollision2, sizeof(dataToSendforcollision2) );}}