How do you invert a value?

Hey, I’m looking to invert a value so when a distance/height is collected from a ultrasonic sensor, and a value is set for the desired height of the bath, it inverts it.

The scenario:

If the ultrasonic sensor was placed 40cm above the bath and a value/height was taken of 110cm (for example) that means the depth of the bathtub itself is 70cm (110 – 40). If a user wants the height of the bath to be 30cm (they will put in the value on a 2x16 LCD shield), I need an equation that will take that 30cm value (set by the user) and invert it so it’ll be 40cm, and add the other 40cm (distance between sensor and bathtub’s edge) to it, allowing the sensor to stop at 80cm, so the height of the bath water can be 30cm. However the height of the bath and the desired height always varies, is there a formula/ equation that could be implemented into my code:

(I’m using an Arduino Uno Rev 3)

#include <LiquidCrystal.h>
#define echoPin 2
#define trigPin 3
#define solenoidPin 11

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int a = 0; //used for moving the cursor left or right. 

byte numbr[4];  // This will hold 4 bytes, and if global, will init to all zeros
byte indx;  //This will hold an index to the array. If global, it will init to 0
int enteredValue;  // this will hold the final value entered

void setup() {
  lcd.begin(16, 2);
  lcd.print("Enter Height");

  lcd.print("in cm:");
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(11, OUTPUT); // connected to S terminal of Relay

void loop() {

  int x;  
  x = analogRead (0);

  if (x < 60) {
   //right button
   if (indx < 3 ) { 
  else if (x < 200) {
  //up button
   if (numbr[indx] < 90 ) {

  else if (x < 400){
  //down button
    if (numbr[indx] > 0 ) {

   else if (x < 600){
   //left button
     if (indx > 0) {

  else if (x < 800){
//want to use this to save the value and move onto another function. 
enteredValue = 0;
   for (int i = 0; i < 4; i++) {
     enteredValue = (enteredValue * 10) + numbr[i];

  unsigned long duration, distance;
  digitalWrite(trigPin, LOW); 
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;
  if (distance >= 200 || distance <= 5){
    Serial.print("Distance = ");
    digitalWrite(solenoidPin, LOW); // turn relay off

  else {
    Serial.print("Solenoid On Distance = ");
    Serial.println(" cm");;
    digitalWrite(solenoidPin, HIGH); // turn relay on 

You are trying to make this more complicated than it is.

Constant values are
Height of sensor above tub, let’s call it “HST”
Depth of tub, let’s call it “DOT”

Variable values are
Required depth of water, let’s call it “RDW”
Current sensor reading, let’s call it “CSR”

 if ((HST + DOT - CSR) < RDW) {
// Too much water
else {
// Not enough water

Does that make it simpler?

To get the negative of an integer is just x  = -x

The depth of the tub is varied as well.

Only if you move the sensor to a different tub. Once you have the sensor located, the values are constant.

Also when you flesh out the outline of the code you MAY find a simple mistake.

But the arduino wouldn’t know that, it would have to calibrate the depth of the empty bath, and subtract the 40cm (distance between the sensor and top of bathtub) equalling the bathtub’s depth.

please elaborate…


The Arduino doesn’t know it’s 40cm above the tub either. You have to tell it that, or write a calibration routine. But that is not in the scope of this question, nor does it affect the coding to empty/fill the tub.

As to the possible error, you don’t have to use my example, please feel free to formulate a different solution.

“HST” is 40cm.

 if ((HST + DOT - CSR) < RDW) {
// Too much water
else {
// Not enough water
Does that make it simpler?

To get the negative of an integer is just x  = -x

Is it implemented as:

if ((HST + DOT - HST - CSR) < RDW) { 
// stop solenoid/relay
        Stopwater ()

HST = 40cm

Please don’t guess. Look at the pseudo code, and figure out what gives.
HST + DOT - CSR should give the current depth of water, shouldn’t it?

So now you should be able to see the error.

You would have to define HST as 40cm in the setup, right?

As well as the other values.

By the time we get to where we are putting water into or out of the tub, the depth of the tub and height of the sensor have already been determined, either by hard coding the value, or through some other means, such as a calibration routine measuring actual values from the sensor, or via input via the keys and LCD.

Because of this, they are constants in the fill/empty routines.

To find the empty bathtub’s depth, is through a calibration method.

To set the height for the water to rise to, is through an LCD shield.

By using the calibration method to find the depth of the bath, the user’s desired height can be taken away from that, leaving the distance between the bathtub’s top and the water level surface, once filled to the desired height.

My initial post was to see if anyone could help me develop a functional code for through two functions.

But thanks for your help!!