Arduino, matlab and servo control

The .ino file looks rather complex, can you explain the meaning of the states?

You can compare val like this

if (val < 'a') { ....

makes it a bit more readable imho