Arduino + TF Mini

Hello,

I have been testing code for my TF-Mini. It currently works and shows on the Serial.Plotter.

I was wondering if i can then use this Serial.Plotter to room map in 2 dimensions?

So that when i move around the room with the tf-mini it maps accordingly.

Any help is appreciated.

#include <SoftwareSerial.h>  //header file of software serial port
#include <Servo.h>
#define TRIGGER_PIN A1
#define ECHO_PIN A0
#define MAX_DISTANCE 400
const int LS = A2; //set Left Sensor to digital pin 2
const int RS = A3; //set Right Sensor to digital pin 3
Servo servo;
SoftwareSerial Serial1(8, 9); //define software serial port name as Serial1 and define pin8 as RX and pin9 as TX
SoftwareSerial mySerial(11, 10);
//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()
{
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial1.begin(115200);	//set bit rate of serial port connecting LiDAR with Arduino
  servo.attach(12);
  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 Forward()
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enA, 150); //enA speed variable - Right Wheel
	analogWrite(enB, 150); //enB speed variable - Left Wheel
}
void TurnLeft()
{
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enA, 100); //enA speed variable - Right Wheel
	analogWrite(enB, 100); //enB speed variable - Left Wheel
}
void TurnRight()
{
  digitalWrite(in1, HIGH);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, HIGH); 
  analogWrite(enA, 100); //enA speed variable - Right Wheel
	analogWrite(enB, 100); //enB speed variable - Left Wheel
}
void Stop()
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enA, 100); //enA speed variable - Right Wheel
	analogWrite(enB, 100); //enB speed variable - Left Wheel
}

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
        {
          int angle = 0;
          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');
          //Bluetooth Module - HC-05
          if (Serial1.available())
          {
            mySerial.println(dist);
          }
        }
      }
    }
  }
  //Subroutine Statements
  if ((digitalRead(RS) == 0) && (digitalRead(LS) == 0)) {
    Forward(); //if statement for forward
  }
  if ((digitalRead(RS) == 0) && (digitalRead(LS) == 1)) {
    TurnRight(); //if statement for TurnRight
  }
  if ((digitalRead(RS) == 1) && (digitalRead(LS) == 0)) {
    TurnLeft(); //if statement for TurnLeft
  }
  if ((digitalRead(RS) == 1) && (digitalRead(LS) == 1)) {
    Stop(); //if statement for Stop
  }
  if (distance < 25){
    Stop(); //if statement for Ultrasonic Stop
  }
}

The accumulated distance data could be used to map the room in 2D, but I doubt the serial plotter would be useful to display the result.

More information would be required to make the map, though, such as the location of the distance sensor, and the direction it is pointing when it makes a measurement.

Hello.

Thank you for the fast reesponse.

What would be a suitable display that you know of?

I looking into the Benewake App for TF-Mini currently.

Kind Regards

Any bitmapped graphics display could display a map. Adafruit has the best selection.

Hello.

Do you know of any graphical display projects which can be shown using arduino?

I thought about exporting to MATLAB or other software.

Kind Regards

There are countless examples graphical display projects on line, and Adafruit has tutorials for all of their displays.

I will research this further.

Thank you for the help.

Have a nice day.

Kind Regards.

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