Distance sensor displays "1" beyond 9 inches

We have several kits with distance sensors and they all are having this issue:

The distance sensor displays correct values when run directly from the brain via Settings→Device Info→[port] (and reports “No object beyond” 19 inches) but when run from a program in VEXcode IQ Blocks, it fails beyond about 9 inches and displays “1” (1 inch).

Can you post your program that you’re using? I just tried it with the following and everything seemed to work OK.

1 Like

I hadn’t included a wait block. With the addition of that, it works correctly (displaying 4294967295 when beyond about 20 inches).

Do you have any idea why the wait block would make a difference?

Possibly clearing the display quicker than it can write to it so you never see the full number. The wait gives it time to render the display.

Printing / clearing too quickly. The wait block allows time for the text to be drawn on the screen (and read by the user). Typically a 1/10th of a second is more than enough of a delay when printing on the screen.

The “4294967295” is a maximum unsigned 32-bit integer. This is because the sensor reports “-1” when an object isn’t detected. We felt that -1 wasn’t help since a lot of times logic is checking “distance sensor < X”, so instead the value was made positive. I’m not sure (2^32)-1 is the best choice, but it’s better than -1… but that’s a separate discussion.

1 Like

Thanks for the explanation! 2^32-1 is just fine by me… it allows for anything reasonable larger than zero to be used as a value.

Nice choice over -1. Now if you promise that the infinity distance will be “4294967295” we can use that as the upper bound. I most likely would have gone with 9999. But then Future Foster will come back to yell at me to say “Hey the 2045 GDC made the field be 300 yards long.”

Never too early to start the @DRow Son for GDC :smile: