Need help with Code to control servos with an Arduino?

The code will compile but when I connect the servos I start getting errors on the serial port.
any help on this would be great

#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
Servo myservo2; // a maximum of eight servo objects can be created
 
int pos = 90;    // variable to store the servo position
 
// set a variable to store the byte sent from the serial port
int incomingByte;
 
void setup() {

  // start sending data at 9600 baud rate
  Serial.begin(9600);
  myservo.attach(9);    // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);  // attaches the servo on pin 10 to the servo object

}
 
void loop() {
  // check that there's something in the serial buffer
  if (Serial.available() > 0) {
    // read the byte and store it in our variable
    // the byte sent is actually an ascii value
    incomingByte = Serial.read();
    // note the upper casing of each letter!
    // each letter turns a motor different way.
  
    // start of left laser sweep, moving left 90 degrees.
    // arrow laser spins and points to left.
    if (incomingByte == 'L') {
    Serial.println(incomingByte);
   
    myservo2.write(180);                         // send servo 2 to the 180 degree position and stay
    for(pos = 180; pos > 90; pos -= 1)  // goes from 90 degrees to 180 degrees
  {     // in steps of 1 degree
    Serial.println(pos);
    myservo.write(pos);    // tell servo to go to position in variable 'pos'
   
    delay(15);                       // waits 15ms for the servo to reach the position
  }
    for(pos = 90; pos<=180; pos+=1)     // goes from 180 degrees to 90 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
    }
    }
   
   
    // start of right laser sweep, moving right 90 degrees.
    // arrow laser spins and points to right.
   
    if (incomingByte == 'R') {
     Serial.println(incomingByte);
     Serial.println(pos);
    myservo2.write(0);                          // send servo 2 to the 0 degree position
    for(pos = 0; pos < 90; pos += 1)  // goes from 90 degrees to 0 degrees
  { Serial.println(pos);                                 // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
    for(pos = 90; pos>=0; pos-=1)     // goes from 0 degrees to 90 degrees
  { Serial.println(pos);                              
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
    }
    }
   
    // if a S is sent make sure the servos go to 90 degrees
    if (incomingByte == 'S') {
    Serial.println(incomingByte);
    Serial.println(pos);
    myservo2.write(90);
    myservo.write(90); 
     
     
     
     
    }
  }
}




sort by: active | newest | oldest
15mSec ? how fast ARE your servos ? For a 90 deg move, that corresponds to a servo speed of 3000 RPM !!!!

Make it 150mSec or even longer: that will give the serial routine long enough to transmit the serial packet properly.

Steve
DonC_WSU (author)  steveastrouk6 years ago
I did increase the time but still getting errors, the program runs fine until I connect the control wires to the servos and then the com port drops.

Could the servos be drawing to much current from my laptop and that is why it stops?
Yes, very,very easily. I suggest that's your problem, not the program - although that delay IS very short.

As soon as your servos move the load, the current requirement goes up, so that's killing the supply to the Arduino.

Steve
DonC_WSU (author)  steveastrouk6 years ago
Thanks for your help!

I hooked a power supply to the arduino and it worked fine. That was the problem.

Thanks again

Don
Great ! Don't forget to tick the "best answer" button for me !
maewert6 years ago
I haven't tried this code out but it looks OK from my quick review. 

Maybe you are sending too much data to the serial port and its overflowing. 

Try increasing the baud rate. 

Also do you really need each degree value printed?  You could print out every 10th by changing 'Serial.println(pos); ' to
'if ((pos%10)==0) Serial.println(pos);'

Best Wishes.