Maintain Depth in RoV

Hi.
All the code is now complete with the exception of the depth control.
I have a sketch that will control the depth But I would like a little more dead band when depth is correct.
I have marked the place were the dead band is wanted with a comment..

#include <Servo.h> 
int requireddepth = 0;  // variable to store the value coming from the sensor
int currentdepth = 0;
int tick = false;//    Am I already in LOOP 
int Virt_thruster_adjust = 0;// Amount to maintian depth
int vertical_thruster = 90;// actual thruster speed forward or revirse
int Vertical_Potentiometer ;// Signal from topside control box
Servo Vertical_Thruster;

void setup() {
  Serial.begin(9600);  
  Vertical_Thruster.attach(9); 
}

void loop() {
  Vertical_Potentiometer = analogRead(A0);                    // Temp Fix that is the same as signal from topside control
  Vertical_Potentiometer = map(Vertical_Potentiometer, 0 , 1023 , 0 , 180);// 0 to 180 out
  if ( Vertical_Potentiometer > 88 &&  Vertical_Potentiometer < 92 ) {  //Skips if Data is around centre point SO  Hold current depth
    currentdepth = analogRead(A3);                        // take a depth reading with Pressure sensor
    if (tick == false){                                    // I want to do this Hold Depth
      requireddepth = currentdepth;                        // Now hold this as a referance point
      tick = true;                                         // Now I cannot redo the requird depth and I am in Depth maintain loop
    }
    //**********  AREA here is still not skiping if currentdepth = requireddepth + or - 2..Not sure how to fix this
    currentdepth = analogRead(A3);                     //Take another reading as first is in check loop  with Pressure sensor
    // if currentdepth +3  requireddepth
    Serial.print("  currentdepth = "); Serial.print(currentdepth);
    Serial.print("  requireddepth = "); Serial.print(requireddepth);
    if ((currentdepth  - requireddepth) > 5){    
      Virt_thruster_adjust = Virt_thruster_adjust - 1; // Decrease thruster speed
      Serial.print (" Go up ");
    }
    if ((requireddepth  - currentdepth) > 5) {         
      Virt_thruster_adjust = Virt_thruster_adjust + 1; // Increase thruster speed
      Serial.print (" Go Down ");
    }
  }
  vertical_thruster = Vertical_Potentiometer + Virt_thruster_adjust;
  if ( Vertical_Potentiometer > 88 &&  Vertical_Potentiometer < 92 ) {              // Still in centre
    tick = true;       // DONT do this loop as Depth is OK
    
  }  
  else{
    tick = false;                                        // go back to surface control as No longer in Depth maintain loop
    Serial.print("tick =  "); 
    Serial.print(tick);           //   Tick should be 0 here
    vertical_thruster = Vertical_Potentiometer;             // resets to centre/netual postition
    Virt_thruster_adjust = 0;                                     // remove anything  here as its no longer wanted
  }
  if(vertical_thruster > 180)  vertical_thruster = 180;          // checks the range does not excide limmets
  if(vertical_thruster < 0)  vertical_thruster = 0;
  Vertical_Thruster.write(vertical_thruster);                         //Continious rotation Servo in here for Testing
  Serial.print("              Virtical Thruster Speed = "); 
  Serial.println(vertical_thruster);
  delay(100);
}// *************************************************    END of MAIN  LOOP    ****************************************

I have used Pots to input the varables.
I did try the Pid method But I lack knowledge on using the PiD.
Many thanks for looking.
Regards Antony.

So, what is the problem? Does the code not do what you want?

Hi Paul.
The code is working .. But I would like a small area of deadband when the required depth is near actual depth. So if it's + or - 2 then do nothing.. ( easy to write but hard to code ).
I have no idea on how to achieve this.

Regards Antony

This code

    if ((currentdepth  - requireddepth) > 5){    
      Virt_thruster_adjust = Virt_thruster_adjust - 1; // Decrease thruster speed
      Serial.print (" Go up ");
    }
    if ((requireddepth  - currentdepth) > 5) {         
      Virt_thruster_adjust = Virt_thruster_adjust + 1; // Increase thruster speed
      Serial.print (" Go Down ");
    }

appears to give you that deadband. Is it not doing what you want?

Hi Paul.
You are correct. I have tested the code with the pot's and it shows no deadband.. Mabe it will work when tested under real conditions.
I will put together a pressure rig and retest under pressure and adjustment. Using a pressure sensor and the foot pump.
Thanks for your help.
Regards Antony.