Adding more servo's to code. Please help.

Hello everyone,

The code will follow. I have one servo that runs through serial communication. When I enter a "1" the servo moves to 90 degrees, when I enter a "0" the servo returns to 0 degrees. It's pretty simple.

I am having a difficult time getting an additional 3 servo's to operate in conjunction with the first servo. I have added the addition servo's to pins 5,6,7,8 and would like them to move like the single servo does when I enter 1111 into serial monitor. Also, if I enter 0000 I would like them all to return.

But, I guess the hard part I am having is figuring out when I enter 1100 how to make the first and second servo move to 90 degrees and the third and forth to move to 0 degrees. These aren't all the four digit combinations I am using. I am using all variations of four digital 1s, and 0s.

Also, I do not think I attached the servo's property in the code to do this. I do not understand how to add them in properly.

Thank you.

They number combinations are:

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

Here is the code:

#include <Servo.h> 
 
Servo myservo;          // Create servo object to control a servo 
 
char readstr[4];        // Character Array to store the serial input
int cstrpos = 0;        // Variable to store the current input array position
long val = 0;           // variable to store the value from the input 
 
int maxValue = 0;       //The "0" value causes the servo to return to Zero degrees when a "0" is input into the serial monitor.
 
void setup() 
{ 
  Serial.begin(9600);   // Startup the Serial Interface
  myservo.attach(5);    // Attaches the servo on pin 5 to the servo object 
  myservo.attach(6);
  myservo.attach(7);
  myservo.attach(8);
} 
 
void loop() 
{ 
  char ch;              //A Place to Store the character we just read
  
  if (Serial.available())      // is there anything to be read from serial port?
  {
    ch = Serial.read();        // read a single character
    
    // print out to serial port the character we received (similar to an echo)
    Serial.print(ch);
    
    if (ch >= '0' && ch <= '1')    // Number so store it in the input array.  Note that if any other than 1 or 0 is pressed, nothing will happen.  1 is to go to 90, 0 is to return.
                                   // This number can be change from 0-9 and will always go to 90 degrees because that is the degrees set below.
    {
      readstr[cstrpos] = ch;      // Add the read character to the array of read numbers
      ++cstrpos;                  // Increase the position in the array
    }
    else                          // not a number so lets set the position of the servo
    {
     readstr[cstrpos] = '\0';     // Add a null to the end of the string array to terminate the string
     val = atol(readstr);         // Convert the string to a long int
 
     if (val > maxValue) {
       val = 90;                   //A value of 90 is the degrees in which the servo will stop.  In this case, the servo will stop at 90 degrees.
     }
    
     cstrpos = 0;                 // Reset the array position back to the beginning so the next input starts a new input
     
     //Print to the serial port what we are going to do
     Serial.print("Servo set to: ");
     Serial.println(val);
     Serial.println("");
    }
  } 
  
  // The servo needs constant feed of values so we constantly output the PWM value
  myservo.write(val);            // Set the PWM value to send to the servo
  delay(15);                     
    
}

But, I guess the hard part I am having is figuring out when I enter 1100 how to make the first and second servo move to 90 degrees and the third and forth to move to 0 degrees. These aren't all the four digit combinations I am using. I am using all variations of four digital 1s, and 0s.

So you need an array to store the 4 bytes. As serial data comes in, put it in the array. If it's a new line, it's time to process the data. Loop through each item in the buffer and set the proper degrees based on the value.

Also, I do not think I attached the servo's property in the code to do this. I do not understand how to add them in properly.

All you've done is create a single servo object, and re-attach different pins to the single servo. You need an array of servos, and an array of pins.

So you need an array to store the 4 bytes. As serial data comes in, put it in the array. If it's a new line, it's time to process the data

Thank you. I found a great video on Youtube how to set up an array.

simple servo test code for multiple servos

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

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
    }
  }
}

Thanks for posting this test code. I gave it a go and it seems to be working properly in the serial monitor but the servo's themselves are not actually moving. I think I have to modify it a bit. I may just start completely from scratch, this is getting to be frustrating. I'm going to go back and try to make it work with an Array like suggested earlier. I'll make a new post when that part is done and working, before I attempt part 2 (getting the Uno to read 1,1,0,0, from an SD card and actually making the servos to move).

What external power source are you using to power these servos?

What external power source are you using to power these servos?

There are four micro servo's and they are powered by a four AA battery pack totaling 6V.

dkoc:

What external power source are you using to power these servos?

There are four micro servo's and they are powered by a four AA battery pack totaling 6V.

So have you tried just creating the four servo objects with the four separate pins, and just move them all at the same time to make sure the wiring/power isn't an issue, and not worrying about Serial communication for now?

There are four micro servo's and they are powered by a four AA battery pack totaling 6V

with a common ground to the Arduino ?

So have you tried just creating the four servo objects with the four separate pins, and just move them all at the same time to make sure the wiring/power isn't an issue, and not worrying about Serial communication for now?

Yes, I have tried this. They all seem to function correctly.

There are four micro servo's and they are powered by a four AA battery pack totaling 6V
with a common ground to the Arduino ?

Yes. Powering them doesn't seem to be an issue... I've tested them with different code.

dkoc:

So have you tried just creating the four servo objects with the four separate pins, and just move them all at the same time to make sure the wiring/power isn't an issue, and not worrying about Serial communication for now?

Yes, I have tried this. They all seem to function correctly.

Well unfortunately, Zoomkat is the only regular I have seen that regularly recommends serial communications using the String object, so you might have to wait for him/her to reply. It would probably be a good idea, to post whatever the updated code is that is still not working.

okay, thanks for your help.