Getting my second servo to work

Hello,
Im pretty new at programming and making sketches. Im trying to control 2 servos through the serial monitor however only one turns. When I put in 1 or 0 the first servo works fine. When i type 4 or 6 the second servo doesnt work. Im not sure if its the code thats wrong or if maybe theres not enough power going to the servo (if thats possibble). The servo does kinda flick when i connect the arduino to the usb port.

Heres my code:
char INBYTE;
#include <Servo.h>

Servo myservo;// create servo object to control a servo
Servo myservo2; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup()
{
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(4); // attaches the servo on pin 4 to the servo object
}

void loop()
{
Serial.println(“Press 1 to turn Arduino pin 9 servo to 75 or 0 to turn it to 5:”); // goes from 5 degrees to 75 degrees
while (!Serial.available()); // stay here so long as COM port is empty
INBYTE = Serial.read(); // read next available byte // in steps of 1 degree
if( INBYTE == ‘0’ ) myservo.write(5); // if it’s a 0 (zero) turn servo to 5 degrees
if( INBYTE == ‘1’ ) myservo.write(75); // if it’s a 1 (one) turn servo to 75 degrees

Serial.println(“Press 4 to turn Arduino pin 4 servo to 180 or 6 to turn it to 0:”); // goes from 0 degrees to 180 degrees
INBYTE = Serial.read(); // read next available byte // in steps of 1 degree
if( INBYTE == ‘4’ ) myservo2.write(0); // if it’s a 4 (zero) turn servo to 0 degrees
if( INBYTE == ‘6’ ) myservo2.write(180); // if it’s a 6 (one) turn servo to 180 degrees
}

You are forgetting to wait for a byte to become available in the second case.

if maybe theres not enough power going to the servo (if thats possibble).

Don’t power servos from the arduino if possible. Use an external power source. Servo test code below.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    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);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

Thanks for that sketch (thats something that i can use for my final sketch :slight_smile: ). The servo is working and it also works when connected to the arduino. I put the "while" in my sketch but its still not working :confused:

I put the "while" in my sketch but its still not working

...and I'm not going to show you what my code looks like now, or describe what "not working" means.

Sorry heres the code:

char INBYTE;
#include <Servo.h>

Servo myservo;// create servo object to control a servo
Servo myservo2; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup()
{
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(4); // attaches the servo on pin 4 to the servo object
}

void loop()
{
Serial.println(“Press 1 to turn Arduino pin 9 servo to 75 or 0 to turn it to 5:”); // goes from 5 degrees to 75 degrees
while (!Serial.available()); // stay here so long as COM port is empty
INBYTE = Serial.read(); // read next available byte // in steps of 1 degree
if( INBYTE == ‘0’ ) myservo.write(5); // if it’s a 0 (zero) turn servo to 5 degrees
if( INBYTE == ‘1’ ) myservo.write(75); // if it’s a 1 (one) turn servo to 75 degrees

Serial.println(“Press 4 to turn Arduino pin 4 servo to 180 or 6 to turn it to 0:”); // goes from 0 degrees to 180 degrees
while (!Serial.available()); // stay here so long as COM port is empty
INBYTE = Serial.read(); // read next available byte // in steps of 1 degree
if( INBYTE == ‘4’ ) myservo2.write(0); // if it’s a 4 (zero) turn servo to 0 degrees
if( INBYTE == ‘6’ ) myservo2.write(180); // if it’s a 6 (one) turn servo to 180 degrees
}

The second servo is still not moving when i press 4 or 6 the first servo is working fine and i now know thanks to zoomkats sketch its not a power problem.
Thanks

Hi nitromt2.

You've been around for almost a year now.
Maybe it's time to read the manual and find out how to ask questions the correct way.

You might as well start reading about how a servo is controlled, and discover why using pin 4 to control a servo will get you into trouble.

Lots of success !

and discover why using pin 4 to control a servo will get you into trouble.

Now, this is getting interesting.

MAS3:
discover why using pin 4 to control a servo will get you into trouble.

And why is that? Just in case I missed that memo, I tried it and it works fine...

You're right, it does work.
I've just tried it myself (and didn't before i posted).
I posted that because i remembered that you need a PWM output to control a servo and pin 4 isn't marked to be PWM.
Turns out i've misread or didn't remember correct , using servo will cost you some PWM outputs, but PWM isn't needed for it.
So sorry for this mistake of mine.

, using servo will cost you some PWM outputs, but PWM isn't needed for it

The Servo library generates its own PWM (low frequency (50Hz), low duty-cycle (~5-10%)), at the cost of ordinary analogWrite PWM on pins 9 and 10 (on a Uno or similar)

Thanks for all the replies I did try my servo 1 on Pin 4 and it did work. So i have may found another issue, when i connect my servo number 2 to pin 9 it still doesnt work. I'm using an arduino nano for the moment. I've read up on a lot of info on servo's but they still confuse me a lot. Maybe I should just scrap the code I was using and start from scratch because I modified this sketch from turning on LEDs. Thanks for all the help and comments.

How are you sending characters to your sketch?
With the serial monitor?
With or without line-endings?

You aren't clearing the serial buffer before outputing the second message - so you'll see the newline character
in the second read I think.

Why are you prompting twice BTW - it would be simpler to just read once and dispatch.

I’m using the serial monitor with “no line ending”. My setup also has bluetooth which does work but since I’m still trying to get that second servo to work I,ve disconnected the BT chip and I’m just using serial monitor.

MarkT, I'm sorry I'm not really sure how to clear the serial buffer do I use while

(Serial.available())
  Serial.read();

And by promting you mean I should get rid of

 Serial.println("Press 4 to turn Arduino pin 4 servo to 180 or 6 to turn it to 0:");  // goes from 0 degrees to 180 degrees

and just combine it with the first serial.println?
Thanks.

What if you try to control the first servo by anything else than "0" and "1" ?
So what if you test for "2" and "3", and what if you press wrong keys ?
And do you need to read serial, then control servo 1, and then read serial again and control servo 2 ?
Or is reading serial and then controlling the corresponding servo also an option ?

If I control the first servo with anything else then 1 or 0 nothing happens. I have now plugged in my first servo in to pin 4 which I was using for my second servo and I discoverd if I press 4 and 6 then the servo does work. So I think that the arduino board just can't or doesn't want to control the other servo (its a different brand). Reading the serial and then controlling the corresponding servo is a option. Thanks for the reply

Ok I just grabbed a servo from an RC car and this one does work so now both servos are working :slight_smile: one thing though when I write 1, 0, 4, or 6 I have to type it then send it and then type it and send it again for it to respond. Any ideas?

Use a proper terminal emulator, like hyperterm or putty