I'm currently using an ultrasonic proximity sensor to measure how much of the water in a tank is filled up to. I'm starting by simply trying to receive a distance measurement between the sensor and the object in which the wave is reflected by. I've tried building my own code (in C language) however I'm not quite sure where is my issue. I have the code written to display on the LCD. The numbers in which I am getting are 669, A45, h45. Below is the commented function that I have written to perform this task.
void distance_sensor(void)
{
DDRC= 0x0F; //enables only one nybble to be an output
double timecnt;
double distance;
int echo;
char distb [16] = {"Container up to:"};
lcd_write(distb, 16, line_1); //displays banner on LCD
echo = (PINC & 0x10); //declares the input echo from the 2nd nybble
distance= 0; //initialization
timecnt = 0;
PORTC = 0x00;
_delay_ms(1.0);
PORTC = 0x0F;
_delay_ms(5.2); //creating a 10 us output square wave and sending it out through port c
PORTC = 0x00;
while(echo==0)
{
echo = (PINC & 0x10); //rechecking the input
timecnt = timecnt + 1; //time taken for the wave to reflect back to the sensor
_delay_us(100.0);
}
timecnt= timecnt*100;
distance = (timecnt*0.000343)/2;
hextodec(distance); //function made to split the value into msd, nsd, lsd and displays on LCD
return;
}