Arduino Double Servo sketch

I have been playing with two servos on my Arduino, here are some videos of it. I wanted to see how stable it is to control multiply servos.

In the first video you can see a pre-programmed motion with each servos.



On the second video I control the servos with direct commands to the Arduino.

The code is quite simple, could be more elegant but perfect to teach a beginner.

 #include <Servo.h> 

  Servo myservo1;
  Servo myservo2;

  uint8_t chosen;
  uint8_t whereTo;

  void setup() 
  { 
    Serial.begin(9600);
    myservo1.attach(9);
    myservo2.attach(6);
  } 

  void loop() 
  { 
    Serial.println("Choose the servo to move: [1-2].");

    while(!Serial.available())
      ; // Wait for something to appear on the input

    switch(Serial.read())
    {
    case '1':
      chosen = 1;
      break;
    case '2':
      chosen = 2;
      break;
    default:
      Serial.println("No such servo.");
      chosen = 0;
    }

    Serial.println("Choose X to move servo to X*20 degree: [0-9].");  

    while(!Serial.available())
      ; // Wait for something to appear on the input

    switch(Serial.read())
    {
    case '0':
      whereTo = 0;
      break;
    case '1':
      whereTo = 1;
      break;
    case '2':
      whereTo = 2;
      break;
    case '3':
      whereTo = 3;
      break;
    case '4':
      whereTo = 4;
      break;
    case '5':
      whereTo = 5;
      break;
    case '6':
      whereTo = 6;
      break;
    case '7':
      whereTo = 7;
      break;
    case '8':
      whereTo = 8;
      break;
    case '9':
      whereTo = 9;
      break;
    default:
      Serial.println("Bad X.");
      chosen = 0;
    }

    switch(chosen)
    {
      case 1:
        myservo1.write(whereTo*20);
        delay(50);
        break;
      case 2:
        myservo2.write(whereTo*20);
        delay(50);
        break;
      default:
        ;
    }

  }

 


Advertisements

2 thoughts on “Arduino Double Servo sketch

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s