Reddit – Dive into anything

I am trying to use a sketch which I originally had working with an UNO, it made use of the software serial Library and was being used for a LiDAR sensor, however when I try and use the sketch on an ESP32 it does not work properly and I have determined that it is due to the software serial library, I can’t figure out where I am going wrong with it. i have connected the sensor up to pin 2 and 4 on the esp as I have indicated these pins for rx and tx in the code.

CODE:

#include <SoftwareSerial.h>

#include “TFMini.h”

TFMini tfmini;

SoftwareSerial SerialTFMini(2, 4); //The only value that matters here is the first one, 2, Rx

void getTFminiData(int* distance, int* strength)

{

static char i = 0;

char j = 0;

int checksum = 0;

static int rx[9];

if (SerialTFMini.available())

{

rx[i] = SerialTFMini.read();

if (rx[0] != 0x59)

{

i = 0;

}

else if (i == 1 && rx[1] != 0x59)

{

i = 0;

}

else if (i == 8)

{

for (j = 0; j < 8; j++)

{

checksum += rx[j];

}

if (rx[8] == (checksum % 256))

{

*distance = rx[2] + rx[3] * 256;

*strength = rx[4] + rx[5] * 256;

}

i = 0;

}

else

{

i++;

}

}

}

void setup()

{

Serial.begin(115200); //Initialize hardware serial port (serial debug port)

while (!Serial); // wait for serial port to connect. Needed for native USB port only

Serial.println (“Initializing…”);

SerialTFMini.begin(TFMINI_BAUDRATE); //Initialize the data rate for the SoftwareSerial port

tfmini.begin(&SerialTFMini); //Initialize the TF Mini sensor

}

void loop()

{

int distance = 0;

int strength = 0;

getTFminiData(&distance, &strength);

while (!distance)

{

getTFminiData(&distance, &strength);

if (distance)

{

Serial.print(distance);

Serial.print(“cm\t”);

Serial.print(“strength: “);

Serial.println(strength);

}

}

delay(100);

}

Xổ số miền Bắc