How to invert Ultrasonic "distance" value?

Greetings,

As we know a normal Ultrasonic sensor works when we put an object near the sensor, the distance value is small and if we put the object far the distance value will be large. So for my application, I want the functionality to be inverted. Is there any way of mathematical calculation that may convert the distance value into another value like the one below?

My application: When the object is near, the value will be large, and when the object is far, the value will be small.

Many thanks!

Subtraction?

Hello rockrziq
Take a look at the map() function.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

Hello rockrziq, Good day and wolcome to the Forum.
You have to define what is far.
Say far = 200cm and the object is 10 cm in front of the sensor.
distance = 200cm - 10cm = 190cm
Other way round object is 180cm away from sensor
distance = 200cm - 180cm = 20 cm
So as already written the "Substraction" is the solution.

Try
distance *= -1;
or
distance = -distance;

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.