 
 public char time200 = 0;
 //public char time4 = 0;
+public int sonicValue=0;
+public int irValue=0;
+public int encoderSpeed = 0;
+
+public boolean buttonValue= false;
+public boolean slotValue= false;
+
+public int SerialState=0;
+
 
 void timerInterrupt(){
   //read switch at a granulartiy of 0.02s

  static int timer_count = 0;

  timer_count++;
  if(timer_count % 4 == 0) {
    //0.02 second reached
    ButtonBuf = ButtonBuf * 2 + digitalRead(BUTTON);
    SlotBuf = SlotBuf *2 + digitalRead(SLOT); 
    } 
  if(timer_count ==200) {
    //0.1 secound reached
    time200 = 1;

    timer200 = 0;
    }
  //Check serial port
  int len = Serial.available();                                                              
  if ((Serial.available() >0 )&&(SerialState == 0)) {                             //when the serial input is ready                                 
    CharReceived=Serial.read();                                                   //read the character
    if ((CharReceived == 'a' )||(CharReceived == 'w') ||(CharRecieved == 's')) {  //if it is in the char list, transist the serial state into 1, reset the delay signal
      SerialState = 1;
      Serialdelay=0;
    }
  }
  
  
  if ((SerialState ==1) && (Serialdelay>1))                                          //if already receive a letter, and 5ms passed
    {
    if (Serial.available() > 0) {                                                      
      ValReceived = Serial.parseInt();
           
      Serial.print(ValReceived);                                   
        switch(CharReceived){
          case 'a': setServoAngle(ValReceived);break;                                //apply the command on motor        
          case 'w': setDCspeed(ValReceived);break;
          //case 'd': analogWrite(LedBluePin, 255-ValReceived);break;
          case 's': setStepperAngle(ValReceived);break;
          //case 'D': analogWrite(LedBluePin, 255-ValReceived);break;
          }
        SerialState = 0;                                                              //transist back to receiving letters
        break;      
      }   
      else                                                                               //anything else are garbage, return letter polling
      { 
      SerialState=0;
      break;      
      } 
    }
  

}
  
