Serial communication to servos with integers

Hello,

I am working on a project in which the user inputs characters into the Serial port which get translated into integers and sent to the Arduino for controlling two servos.

I have researched how to translate these characters into integers for the servos, and the code seems to work for a single servo. However, when I tried to incorporate a second servo with an if statement, the code works once for one of the servos and thereafter fails for the second one.

I have thoroughly checked my circuitry, and it seems that the problem resides in the program.

Code:

#include<Servo.h> // Servo library
Servo servo1;
Servo servo2;

void setup()
{
servo1.attach(9);
servo2.attach(8); // attach motors to the pins
Serial.begin(9600);
}

int numCollect() // function for char to int conversion in Serial port
{
int numInput= 0;
while(Serial.available() > 0)
{
numInput *= 10;
numInput += (Serial.read() - ‘0’);
delay(10);
}
return numInput;
}

void loop()
{
if(Serial.available() > 0) // checks if there is data
{
if(Serial.read() == ‘D’) // condition for switching from the first servo to the second
{
servo1.write(numCollect());
delay(100); // wait for the motor to get there
}
else
{
servo2.write(numCollect());
delay(100);
}
}

}

Your help is very much appreciated

Thank you

Test code for sending positions to several 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 
// 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
    }
  }
}

x3MasterofElectrons: servo2.attach(8); // attach motors to the pins

This is why it's necessary to use [ code ] tags.

There was a very similar question recently.

Look at the Thread serial input basics which illustrates how to receive and parse data

…R

Thank you for all of your help this has all been very helpful.