Sonar not Displaying values

When I was debugging some values for the Sonar sensor, the proper values do not show. Instead, it displays a constant value of -0.03995 or something like that. The sonar is plugged in correctly and we have a while loop that displays the code onto the screen, but it is still only outputting a constant value that is unreasonable. Does anyone have any idea why this is happening?

We encountered the same issue (different value though) with our ultrasonic (I’m assuming you’re talking about that seeing as there isn’t a sonar for vex yet.) We thought it was our sensor, and didn’t go much further into it since it wasn’t important for our robot.

Could you post your while loop here?

Sorry, I meant Ultrasonic sensor, but here is our while loop.

int main() {



while (true)
    Brain.Screen.printAt( 10,  30, "Battery Capacity      %d",Brain.Battery.capacity());  
    Brain.Screen.printAt( 10,  50, "Potentiometer Value     %d",CatapultPot.value(percentUnits::pct)); 
    Brain.Screen.printAt( 10,  70, "counts     %.2f", RightMotor2.rotation(rotationUnits::raw) );  
    Brain.Screen.printAt( 10,  90, "counts     %.2f", LeftMotor2.rotation(rotationUnits::raw) );
    Brain.Screen.printAt( 10,  110, "Potentiometer Value     %d",ScrapePot.value(percentUnits::pct)); 
     Brain.Screen.printAt(10, 130, "distance: %f in", CatapultSonar2.distance(distanceUnits::in));
     Brain.Screen.printAt(10, 150,  distance: %f in", CatapultSonar.distance(distanceUnits::in));


The only part that pertains to the ultrasonic sensors are the lines at the end of the while loop. All of the other commands print the correct information onto the screen, but the ultrasonic sensors’ data is not printed properly.

most likely a broken ultrasonic, try switching it.

The -0.094 is -1mm converted to inches. -1 means no object has been detected. Assuming that there is in fact an object present, either you have configured the sonar incorrectly, plugged the wires in with input and output swapped (can you hear the sonar ping ? I usually can) perhaps not pushed the wires into the brain far enough, or it’s broken.

I can hear the ping when plugged in, so i might just need to swap them out.

you have two sonars in your code, are either working ?

No, both are displaying the same value listed at the top.