Problem: Serial COM Port Closes

Hello all, I would like to post a problem I have to see if someone could help me.

I have an Arduino BT and made a sketch tat recieves a number through bluetooth, sends that reading back to the computer, and uses that value to move some servos. The problem is that every second or third number I send, the COM port closes, and the connection is lost. If I close and reopen the serial monitor, the port opens again, but closes after just a few inputs. If I don’t send data the port never closes. Am I doing something wrong? Or perhaps it’s my bluetooth dongle?.. Thanks alot for any help you could provide.

#include <ServoMultiPin.h>

 
Servo frontservo;   
Servo rightservo;
Servo leftservo;
int read1;

 
void setup() 
{ 
  Serial.begin(115200);
  frontservo.attach(9); 
  rightservo.attach(8); 
  leftservo.attach(7); 
  Serial.flush();
} 
 
 
void loop(){
  if (Serial.available() > 0 ){ 
    read1 = Serial.read() - 48;
    delay(200); 
    Serial.print(read1);
    delay(200);
  
    if(read1 == 0){  
      frontservo.write(90);               
      rightservo.write(90);
      leftservo.write(90);
      }  
    else if(read1 == 1){  
      frontservo.write(90);               
      rightservo.write(180);
      leftservo.write(0);
      for(int i=0; i<50; i++){
        Servo::refresh();
        delay(50);
      }
    } 
    else if(read1 == 2){  
      frontservo.write(60);               
      rightservo.write(96);
      leftservo.write(96);
      for(int i=0; i<50; i++){
        Servo::refresh();
        delay(50);
      }
    }
    else if(read1 == 3){  
      frontservo.write(120);               
      rightservo.write(84);
      leftservo.write(84);
      for(int i=0; i<50; i++){
        Servo::refresh();
        delay(50);
      }
    }
   Serial.flush();
  }
}

Are you powering the servos from Arduino ?

If so, you are probably drawing too much current, and Arduino resets, this is what you experience as com port close down.

Thanks for the reply.

I ran it a couple of times with all the servos disconnected from the Arduino (nothing connected to the Arduino), and with new batteries (3V), and still the same behavior. I also tried a different serial monitor I downloaded, and different baud rates, but the same thing happens. :'(

But It works properly just with a Serial.print("hello world"); sketch? or fail too?

But It works properly just with a Serial.print("hello world"); sketch? or fail too?

I started again with a simple hello world program and it did not disconnect. I added features until it disconnected again, and I pinpointed the problem: the refresh function. If I use the refresh function the connection is lost. I'm searching to see if I'm using it wrong...

Don’t use the servo library, it just bloats your code.

Just use,

#define CENTER_RANGE 5

// Speed = 0 to 100
// Forward = true or false
// ServoPin = pin attached to control
void servo_control(int speed, bool forward, int servoPin)
{
    analogWrite(servoPin,(speed>CENTER_RANGE || speed<-CENTER_RANGE) ? map(-speed * (forward ? 1:-1),-100,100,135,225):0); 
}

Gracias eried!

Can this be used with more than two servos?

Of course (max 6 servos), but only use the pin 3, 5, 6, 9, 10 or 11