Understanding Serial

Hi im trying to controll a servo from a computer using arduino, but i simply cant understand the code in the arduino example, iv broken it down to bare minimals, thx for the help in advance :slight_smile:

#include <Servo.h>

Servo servo1;

void setup() {




void loop() {

static int v = 0;


  char ch = Serial.read(); //START  this is the part i dont understand
  switch(ch){case '0'...'9':
             v = v * 10 + ch - '0';
             case 's':
             v = 0;
             break;                  //END


The code reads the character. into "ch". If it is a numeric digit (which is a letter, not a numeric value) it accumulates this into "v". If it ses a "5" the value in "v" is set to 5 (the letter "5" minus the letter "0" is the numeric value 5). If it then sees a "3" the 5 is multiplied by 10 and the 3 added so it becones 50+3 = 53 as the string that arrived was the letters "53". And so on, until the letter "s" is seen on the input. Then the accumulated value is sent to positin the servo and "v" is reset to zero.

thx for the reply! sounds kinda hard! :cold_sweat: im a newbe in both programming an arduino, whats the easyest way to transfer something with a program using serial and get the same thing in arduino?

That is it. This is not deemed “hard”, that is the way it is done internally. This little code snippet is optimized (or “limited” if you are not optimistic :wink: ) for integers only.

Arduino has partially wrapped this function in a function. Read up on http://arduino.cc/en/Serial/ParseInt and related stuff on http://arduino.cc/en/Reference/Serial