While it seems like you’re starting down the right track, the symptoms you report unfortunately don’t have a single possible root cause. To help narrow it down:
Do you have any info on the part number, or a link to the product that was purchased? Not all of them are exactly the same, and not all would work correctly with the roboRIO. The title says “VEX”, but I just want to confirm which one.
By having this info, people can help determine whether the sensor hardware is electrically compatible with the roboRIO, and whether the specifications align with the use-case.
This will print the reading once, right as you enable teleop. This isn’t necessarily “wrong” for initial testing, but keep it in mind as you construct your experiments.
Can you confirm which ports you actually used? The diagram indicates ports 0 and 1:
This information will help confirm whether the code has been written correctly (IE, aligned with your hardware and hookup assumptions).
The first line of code has a Capitalized U for Ultrasonic, while the second line has a lower-case u. Is this how your code is currently set up? Capitalization matters - you’ll want to make sure code functionally aligns with the examples in WPILib.
Can you describe the objects, and the distances you used? Ultrasonic sensors generally need large, flat surfaces, and have a certain minimum and maximum working range. Providing your test case will help people get an idea of what sorts of values should be expected in software.
It’s probably worth posting a picture (or two) of your electrical connections. If you have the wrong ports, have input and output flipped, or have flipped either of the individual ports (GND and SIG), it will cause problems.
While it’s hard to say for certain there’s nothing wrong with the code, everything I’m hearing sounds like that’s headed in the right direction.
Agreed - photos of setup or more details might be able to eek out a solution. But I’m struggling to find anything fundamentally wrong with the setup described.
In cases like this, I usually start trying to run experiments to find the at-fault component. Running the sensor with a different microcontroller (arduino?), running the same code and roboRIO with a different sensor… something like that to help extract whether it’s a wiring or hardware issue, or if the issue lies with the software implementation.
While this might be a stretch - if you have access to an oscilloscope, it would make a great debug tool for a case like this.