360 servo bug

Hello,
I recently built a robotic arm and a BT android app to control it, but unfortunately, sometimes I can’t connect the phone to control it, and the motor, even though set by default when not used at servo.write(90), moves slowly backwards and forward a few times, and then frankly moves in one direction.
It seems to be the source of my connection problem, because when I disconnect the servo, and remove the part of code that contains the conditions for this motor, it works (the rest of the arm)…

Here is the code, note that the servo I’m talking about is servo3, and it is a 360 continuous servo, although there are other normal 180° servos.
This motor is used to rotate the arm around the torso.
Before I added this motor, everything used to work properly…

Thx in advance for answering!

Hugo.T

#include <Servo.h>

int val;
int angle1, angle2, angle3;
int charsRead;
char buffer[10];
Servo servo, servo1, servo2, servo3;

void setup() {
  Serial.begin(9600);
  
  servo.attach(6);
  
  servo1.attach(10);
  servo1.write(90);
  angle1 = servo1.read();
  
  servo2.attach(9);
  
  servo3.attach(11);
}

void loop() {
    if (val == 6) {
      servo.write(150);
    }
    else if (val == 5) {
      servo.write(30);
    }
   else if(val == 3011){
       servo1.write(angle1--);
       delay(35);
       }
       else if (val == 3021){
         servo1.write(angle1++);
         delay(35);
         }
      else if(val == 3022 || val == 3012){
        servo1.write(angle1);
        }
        
        else if (val == 2021){
          servo2.write(105);
          }
          else if (val == 2011){
            servo2.write(92);
            }
            else if (val == 2022 || val == 2012){
              servo2.write(96);
              }
              
              else if (val == 4021){
                servo3.write(94);
                }
                else if (val == 4011){
                  servo3.write(86);
                  }
                  else if (val == 4012 || val == 4022) {
                    servo3.write(90);
                    } 
        
          serialCheck();
}

void serialCheck(){
  while(Serial.available() > 0){
    charsRead = Serial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
    buffer[charsRead] = '\0';
    val = atoi(buffer);
    Serial.println(val);
    }
  }

You need to reformat your code so the indentation isn't wacky, and it can be read.

You are failing to clamp angle1's value to a sensible range - that's bad, sort it first, that may cure it.

OK, thanks.
But, I just did a few tests, and when I used only this servo separately, it stopped rotating at servo.write(92), certainly because servos are never extremely precise.
But in the main code and with the other motors connected, it does not stop and the closest point to immobility is at servo.write(90), but as I said at the beginning, it doesnt’ fully stop…

For angle1, I tried to add :

for (angle1 = 10; angle1 < 170; angle1++){
int angle1 = servo3.read();
}

But it didn’t do anything… Was it what you expected me to do?

Why are you calling servo.read()? You should be telling the servo where to be, not asking it where you last told it to go! servo.read() doesn't do anything but return what you last wrote.

By clamping I just meant change

 servo1.write (angle1++) ;

by

 angle1 = constrain (angle1+1, 0, 180) ; servo1.write (angle1) ;

(replace 0 and 180 by limits that are valid for your servo)

OK got it! Thanks a lot! ;)