Want to return data from a function

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?

return (dist);

Gates:

return (dist);

Thanks for you reply, I tried return (dist); and return int(dist); but still no success. :frowning:

You need to post the revised code. Nobody can guess what you did. Also we can't see the declaration of 'dist' because you didn't follow the forum guidelines and post your entire sketch.

Why are you reading the data into a variable ret and then trying to read the same data again to print it? What happens if you just print ret?

Steve

You have a couple of issues:

  1. Get_Lidar_data() has a return value of int. Every time it is called it returns a value, whether dist has been recalculated or not. This may or may not be a problem depending on how you use the function.

  2. Because you return dist directly after it is calculated, you never reset rec_debug_state back to 0x01 and therefore never read the next lidar value because rec_debug_state is permanently stuck at 0x09!!!

    else if (rec_debug_state == 0x09)
    {
      uart[8] = Serial1.read();
      if (uart[8] == check)
      {
        dist = uart[2] + uart[3] * 256; //the distance
        return dist;
      }
      rec_debug_state = 0x01;
    }

If you code it like this then it returns the latest dist value:

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
      }
      rec_debug_state = 0x01;
    }
  }
  return dist;
}

slipstick:
Why are you reading the data into a variable ret and then trying to read the same data again to print it? What happens if you just print ret?

Steve

Thanks I know about this but its not just about printing it to serial port. I need to do further development, so first I have to deal with this issue.

ToddL1962:
You have a couple of issues:

  1. Get_Lidar_data() has a return value of int. Every time it is called it returns a value, whether dist has been recalculated or not. This may or may not be a problem depending on how you use the function.

  2. Because you return dist directly after it is calculated, you never reset rec_debug_state back to 0x01 and therefore never read the next lidar value because rec_debug_state is permanently stuck at 0x09!!!

    else if (rec_debug_state == 0x09)

{
     uart[8] = Serial1.read();
     if (uart[8] == check)
     {
       dist = uart[2] + uart[3] * 256; //the distance
       return dist;
     }
     rec_debug_state = 0x01;
   }




If you code it like this then it returns the latest dist value:



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
     }
     rec_debug_state = 0x01;
   }
 }
 return dist;
}

Appreciate your interest and help. Actually I tried it before what you suggested I mentioned this in my written words. But this time I also edited the code in my above post and added outside of if statement as I mentioned but it also didnt work.

to analyse what is really going on I would add serial out-put that is completely unconditional
to see what goes on in each and single call of your function.

In the code posted above even the receiving of the bytes is conditional to your debug-variable.

You haven't posted a full sketch in the last code.
Assuming your full code has the same

void loop() {[color=#222222][/color]
ret = Get_Lidar_data();[color=#222222][/color]
Serial.print("dist = ");[color=#222222][/color]
Serial.println(ret); //output measure distance value of LiDAR[color=#222222][/color]
delay(100);[color=#222222][/color]
}[color=#222222][/color]

it might be that you start read out the serial buffer at a different point than you expect it.
and then your if elsif logic behaves different than you expected it.

Serial.read() gives you a single byte. If I were you I would read in all bytes into a char-array where the char-array has a size that is way big enough to store all received bytes. Or if the number of bytes that are sended is a always fixed number read-out this number of bytes from the serial buffer. And then doing an analyse of the char-array.

Serial.read() means you take the byte out of the serial buffer and then this byte is gone in the serial-buffer.

So reading the serial buffer into an char-array offers the opportunity to print out the whole bytesequence to get real feedback what do you receive. Assuming "it must be this..." is just assuming.

For further help it would be very good if you would share a datasheet of your sensor and if you have any kind of testprogram to read the bytes of your sensor how does the bytesequence look like and post

both : completely sketch that does the reading and the serial printing and the serial output you get in the serial monitor.

This delivers enough information to do a deep enough analysis to find the error and then find a solution.

In the long run it is much more efficient to check every dammed single detail of what is going on than guessing around "maybe if I change ...." or thinking "the byte-sequence is ...." so why does this code not work.

Obviously it does not work. Otherwise you wouldn't ask here.

It is your decision:
going on guessing around and maybe have luck
or analysing all details with guarantee to get it to work.

best regards Stefan