MultiSerial Echo Errors

Hello,
I am building a big spider robot and want to control it over PC ( Processing).
I need to send servo angles over serial to the arduino.
But like always there are problems…
I tried it with a very simple sketch to find the errors and test the approach.
(You need to ignore all the servo stuff, that is unimportant.)
just type a number into the serialmonitor and behind it a letter a,b,c or d (“120a”)and it will show it to you in response. (“a 120”)

#include <Servo.h>

Servo servo1, servo2; 


void setup() {

  pinMode(1,OUTPUT);
  servo1.attach(9); //analog pin 0
  //servo1.setMaximumPulse(2000);
  //servo1.setMinimumPulse(700);

  servo2.attach(10); //analog pin 1
  Serial.begin(19200);
  Serial.println("Ready");

}

void loop() {

  static int v = 0;

  if ( Serial.available()) {
    char ch = Serial.read();

    switch(ch) {
      case '0'...'9':
        v = v * 10 + ch - '0';
        break;
      case 'a':
        Serial.print("   a   ");
        Serial.println(v);
       
        v = 0;
        break;
      case 'b':
        Serial.print("   b   ");
        Serial.println(v);
        v = 0;
        break;
      case 'c':
        Serial.print("   c   ");
        Serial.println(v);

        break;
      case 'd':
        Serial.print("   d   ");
        Serial.println(v);

        break;
    }
  }

What I need is to put more than one of these in a row for example “120a40b56c180d”
There the probleme begins.
The serial puts out wrong data…
I inserted “120a40b56c180d (enter)” a couple of times and that was the output:

   a   120
   b   40
   c   56
   d   -9356
   a   15768
   b   40
   c   56
   d   -9356
   a   15768
   b   40
   c   56
   d   -9356
   a   15768
   b   40
   c   56
   d   -9356
   a   15768
   b   40
   c   56
   d   -9356
   a   15768
   b   40
   c   56
   d   -9356

My robot nearly broke itself because of that and my hand :stuck_out_tongue:
Does someone have any ideas?

In cases 'c' and 'd', you don't set 'v' to zero - is this intentional?

You are beautiful! thank you. It's funny how blind I get when I work too long on a thing :P

Do you know if there is a better way to do such a serial comunication, because I need to adress more than 28 Servos?

You could push the bit rate to 57600, or higher.
Or you could only send the changes to the servo angles - every few samples you should send the actual value.
You could save bandwidth and send as binary, or as ASCII hex.

Lots of ways to do it, but the method you choose depends on how well your serial link performs and is immune to noise.

(BTW, I’m really not beautiful)