Function will not return a value

I'm a newbie, and have been struggling for days. Although the code turns the LED "on" and "off" properly, my function will NOT return any value other than zero. I'm certain that I have made many mistakes, but - as I mentioned, it does turn on the LED properly. What I want it to do is:

Get a data stream from a radar unit that outputs a new value every 2 mSec. The radar unit outputs a "0x02", then the distance in binary of what it sees. If the object is too far away, it sends absolutely nothing after that 0x02, so it gives a continuous stream of 0x02,0x02,0x02, etc. If an object is within range, it outputs 0x02,0xXX, where "XX" is the distance. So I wait for 1ms of no input, then grab the next two chars. If the first is 0x02 and the next is 0x03 or more, I know the second is the distance and want return that value. Otherwise I want to return a 0x00. But my code returns only 0x00 - no matter what. Please tell me what I'm doing wrong. I'm using a TEENSY 3.5 processor. The USB serial is Serial, while the radar is connected to Serial1

#define ledPin 13
#define SERIAL_DBG    Serial
#define SERIAL_Omni   Serial1

void setup() {

   pinMode(ledPin,OUTPUT); 
   digitalWrite(13,0);                                                   
  SERIAL_Omni.begin (19200);
  SERIAL_DBG.begin(115200);
}

void loop(void){
int  dist;
dist = radar_read(0);

Serial.write(dist);
}

int radar_read(int distance)
{
  static int state = 0;
  static unsigned long timer = 0;
  static unsigned long timer2 = 0;
 switch(state)
  {
  default:
  case 0: // clear RX and start timer
    SERIAL_Omni.clear(); // flush rx
    timer = millis();
    state++;
    break;
  case 1: // need >1ms gap with no data
    if(SERIAL_Omni.available())
    {
      state = 0; // start over
      break;
    }
    if(millis() - timer > 1)
    {  
     state++; // success
     break;
    }

  case 2: // normal state.. always in this state except for startup and error
    if(SERIAL_Omni.available() < 2)
    {
      break; // wait
    }
    else 
    {
      unsigned char buf[4];
      unsigned int distance;
 
      for(int i = 0; i < 3; i++)
{
        buf[i] = SERIAL_Omni.read();

      if(buf[0] == 0x02) 
      {
         if (buf[1] >= 0x03)
          {
           distance = buf[1];
           digitalWrite(ledPin,1);
          }
      else
        {
         digitalWrite(ledPin,0);
        }
      }
  }
}
 
 return (distance);
}

type or paste code here

type or paste code here

You pass zero to the function and that is what is returned. Your other distance variable, that may have got something from the omni has gone out of scope.

You pass in distance and return distance. So radar_read(0) will always return 0.

Probably not a good idea to have these variables with the same name...

I'm wondering whether you are not quite sure how values are passed to & from a function?

int myFunction (int inParm1)

Anything inside ( ) is passed to the function. There can be multiple variables passed in.

The int at the start of the line defines the type of result the function will return - in this case an integer value .

consider
need to reset whenever 0x2 is received

#undef MyHW
#ifdef MyHW
#define SERIAL_Omni   Serial
#define STX   '2'

#else
#define SERIAL_Omni   Serial1
#define STX   2
#endif

#define ledPin 13
#define SERIAL_DBG    Serial

void setup () {
    pinMode (ledPin,OUTPUT);
    digitalWrite (13,0);

    SERIAL_Omni.begin (19200);
#ifndef MyHW
    SERIAL_DBG.begin (115200);
#endif
}

void loop (void){
    int  dist = radar_read (0);

    if (dist)  {
        Serial.println (dist);
        digitalWrite   (ledPin, 1);
    }
    else
        digitalWrite   (ledPin, 0);
}

int
radar_read (
    int distance)
{
    static unsigned idx = 0;

    if (SERIAL_Omni.available())  {
        char c = SERIAL_Omni.read ();

        if (c == STX)
            idx = 1;
        else if (1 == idx && 3 <= c) {
            distance = c;
            idx = 0;
        }
    }

    return distance;
}