Serial Servo problem

Hi everyone,

I am having a big problem with my Arduino's (I have 3) which I really don't understand anymore.

Something has happened and I have no idea what has gone wrong: these files have worked perfectly while demonstrating last week.

I am using Processing to send 2 variables with identifiers (X and Y) to 2 different servo's. The signal wires of the servo's are connected to an Arduino, while they are being powered through an external power supply (500mA, so there is no problem with too little power available in the Arduino to work - had that before). The Arduino itself is powered by an external 300mA power supply. The arduino in this setting seems to be working normally: doing some hello world led tests show it does what it's told.

However, the servo's are instead of doing what they're told doing complete random movements, which I have not told them to. What is going wrong here? Even the tests (like Todbots simple moving script (http://todbot.com/arduino/sketches/servo_move_simple/servo_move_simple.pde )) do not work correctly.

I am using this Arduino code:

int s1 = 11;
int s2 = 12;
int led = 13;

int id;
int val1, val2;
int lastRead1, lastRead2;
int servoVal1, servoVal2;

void setup() {
  
  Serial.begin(115200); 
  
  pinMode(s1, OUTPUT);  
  pinMode(s2, OUTPUT);  
  pinMode(led, OUTPUT);
  
  servoVal1 = 0;
  servoVal2 = 0;

  digitalWrite(led, HIGH);
}

void loop(){

  while (Serial.available() > 1){ 
    id = Serial.read(); 
    //servo 1:
    if (id == 88) { // ascii for 'X'
      val1 = Serial.read();  
      if (val1!=lastRead1) {
        servoVal1 = val1;
      }
      lastRead1 = val1; 
    }
    //servo 2:
    if (id == 89) { // ascii for 'Y'
      val2 = Serial.read();  
      if (val2!=lastRead1) {
        servoVal2 = val2;
      }
      lastRead2 = val2; 
    }

  }
  moveServos();
}

void moveServos(){
  //servo s1 responding to X
  digitalWrite(s1, HIGH);   
  delayMicroseconds((servoVal1 * 20) + 500); 
  digitalWrite(s1, LOW);   
  delay(10); //usual servodelay divided by 2

  //servo s2 responding to Y
  digitalWrite(s2, HIGH);   
  delayMicroseconds((servoVal2 * 20) + 500);      
  digitalWrite(s2, LOW);   
  delay(10); //usual servodelay divided by 2
}

And this is the Processing code:

import processing.serial.*;

Serial myPort;       

int x,y;
int klik;

void setup(){
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 115200);
  size(256,256);
  background(0);
}

void draw(){
  if (x != mouseX){
    myPort.write('X');
    myPort.write(x/2);
  }
  x = mouseX;
  //
  if (y != mouseY){
    myPort.write('Y');
    myPort.write((height-y)/2);
  }
  y = mouseY;



}

void mousePressed(){
  myPort.write('M');
  myPort.write(0);
}

void mouseReleased(){
  myPort.write('M');
  myPort.write(1);
}

As said above, the absurdness is that it worked perfectly. Nothing has changed, yet now it does not work anymore. The serial ports are set correctly, the serial speeds also, all seems to be connected as should be (walked through the wiring 10 times or so, rewired half a dozen times).

To be sure it wasnt my servo's playing up I tried it with other ones: same problem. To be sure it wasnt my Arduino playing up I tried it with another one: dito. To be sure it wasnt my PC, I tried another one: same problem.

HELP!! (pretty desperate)

Well, it was no problem with my codes, the problem was clearified in the post of KC about his servo problems: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1164234873/3#3

I also did not have an extra cable from the negative DC adapter cable to the ground of the arduino. apparently that is a only problem during the setup() routine, because if the cable is unplugged during the loop() there is no problem at all: servo's work fine.

I have no idea howcome my setup worked fine during the previous demonstration, as I really didn't think of that extra wire myself back then.. Als I can't remember having used such a wire during my previous project (http://youtube.com/watch?v=NW5fCQQUnAA)