controll servos

Hello guys,
i'm a new one , and i have questions for controlling servos. i modified a code for controlling 5 servos,but just 2 servos can be moved in the process.
Could you please tell me the reason and how to modify, thank you so much.


int servopin=9;
int servopin1=10;
int servopin2=11;
int servopin3=6;
int servopin4=5;
int myangle;
int pulsewidth;
int val; 
void servopulse(int servopin,int myangle)
{ 
pulsewidth=(myangle*11)+500;
digitalWrite(servopin,HIGH); 
delayMicroseconds(pulsewidth);
digitalWrite(servopin,LOW);
delay(20-pulsewidth/1000); 
} 
void setup() 
{ 
pinMode(servopin,OUTPUT);
pinMode(servopin1,OUTPUT);
Serial.begin(9600);
Serial.println("servo=o_seral_simple ready" ) ; 
} 
void loop()
{ 
val=Serial.read();
if(val>'0'&&val<='9') 
{
val=val-'0';
val=val*(180/9);
Serial.print("moving servo to "); 
Serial.print(val,DEC);
Serial.println(); 
for(int i=0;i<=50;i++) 
{ 
servopulse(servopin,val);
servopulse(servopin1,val);
servopulse(servopin2,val);
servopulse(servopin3,val);
servopulse(servopin4,val);
} 
}
}

How are you powering your servos? Powering servos from the arduino causes problems. Servos need an external power supply.

zoomkat:
How are you powering your servos? Powering servos from the arduino causes problems. Servos need an external power supply.

Thank you for your answering, but i don't think this is the reason. Because I use external power for servos and use usb power for arduino.
If i upload code as follow, then my 5 servos can move and at same time.

// Multi Sweep, Duane B

// Using the servo library created by Michael Margolis 

// to control 12 Servos with one Arduino Uno 




#include <Servo.h>
// Sample sketch for driving 12 Servos from an Arduino UNO, servos are attached to digital pins 2,3,4,5,6,7,8,9,10,11,12,13

#define CONNECTED_SERVOS 12

// macro just adds two - the first servo is attached to digital pin 2, this gives us upto 12 servos - digital 2 to 13
#define SERVO_TO_PIN(x) (x+2)
int val;
Servo myServos[CONNECTED_SERVOS];

#define COUNT_DOWN -1
#define COUNT_UP +1
#define INCREMENT 10 // move in steps of 10 milliseconds

int nPulseWidth = DEFAULT_PULSE_WIDTH ; // 1500, defined in servo.h
int nDirection = COUNT_UP;

volatile unsigned long ulStart = 0;
volatile unsigned long ulStartToEnd = 0;

void setup()
{
 
 // attach the servos
 for(int nServo = 0;nServo < CONNECTED_SERVOS;nServo++)
 {
   myServos[nServo].attach(SERVO_TO_PIN(nServo));
 }
 
 // the library sets all servos to 1500 ms pulse width by default, this is center for a steering servo
 Serial.begin(9600);
 Serial.println("Completed setup");  
}

void loop()
{
    if(ulStartToEnd)
   {
     Serial.println(ulStartToEnd);
     ulStartToEnd = 0;
     }
     nPulseWidth += nDirection * INCREMENT;
     if(nPulseWidth >= 2000)
     {
       nPulseWidth = 2000;
       nDirection = COUNT_DOWN;
       }
 
 if(nPulseWidth <= 1000)
 {
   nPulseWidth = 1000;
   nDirection = COUNT_UP;
 }
 
 for(int nServo = 0;nServo < CONNECTED_SERVOS;nServo++)
 {
   myServos[nServo].writeMicroseconds(nPulseWidth);
 }
 
}

Do you Below is some servo test code you can try to see if you can the servos to move at the same time, Also read #7 below about how to post your code:

http://forum.arduino.cc/index.php/topic,148850.0.html

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Hi zoomkat,
Thank you for your help about the code and how to post code.I use your code to test and 4 servos can work well, so I think it's my code problem.

 if(val='1') {
    delay(10);  // give the servos time to move after each update
  1. Assigning val the value of '1', in an if statement, seems pointless. == is used to compare values
  2. That comment is flat ass wrong. One delays AFTER telling a servo to move, to give it time to get there. Delaying BEFORE moving a servo just wastes time.

PaulS:

 if(val='1') {

delay(10);  // give the servos time to move after each update



1) Assigning val the value of '1', in an if statement, seems pointless. == is used to compare values
2) That comment is flat ass wrong. One delays AFTER telling a servo to move, to give it time to get there. Delaying BEFORE moving a servo just wastes time.

You are right, the commands that you point is added by me and i forgot to delete.
I am learning programming for controlling 5 servos and use serial monitor to give a signal. The first code i posted that just can control 2 servos (pin9,10) and i don't know what is the problem. For second code , the original code can control 5 servos and i want to modify code to use serial monitor to give a signal.

Could you please give me some advice about the first code? I'll appreciate.

The first code i posted that just can control 2 servos (pin9,10) and i don't know what is the problem.

The first code does not use the Servo class.

For second code , the original code can control 5 servos and i want to modify code to use serial monitor to give a signal.

The second code does.

Could you please give me some advice about the first code?

Certainly. Delete it. Make the second code do what you want.