Hello all great programmers
I have a problem when returning a value from function. I am reading data from sensor, when I use the following code it works without any problem.
void loop() {
Get_Lidar_data();
}
void Get_Lidar_data(){
if (Serial1.available())
{ //check if serial port has data input
if(rec_debug_state == 0x01)
{ //the first byte
uart[0]=Serial1.read();
if(uart[0] == 0x59)
{
check = uart[0];
rec_debug_state = 0x02;
}
}
else if(rec_debug_state == 0x02)
{//the second byte
uart[1]=Serial1.read();
if(uart[1] == 0x59)
{
check += uart[1];
rec_debug_state = 0x03;
}
else{
rec_debug_state = 0x01;
}
}
else if(rec_debug_state == 0x03)
{
uart[2]=Serial1.read();
check += uart[2];
rec_debug_state = 0x04;
}
else if(rec_debug_state == 0x04)
{
uart[3]=Serial1.read();
check += uart[3];
rec_debug_state = 0x05;
}
else if(rec_debug_state == 0x05)
{
uart[4]=Serial1.read();
check += uart[4];
rec_debug_state = 0x06;
}
else if(rec_debug_state == 0x06)
{
uart[5]=Serial1.read();
check += uart[5];
rec_debug_state = 0x07;
}
else if(rec_debug_state == 0x07)
{
uart[6]=Serial1.read();
check += uart[6];
rec_debug_state = 0x08;
}
else if(rec_debug_state == 0x08)
{
uart[7]=Serial1.read();
check += uart[7];
rec_debug_state = 0x09;
}
else if(rec_debug_state == 0x09)
{
uart[8]=Serial1.read();
if(uart[8] == check)
{
dist = uart[2] + uart[3]*256;//the distance
Serial.print("dist = ");
Serial.println(dist); //output measure distance value of LiDAR
Serial.print('\t');
delay(100);
}
rec_debug_state = 0x01;
}
}
}
But I return the dist value from the function then controller returns the same value and the distance from the sensor is not updated. Although I removed the sensor but RX LED on controller continues to blink.
#include <SoftwareSerial.h> //header file of software serial port
SoftwareSerial Serial1(2,3); //define software serial port name as Serial1 and define D2 as RX and D3 as TX
int dist; /*----actual distance measurements of LiDAR---*/
unsigned char check; /*----save check value------------------------*/
int i;
int ret;
unsigned char uart[9]; /*----save data measured by LiDAR-------------*/
const int HEADER=0x59; /*----frame header of data package------------*/
int rec_debug_state = 0x01;//receive state for frame
//unsigned char rec_flag = 0;
void setup() {
Serial.begin(115200); /*----set bit rate of serial port connecting Arduino with computer-----*/
Serial1.begin(115200); /*----set bit rate of serial port connecting LiDAR1 with Arduino-------*/
}
void loop() {
ret = Get_Lidar_data();
Serial.print("dist = ");
Serial.println(ret); //output measure distance value of LiDAR
delay(100);
}
int Get_Lidar_data(){
if (Serial1.available())
{ //check if serial port has data input
if(rec_debug_state == 0x01)
{ //the first byte
uart[0]=Serial1.read();
if(uart[0] == 0x59)
{
check = uart[0];
rec_debug_state = 0x02;
}
}
else if(rec_debug_state == 0x02)
{//the second byte
uart[1]=Serial1.read();
if(uart[1] == 0x59)
{
check += uart[1];
rec_debug_state = 0x03;
}
else{
rec_debug_state = 0x01;
}
}
else if(rec_debug_state == 0x03)
{
uart[2]=Serial1.read();
check += uart[2];
rec_debug_state = 0x04;
}
else if(rec_debug_state == 0x04)
{
uart[3]=Serial1.read();
check += uart[3];
rec_debug_state = 0x05;
}
else if(rec_debug_state == 0x05)
{
uart[4]=Serial1.read();
check += uart[4];
rec_debug_state = 0x06;
}
else if(rec_debug_state == 0x06)
{
uart[5]=Serial1.read();
check += uart[5];
rec_debug_state = 0x07;
}
else if(rec_debug_state == 0x07)
{
uart[6]=Serial1.read();
check += uart[6];
rec_debug_state = 0x08;
}
else if(rec_debug_state == 0x08)
{
uart[7]=Serial1.read();
check += uart[7];
rec_debug_state = 0x09;
}
else if(rec_debug_state == 0x09)
{
uart[8]=Serial1.read();
if(uart[8] == check)
{
dist = uart[2] + uart[3]*256;//the distance
//return int(dist);
}
rec_debug_state = 0x01;
}
}
return (dist);
//delay(100);
}
I tried to add delay and without delay, added return inside the if statement and at the end of function but still the same result.
Can anyone please tell me is there any logical error in my code?