Seriel monitor testing servos

Hi I am working on a project on animated puppets.
I want to use servos for a start.
To test my servos range I made a program where I use the serial monitor to input the data to the write() or writeMicroseconds() functions in the Servo library.
To test it i made the program print the values back to the serial monitor.

But i does not work, and he servos don’t move correct, to simplify the problem i found this program that gives the same outputs: when I input 1 it prints out 49, 2 prints 50 and so on, two digits numbers like 22 prints two times 50.

I’m running winXP on my “arduino laptop” (old laptop) and using a arduino duemilenova.

I need a way to make the serial monitor input the right data(0-2400) in my program.

I hope anyone can help a newcomer.
Thanks Jes


int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte =;

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);}

Below is a simple servo test code I made for tinkering with my servos. Note that it uses the Servo.h library which is included in the arduino IDE download, and the WString.h library, which makes string handling much easier than dealing with arrays.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor, then enter or send.

#include <WString.h>
String readString = String(100);
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {

void loop() {

        while (Serial.available()) {
          if (Serial.available() >0) {
        char c =;
        readString.append(c); }
      if (readString.length() >0) {
      int n;
      n = atoi(readString); //convert string to number

Curious is it possible, to control a servo + arduino, from say a batch file?


batch180 file myservo = n, set n to 180 batch0 file myservo = n, set n to 0

? possible to control a servo from command line dos? or any other way?

You could try something like below in a batch file, noting that a carrage return and line feed are appended to the end of the 180. If the computer serial port is already set for the appropriate the mode line can be skipped.

@echo off
mode com5:9600,N,8,1 >nul
echo 180 >com5

I've set servos to my blinds to open/close hardwired to computer. just hate having to open arduino.exe, and setting to open or closed, would be awesome to have a batch file i could just double click to open or close the darn blinds :)

Thanks zoomkat
Your program worked fine.

But now I on to controlling a servo by using a array to contain the position of the servo.
I’m using this code.
It compiles just fine but when uploaded nothing happens, no input to the serial monitor either.
What i wrong with my code ?

// Controlling a servo position using a array 
// by Jes Troelsen

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
int pos[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 80, 120, 140, 90, 40}; //array to store vanted servo positions

void setup() { 
  myservo.attach(9); //servo at pin 9

void loop()
  for (int x=0; x>14; x++)
      int val=pos[x];
    Serial.print("value sent to servo: ");
      Serial.println(val);                  // srial ptint the value
       myservo.write(val);                  // sets the servo position according to the scaled value 
     delay(250);  // waits for the servo to get there 
for (int x=0; x[glow]>[/glow]14;

I don't know much about array operations, which is why I used the WString.h in my code. In the past I made a blind tilt setup using a servo and controlled it via a web page. Fairly simple setup below.

#include <WString.h>

I’m not finding where to get this library. This looks like the best solution to getting multi-byte inputs from the computer that I’ve found so far. There’s a dozen of them, but none are really easy to use. I suppose strings are different enough you really need both libraries to pull the tricks though.

NEvermind, I finally found it.