I want to code a depth sensor for arduino
Your code has several problems:


You are including the MS5837 library twice. That just doesn't pose a problem, because the library has include guards, that make sure it is only included once.
At global scope you use float currentdepth = sensor.depth();, which calls the function sensor.depth(). This will be done before the setup() function, so at that time the sensor library does not have any valid depth data. So it's useless. You can just set it to zero.
In your loop() you are doing sensor.read(), but then you are never actually getting the new depth value from the library. You are just again using currentdepth without changing it ever. You should set the variable to the new value by using currentdepth = sensor.depth(); after the sensor.read().
You are using the statement if ( error=0). Using only a single = means an assigment. So here the variable error get's set to zero and the if statement will not compare it so zero. You have to use a double =: if(error == 0).
if( error &gt;-0): Why are you checking for error being greater than MINUS ZERO. Sure, -0 is a valid integer, but mostly senseless. Also it is questionable, if the logic is correct here, since first you check for error being different than zero. Then you check for it being zero and then (if it is not equal zero) you check for it being greater than zero. If error really happens to be greater than zero, you will first write pwm to the servo and then directly 1400, which is submerging and resurfacing directly after another in only one loop.
The whole if structure seems quite off. You should rethink the logic of the program.




That said, for reaching a set point of whatever kind it is good to use a PID controller, that will get you to the setpoint rather fast and will keep you there. A PID controller can be done in hardware or software. For implementing a software PID controller on Arduino you can use one of the various PID libraries, for example this one. Most libraries also contain examples, that show how to use them.