/*  controllo motore in dc e step by step;

    la variabile tipomotore determina la scelta tra motore dc e motore

             step by step con tipomotore=0 si vuole il motore step by step scrivere il codice, dove

            avvio=1   accenzione motore

             avvio=0 spegnimento motore

*/

                const int LD1=2;

const int LD2=3;

const int LD3=4;

const int LD4=5;

const int LD5=6;

const int incVeloci=7;

const int decVeloci=8;

const int startStop=9;

const int invMarcia=10;

const int tipo=11;

const int LD6=12;

const int LD7=13;

 

int i;

int incremento=0;

int decremento=0;

int velocita=500;

int avvio=0;

int dv=100;

int tipomotore=0;

int senso=0;

int sensopre=0;

int dt=1000;

 

void setup()

{

  pinMode(LD1,OUTPUT);

  pinMode(LD2,OUTPUT);

  pinMode(LD3,OUTPUT);

  pinMode(LD4,OUTPUT);

  pinMode(LD5,OUTPUT);

  pinMode(LD6,OUTPUT);

  pinMode(LD7,OUTPUT);

  pinMode(invMarcia,INPUT);

  pinMode(incVeloci,INPUT);

  pinMode(decVeloci,INPUT);

  pinMode(startStop,INPUT);

  pinMode(tipo,INPUT);

     digitalWrite(LD1,LOW);

    digitalWrite(LD2,LOW);

    digitalWrite(LD3,LOW);

    digitalWrite(LD4,LOW);

    digitalWrite(LD5,HIGH);

    digitalWrite(LD6,LOW);

  }

 

 

 

 

 

  void loop()

  {

      avvio=digitalRead(startStop);

        if(avvio==0)

                {

                    digitalWrite(LD6,LOW);

                        digitalWrite(LD7,LOW);

                }          

 

               tipomotore=digitalRead(tipo);

            while(avvio==1)

            {

                   avvio=digitalRead(startStop);

                        if(tipomotore==1)

                          {

                                 digitalWrite(LD5,LOW);

                                        digitalWrite(LD6,HIGH);

                                         avvio=digitalRead(startStop);

                                                tipomotore=digitalRead(tipo);

                                                  senso=digitalRead(invMarcia);

                                   if(senso==1)

                                       {

                                            digitalWrite(LD7,HIGH);}

                                                   if(senso==0)

                                                         {

                                                              digitalWrite(LD7,LOW);}  

                   

                                                         }

                                                   else

                                                       {

                                                          digitalWrite(LD5,HIGH);

                                                            digitalWrite(LD6,LOW);

                                                                   digitalWrite(LD7,LOW);

                                                                     senso=digitalRead(invMarcia);

                                                                        incremento=digitalRead(incVeloci);

                                                                            decremento=digitalRead(decVeloci);

                                                                 if(incremento==1)

                                                                     {

                                                                            velocita=velocita-dv;

                                                                                if (velocita<0)velocita=490;

                                                                        }

                                                                   if(decremento==1)

                                                                           {

                                                                                velocita=velocita+dv;

                                                                                     if (velocita>2000)velocita=2000;

                                                                              }

 

                                                              if(senso==0)

                                                                {

                                                                     if(senso!=sensopre)

                                                                           {

                                                                                      delay(500);

                                                                             }

                                                     digitalWrite(LD1,HIGH);delay(dt);digitalWrite(LD1,LOW);

                                                                      delay(velocita);

                                                        digitalWrite(LD2,HIGH);delay(dt);digitalWrite(LD2,LOW);

                                                    delay(velocita);

                         digitalWrite(LD3,HIGH); delay(dt);digitalWrite(LD3,LOW);                                                              

                                                                       delay(velocita);

                                        digitalWrite(LD4,HIGH);delay(dt);digitalWrite(LD4,LOW);

                                                                 delay(velocita);

                                                                     sensopre=senso;

                                                            }

                                                    else

                                                            {

                                                                    if(senso!=sensopre)

                                                                              {

                                                                                                delay(500);

                                                                               }

 

                                                                     digitalWrite(LD4,HIGH);delay(velocita);digitalWrite(LD4,LOW);

                                                                      digitalWrite(LD3,HIGH);delay(velocita);digitalWrite(LD3,LOW);

                                                                           digitalWrite(LD2,HIGH);delay(velocita);digitalWrite(LD2,LOW);

                                                                            digitalWrite(LD1,HIGH);delay(velocita);digitalWrite(LD1,LOW);

                                                                                     sensopre=senso;

 

                                                                }

                                                           tipomotore=digitalRead(tipo);

                                                                avvio=digitalRead(startStop);

                                                               incremento=0;

                                                                 decremento=0;

                                                        }

                                        }

    

                               }