/*

     dx   sx   azione

     0    0    stop

     0    1    apri(M1)

     1    0    chiudi(M2)

     1    1    memoria azione

 

 

    */

     

        const int M1=5;

          const int M2=6;

            const int start=9;

           const int finecorsa_dx=11;

        const int finecorsa_sx=12;

     const int stop=10;

 

 

      int moto=0;

        int apertura=0;

          int chiusura=0;

            int statostop=0;

            int statodx=0;

          int statosx=0;

        int avvio=0;

      int statoavvio=0;

      int statoavvio_pre=0;

        int ciclo=0;

          int caso=0;

      void setup()

        { 

 

         

              pinMode(finecorsa_sx,INPUT);

                pinMode(finecorsa_dx,INPUT);

                  pinMode(M1,OUTPUT);

                pinMode(M2,OUTPUT);

              pinMode(start,INPUT);

          pinMode(stop,INPUT);

           

              digitalWrite(M1,LOW);

                digitalWrite(M2,LOW);

   

         }

      void loop()

        {

             statoavvio=digitalRead(start);

 

              while( statoavvio==1)

                {       

                  statoavvio=digitalRead(start);

                    if( statoavvio==0)     {avvio=1;}

                  }

      

       

              while(avvio==1)

                {

                    statodx=digitalRead(finecorsa_dx);

                      statosx=digitalRead(finecorsa_sx);

                        statostop=digitalRead(stop);

                          if(statostop==0)

                            {

                              if (statodx==0 && statosx==0 ){caso=0;}// stop motore

                                if(statodx==0 && statosx==1){caso=1;}//apertura

                                  if(statodx==1 && statosx==0){caso=2;}// chiusura

                                    if(statodx==1 && statosx==1){caso=3;}// mantiene la rotazione precedente

                                }

                            else

                                { caso=0;}

                    switch (caso)

                      {

                        case 0:

                                {  

                                  digitalWrite(M1,LOW);digitalWrite(M2,LOW);

                                  moto=0;avvio=0;apertura=0;chiusura=0;

                                   }

                                    break;

                        case 1:

                              {//apertura

                                  if (moto==0)

                                            {

                                                digitalWrite(M1,HIGH);moto=1;apertura=1;

                                                  }

                                              else

                                                  {

                                                    digitalWrite(M2,LOW);

                                                              if(chiusura==1)

                                                                             {

                                                                              chiusura=0;moto=0;avvio=0;

                                                                              }

                                         

                                                  }

                                                  }

                                            break;

                      case 2:

                              {//chiusura

                                  if(moto==0)

                                      {

                                        digitalWrite(M2,HIGH);

                                            chiusura=1; moto=1;

                                      }

                                   else      

                                       {

                                        digitalWrite(M1,LOW);

                                             if(apertura==1)

                                                          {

                                                            apertura=0;moto=0;avvio=0;

                                                          }

                                   }

                                    }

                                break;

                  case 3:

                          {

                

                            moto=1;avvio=1;

                               }

                          break;

              }

            }

    }