Problem with ultrasonic sensor

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
numeras
Posts: 5
Joined: 16 Dec 2010, 18:13

Problem with ultrasonic sensor

Post 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!!
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: Problem with ultrasonic sensor

Post 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);
   }
}
Regards, Morton
numeras
Posts: 5
Joined: 16 Dec 2010, 18:13

Re: Problem with ultrasonic sensor

Post by numeras »

Hello,

Thank you very much!
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 2 guests