Page 1 of 1

Problem with ultrasonic sensor

Posted: 07 Jan 2011, 00:24
by numeras
I had made that simple code to read US sensor value:

task main()
{
int x;
SetSensorLowspeed(IN_4);
Wait(1000);
TextOut(1,LCD_LINE1,"first");
x=SensorUS(IN_4);
NumOut(40,LCD_LINE1,x);
Wait(1000);
while (true)
{
NumOut(10,LCD_LINE3,SensorUS(IN_4));
}
}

It's working but for example slow mowing from wall form 20cm going to 99cm is ok.. after I going further (over 100cm) and move fast closer then value is three digits. Last digit is constant, first two are changing as normal value of distance.

If sensor is over rich distance and after go back doing similar sort of problem.

When I testing in "NXT Datalog" sensor working good.

Please help!! thanks!!

Re: Problem with ultrasonic sensor

Posted: 07 Jan 2011, 02:43
by m-goldberg
I believe your problem is two-fold.

1. You try to read the sensor too fast.
2. You don't clear out old readings, so you see the old third digit when the reading goes from three to two digits.

The following code should work.

Code: Select all

task main()
{
   unsigned int x;
   SetSensorLowspeed(IN_4);
   x = SensorUS(IN_4); // ignore first reading, it is always zero
   Wait(MS_100);
   x = SensorUS(IN_4);
   Wait(MS_100);
   TextOut(1,LCD_LINE1,"first");
   NumOut(50,LCD_LINE1,x);
   Wait(SEC_1);
   while (true)
   {
      TextOut(10,LCD_LINE3,"   "); // three spaces
      x = SensorUS(IN_4);
      Wait(MS_100);
      NumOut(10,LCD_LINE3,x);
      Wait(SEC_1);
   }
}

Re: Problem with ultrasonic sensor

Posted: 07 Jan 2011, 13:32
by numeras
Hello,

Thank you very much!