Friday, 23 October 2015

Arduino: Read Instruction From Serial

When using Arduino, parsing serial data and converting it to something usable is way harder than it seems.

While this functionality is specially useful as it allows your computer to talk with the Arduino easily, a quick search on google will give you thousands of very different solutions. The one exposed here is just another one that is working perfectly for me.

The instruction:

The idea is to set the position of three servos by serial and also be able to change other parameters.
The instruction, then, must be composed from an instruction name and the parameters, for example, to move the three servos:

MOVE 120 23 90

And, if we also want to switch on a light, the instruction could be:


How do we get the Arduino to know that we want the servos to move and then switch on a light?


Arduino Read Instruction:

This script is made up from snippets found in stackoverflow and other
google results

void setup()
  // open serial port at 115200 baud

String complete_instruction[4];  // will contain the decoded instruction (4 fields)
void loop()
  while(!Serial.available()) {}  // if there is nothing on serial, do nothing
  int  i = 0;
  char raw_instruction[25];
  while (Serial.available())
  {  // if something comes from serial, read it and store it in raw_instruction char array
    delay(10); // delay to allow buffer to fill
    if (Serial.available() > 0)
      raw_instruction[i] =;
  if( strlen( raw_instruction ) > 0 )  // if a new raw_instruction has been read
    // clean raw_instruction before decoding (overwrite non filled array positions with empty spaces)
    for( int n = i; n < 25; n++ ) { raw_instruction[n] = ' '; }
    // decode the instruction (4 fields) (iterator n = field, iterator j = character)
    int j = 0;
    for( int n = 0; n < 4; n++ )
      while( j < 25 )
        if( raw_instruction[j] == ' ' )
          complete_instruction[n] += raw_instruction[j];
    // print decoded instruction by serial
    for( int n = 0; n<4; n++ )
      Serial.println( complete_instruction[n] );
      // clear field after using it

The summary is that you can send by serial something like "A B C D" (using space as delimiter) and the variable complete_instruction will contain {A, B, C, D}. A,B,C,D can be any kind of data of any length you want, for the instruction MOVE 120 23 90, it will return {"MOVE", "120", "23", "90"}. For the second instruction LIGHT ON, it will return {"LIGHT", "ON", "", "" }.

This way, now you just need to set several if conditions to check if the instruction says "MOVE", "LIGHT", "KILL" or whatever, and if it does, check the remaining parameters (you can use toInt() or toFloat() for numeric values).

My experience with this script is that it works as intended and does not cause memory problems. But I do not have enough knowledge about C to assure that it will not crash your script (from what I've read, String variables together with low RAM space seem to give problems.) Just take it into account if something very weird is happening. (code updated, memory problem solved?)