 void loop() 
 {
   String cmd;
-  while (Serial.available()) 
-  {
-    delay(3);  //delay to allow buffer to fill 
-    if (Serial.available() > 0) 
-    {
-      char c = Serial.read();  //gets one byte from serial buffer
-      cmd += c; //makes the string readString
-    } 
-  }
+  build(cmd);
 
   if(cmd.length() > 3)
   {    
-    State state;
-    state.spd = cmd[1] * 5;
-    state.steps =  cmd[3] + (cmd[2] << 8);
-    state.running = true;
-    state.cw = cmd[0] & 0x10;
-  
-    switch (cmd[0] & 0x0f)
+    State state(cmd);
+  
+    switch (state.mode)
     {
       case 1  :  wave(&state);  break;
       case 2  :  full(&state);  break;

//!  struct storing the state information
class State
{
  public:  
  unsigned spd;
  unsigned steps;
  boolean  running;    
  boolean  cw;
};

void wave(const State* state);
void full(const State* state);
void half(const State* state);
void micr(const State* state);
void pwm(unsigned pin, unsigned period, unsigned duty);
void indicate(boolean running);

void setup() 
{                

{
    digitalWrite(indicator, running);
}

//!  @brief  :  receive the command from pc.
void build(String& cmd)
{
  while (Serial.available()) 
  {
    delay(3);  //delay to allow buffer to fill 
    if (Serial.available() > 0) 
    {
      char c = Serial.read();  //gets one byte from serial buffer
      cmd += c; //makes the string readString
    } 
  }
}
