1

I am trying to read ultrasonic sensor reading from an Lego NXT robot using the programming language NBC (Next Byte Code). It says on the Lego Website that the sensor readings range from 0 -255 cm but when I output the sensor data, I get values above 255 sometimes; this usually happens when my Sensor reads in a value of 255; then after that it reads around 170 - 900 I don't know why it is doing this. Here is my code: (I am first reading in the sensor data then have the robot turn 45 degrees and repeat indefinitely just to examine how the data is being read.)

#include "NXTDefs.h"
// have the robot stay still and keep track of all the distances when turning around.

dseg segment
    Distance udword 0
dseg ends

thread main
    SetSensorUltrasonic(IN_4)
    Forever:
        ReadSensorUS(IN_4,Distance)
        NumOut(0,10,Distance)
        wait 1000
        RotateMotor(OUT_B,100,180)
        ClearSensor(IN_4)
        ResetSensor(IN_4)
        set Distance,0
        jmp Forever
        exit
endt
Charles
  • 50,943
  • 13
  • 104
  • 142
jonathan1987
  • 251
  • 1
  • 6
  • 17

3 Answers3

1

Well a table makes it perhaps clearer and stays:

Value of "Distance"   Display
_     255                255
_      80                805 

You see the 5 from the previous 255!

Charbonier
  • 11
  • 1
1

As @Charbonier said, you are not clearing the display. In this way, only some of the numbers are written over, creating the illusion that your sensor has gone crazy.

Try using this instead of NumOut() (DRAW_OPT_CLEAR_SCREEN is a constant defined as 1):

NumOutEx(0, 10, Distance, DRAW_OPT_CLEAR_SCREEN)

Alternatively, you may place the following line of code before your NumOut():

ClearScreen()
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
0

Are reading the numbers from the display? Are these the values the sensor has read?

For example,

Value of Distance    Display
    255                255
    80                 805

You see 805 because, you print 80 on the display and the last 5 from 255 stays still there.

The display is not erased by NumOut(). There should be an option to erase the display, I suppose.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Charbonier
  • 11
  • 1