Code For Lidar Lite V3 Using Arduino

Can anybody help me with my code to get my lidar lite v3 to flash led at different rates/ frequencies as the distance decreases.I am in need of help because the project deadline is soon aproching and I would be ever so greatful if someone would give me a hand

unsigned long pulseWidth;
#define LED 13

void setup() {
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  pinMode(3, INPUT);
  pinMode(LED, OUTPUT);
  #define distance (pulseWidth)
    if(pulseWidth != 0)
  {
    pulseWidth = pulseWidth / 10; 
    (distance <= 0);
    distance=0;}
  
}

void loop() {
{
  pulseWidth = pulseIn(3, HIGH);  
}

   if (distance=0)
    digitalWrite (LED,HIGH);
    
 
  else if (distance >= 3 && distance <= 50){
      digitalWrite (LED, HIGH);
    delay(50);
    digitalWrite (LED, LOW);
    delay(50);
  }
  else if (distance >= 51 && distance <= 100){
      digitalWrite (LED, HIGH);
    delay(100);
    digitalWrite (LED, LOW);
    delay(100);
  }
  else if (distance >= 101 && distance <= 200){
      digitalWrite (LED, HIGH);
    delay(200);
    digitalWrite (LED, LOW);
    delay(200);
  }
    else if (distance >= 201 && distance <= 300){
      digitalWrite (LED, HIGH);
    delay(300);
    digitalWrite (LED, LOW);
    delay(300);
  }
    else if (distance >= 301 && distance <= 400){
      digitalWrite (LED, HIGH);
    delay(400);
    digitalWrite (LED, LOW);
    delay(400);
  }
      else if (distance >= 400) {
      digitalWrite (LED, HIGH);
    delay(500);
    digitalWrite (LED, LOW);
    delay(500);
  }
  delay(50);
  }
  #define distance (pulseWidth)

What is this trying to do? What is the relationship between pulseWidth, that you have not given a meaningful value to, and distance?

    if(pulseWidth != 0)

But, it is...

    (distance <= 0);

Compare distance to 0, but do nothing if it is less than 0 and do nothing if it is greater than 0. So, why bother comparing?

{
  pulseWidth = pulseIn(3, HIGH); 
}

Useless curly braces.

   if (distance=0)

Some time studying the difference between the = and the == operators would be time well spent.

Since it appears that you want the LED on time and off time to be the same as the distance, why do you need so much code to make that happen?

sorry Im only 14 and new to arduino software and Im just trying to programe the sensor to flash an LED more rapidly as distance decreases but i am not having much sucsess while doing so. I would be greatful if you (PaulS) could put me on the path regards to making my code work

PaulS has pointed out where there are mistakes. He's also given you fairly big hints about what is wrong. Look at those lines and check your syntax?

Just finished my first working prototype . If you have any suggestions please leave a reply

unsigned long pulseWidth;

void setup()
{
  Serial.begin(115200); // Start serial communications
  
  pinMode(2, OUTPUT); // Set pin 2 as trigger pin
  digitalWrite(2, LOW); // Set trigger LOW for continuous read

  pinMode(3, INPUT); // Set pin 3 as monitor pin

  pinMode(13, OUTPUT);
}

void loop()
{
  pulseWidth = pulseIn(3, HIGH); // Count how long the pulse is high in microseconds
  pulseWidth = pulseWidth / 10;
  // If we get a reading that isn't zero, let's print it
  if(pulseWidth >= 51 && pulseWidth <= 100)
  { 
     // 10usec = 1 cm of distance
    Serial.println(pulseWidth); // Print the distance

  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
  }
  else if(pulseWidth >= 10 && pulseWidth <= 50)
  {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(50);
  }
}

Hello I am facing problem when connecting 2 TF mini lidar to arduino mega 2560, the program gets stuck and it is not working.

/*
The (UNO) circuit:
Green TX
White RX
Red +5V
Black Gnd

  • Uno RX is digital pin 10 (connect to TX of TF Mini)
  • Uno TX is digital pin 11 (connect to RX of TF Mini)
    */

#include <SoftwareSerial.h>
#include "TFMini.h"
#include <NewTone.h>

// Setup software serial port
SoftwareSerial mySerialone(10, 11); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
SoftwareSerial mySerialtwo(15,14); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini1;
TFMini tfmini2;

void setup() {
// Step 1: Initialize hardware serial port (serial debug port)
Serial.begin(9600);
// wait for serial port to connect. Needed for native USB port only
while (!Serial);

Serial.println ("Initializing...");
}

void loop() {
// Take one TF Mini distance measurement
mySerialone.begin(115200);
tfmini1.begin(&mySerialone);
uint16_t dist1 = tfmini1.getDistance();
uint16_t strength1 = tfmini1.getRecentSignalStrength();

// Display the measurement
Serial.print(dist1);
Serial.print(" cm sigstr: ");
Serial.println(strength1);

mySerialone.end();
delay(500);
// Take one TF Mini distance measurement
mySerialtwo.begin(115200);
tfmini2.begin(&mySerialtwo);
uint16_t dist2 = tfmini2.getDistance();
uint16_t strength2 = tfmini2.getRecentSignalStrength();

// Display the measurement
Serial.print(dist2);
Serial.print(" cm sigstr: ");
Serial.println(strength2);

mySerialtwo.end();

// Wait some short time before taking the next measurement
delay(500);
if (dist1<100 || dist2<100)
{
Serial.println("**************************************************");
NewTone(3,50,100);
}
}

Hello I am facing problem when connecting 2 TF mini lidar to arduino mega 2560

First thing you need to do is explain why you are using two instances of the SoftwareSerial class, while ignoring three hardware serial ports on the Mega.

void loop() {
  // Take one TF Mini distance measurement
  mySerialone.begin(115200);
  tfmini1.begin(&mySerialone); 
  uint16_t dist1 = tfmini1.getDistance();

Second thing you need to do is explain what you were smoking when you decided that SoftwareSerial could work reliably at that baud rate.

Third thing you need to do is explain why you are calling the two begin() methods on EVERY pass through loop(). The begin() methods should be called in setup(), NOT loop().

  mySerialone.end();

Fourth thing you need to do is explain why you ever call end(), let alone on every pass through loop().

Hi, did anyone managed to make two tf mini work on a mega2560?