Hello,
Working on a project for university a LIDAR Robot which is capable of mapping a room.
I have started by creating a receiver using an Arduino uno with an LCD mounted to a breadboard.
The LIDAR Robot also uses an Arduino Uno with a TF-Mini S as the LIDAR Sensor and both components use nrf24l01 rf to transmit and receive.
I have coded the LIDAR Robot and reciever.
Each component works separately but not combined.
Code Shown Below:
// LIDAR ROBOT
#include <SoftwareSerial.h>
#include "TFMini.h"
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <Servo.h> // Library used for servo motors with predefined functions and methods
TFMini tfmini;
int E1 = 0; //Enable1 set to digital pin 0 - Front Left Wheel
int M1 = 1; //Motor1 set to digital pin 1 - Front Left Wheel
int E2 = 2; //Enable2 set to digital pin 2 - Front Right Wheel
int M2 = 3; //Motor2 set to digital pin 3 - Front Right Wheel
int E3 = 4; //Enable3 set to digital pin 4 - Back Left Wheel
int M3 = 5; //Motor3 set to digital pin 5 - Back Left Wheel
int E4 = 6; //Enable4 set to digital pin 6 - Back Right Wheel
int M4 = 7; //Motor4 set to digital pin 7 - Back right Wheel
int Distance = 0; //set distance value to 0
int Strength = 0; //set strength value to 0
int pos = 0; // variable to store the servo position
boolean object;
SoftwareSerial Serial0(12, 13);
float time = 0;
long duration, cm;
int i;
int Check;
int data = 0;
int uart[10]; //save data measured by LiDAR
const int HEADER = 0x59; //frame header of data package
RF24 radio(9, 8); // CE, CSN
const byte address[6] = "00001";
Servo myservo;
void setup()
{
Serial.begin(9600); //set bit rate of serial port connecting Arduino with computer
Serial0.begin(115200); //set bit rate of serial port connecting LiDAR with Arduino
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MAX);
myservo.attach(10); // attach the servo to our servo object
myservo.write(98);
pinMode(M1, OUTPUT); //sets Left Motor to OUTPUT
pinMode(M2, OUTPUT); //sets Right Motor to OUTPUT
pinMode(E1, OUTPUT); //sets Left Enable to OUTPUT
pinMode(E2, OUTPUT); //sets Right Enable to OUTPUT
pinMode(M3, OUTPUT); //sets Left Motor to OUTPUT
pinMode(M4, OUTPUT); //sets Right Motor to OUTPUT
pinMode(E3, OUTPUT); //sets Left Enable to OUTPUT
pinMode(E4, OUTPUT); //sets Right Enable to OUTPUT
}
void loop() {
radio.stopListening ();
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
if (Serial0.available()) //check if serial port has data input
{
if (Serial0.read() == HEADER) //assess data package frame header 0x59
{
uart[0] = HEADER;
if (Serial0.read() == HEADER) //assess data package frame header 0x59
{
uart[1] = HEADER;
for (i = 2; i < 9; i++) //save data in array
{
uart[i] = Serial0.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
{
Distance = uart[2] + uart[3] * 256; //calculate distance value
Strength = uart[4] + uart[5] * 256; //calculate signal strength value;
Serial.print("Distance = ");
Serial.print(Distance);
Serial.print('\t');
Serial.print("Strength = ");
Serial.print(Strength);
Serial.print('\n');
{
radio.write(&data, sizeof(data));
Serial.print("Distance = ");
Serial.println(Distance);
delay(400);
}
}
}
}
}
}
}
#Receiver
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <LiquidCrystal.h>
RF24 radio(8, 9); // CE, CSN
const byte address[6] = "00001";
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int txt = 0;
void setup()
{
while (!Serial);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, address);
lcd.begin(16, 2);
}
void loop()
{
radio.startListening();
{
if (radio.available())
{
while (radio.available())
{
if (Serial.available()) {
radio.read(&txt, sizeof(txt));
lcd.clear();
lcd.write("Distance = ");
while (Serial.available() > 0) {
lcd.write(Serial.read());
}
}
}
}
}
}
Thank you for reading.
Any Help is Appreciated ![]()