String readStrin;
int i=0, j=0, flag =0;
void setup(){
    Serial.begin(9600);

    //LINEA CLEAR -  (parte sopra)
    //LINEA PRESEST = LINEA CLEAR
    //Cont
    pinMode(A0, OUTPUT);//dato primo cont.
    digitalWrite(A0, LOW);// A0 (Primo contatore)
    pinMode(A1, OUTPUT);//dato secondo cont.
    digitalWrite(A1,LOW);//A1 (secondo contatore)
    pinMode(A2, OUTPUT);//dato terzo cont.
    digitalWrite(A2,LOW);//A2 (terzo contatore)
    //clear+prese
    pinMode(A3, OUTPUT);//A3 (clear+preset)
    digitalWrite(A3, HIGH);
    //clock
    pinMode(A4,OUTPUT);//A4 clock
    digitalWrite(A4, HIGH);
   
   //Reg data
   pinMode(6, OUTPUT); //reg 1
   pinMode(7, OUTPUT); //reg 2
   pinMode(8, OUTPUT); //reg 3
   pinMode(9, OUTPUT); //reg 4
   pinMode(10, OUTPUT); //reg 5
   pinMode(11, OUTPUT); //reg 6
   pinMode(12, OUTPUT); //reg 7
   pinMode(13, OUTPUT); //reg 8
   digitalWrite(5, LOW);
   digitalWrite(6, LOW);
   digitalWrite(7, LOW);
   digitalWrite(8, LOW);
   digitalWrite(9, LOW);
   digitalWrite(10, LOW);
   digitalWrite(11, LOW);
   digitalWrite(12, LOW);
   digitalWrite(13, LOW);
}

void loop(){
   if (Serial.available() >0) {
      delay(29);
      readStrin="";
      while(Serial.available()){
        char c = Serial.read();  //gets one byte from serial buffer
        readStrin += c; //makes the string readString
      }
      //Serial.println(readStrin);
     if(readStrin[0] == 'd'){
        for(j=1; j<8; j++){
           if(readStrin[j] == '0')digitalWrite(13-j, LOW); else  digitalWrite(13-j, HIGH);
           digitalWrite(A4,LOW);
           digitalWrite(A4,HIGH);
        }
      }
      if(readStrin[0] == 'm'){
         for(j=1; j<4; j++){
             if(readStrin[j] == '0'){
                digitalWrite(13+j, LOW); 
              }else{
                digitalWrite((13+j), HIGH);
              }
              digitalWrite(A4,LOW);
              digitalWrite(A4,HIGH);
          }
      }
   }   
}
