I am using the Maxbotics sensor, obtaining data through the AD, and need to find the difference between two consecutive readings. In the following, I have a distance function that returns the distance to the target in inches, and I use that number in the following function delta_distance() to find the difference between consecutive readings.

The problem is, when I run the thing, my return is always the inverse of the actual distance reading, and the issue is, solved with a printf, that my temp variable is never changing. It remains at zero constantly.

```
/*******************************************************************************
* FUNCTION NAME: distance
* PURPOSE: returns a realtime distance from sonar, in inches
* CALLED FROM: This file
* ARGUMENTS:
* Argument Type IO Description
* -------- ------------- -- -----------
* none
* RETURNS: unsigned int
*******************************************************************************/
unsigned int distance(void)
{
return (unsigned int) Get_Analog_Value(rc_ana_in01)/s;
}
/*******************************************************************************
* FUNCTION NAME: delta_distance
* PURPOSE: returns difference in range readings between cycles
* CALLED FROM: This file
* ARGUMENTS:
* Argument Type IO Description
* -------- ------------- -- -----------
* none
* RETURNS: signed int
*******************************************************************************/
int delta_distance(void)
{
int temp;
temp -= distance();
return (int) temp;
temp=distance;
}
```

As well, if anyone can quickly through up a workable averaging function I’d be much obliged. I’ve tried without success, typically ending up with a constantly incrementing distance value, or erratic jumping output.

```
/*******************************************************************************
* FUNCTION NAME: distance
* PURPOSE: returns a realtime distance from sonar, in inches
* CALLED FROM: This file
* ARGUMENTS:
* Argument Type IO Description
* -------- ------------- -- -----------
* none
* RETURNS: unsigned int
*******************************************************************************/
unsigned int distance(void)
{
unsigned int temp;
i++;
unsigned int Distance;
temp+= (Get_Analog_Value(rc_ana_in01)/2)
if (i>=5)
{
Distance=temp/i;
i=0;
}
return (unsigned int)Distance;
}
```

Thanks!