/*pin 2 segmento a;sino al pin 8 i setti segmenti il pin 9 il punto decimale dp
* il punto decimale dp, i pin 9,10 11,12 selezionano il display su cui visualizzare la cifra.
* significato dei dei bit del byte dell'array cifra {b7,b6,b5,b4,b3,b2,b1,b0}
* b7 punto decimale
* b6 segmento a
* b5 segmento b
* b4 segmento c
* b3 segmento d
* b2 segmento e
* b1 segmento f
* b0 segmento g
*quando ciascun bit precedente ha il valore 0 il segmento corrispondente Ŕ spento un qualsiasi segmento si illumina qualora il corrispondente bit vale 1
* selettori del demultiplex S2 S1 S0  cifra
*                                      0   0    0  unitÓ
*                                      0   0    1  decina
*                                      0   1    0  centinaia
*                                      0   1    1  migliaia
*/


const int pin[12]={2,3,4,5,6,7,8,9,10,11,12,13};
const int AN0=A0;
const int AN1=A1;

int I,tipo_misura,ritardo=0.5,numero,portata,cifra_decimale=100,valore,cifra1,cifra2,cifra3,cifre_decimali=2;
float q=0.0048999;
float tensione;
float attenuazione=0.05;
boolean unita,decina,centinaia,migliaia;
void uno();void due();void tre();void quattro();
void cinque();void seii();void sette();void otto();
void nove();
void zero();
void dp();
void azzera_desplay();
void visualizza();

void setup()
{

for (I=0;I<12;I++)
{
pinMode(pin[I],OUTPUT);
}
for (I=7;I<12;I++)
{
digitalWrite(pin[I],LOW);

}

}

void loop()
{
tipo_misura=digitalRead(pin[11]);
if (tipo_misura==0)
{
valore=analogRead(AN0); tensione=valore*q*cifra_decimale/attenuazione;
}
else
{
valore=analogRead(AN1); tensione=valore*q*cifra_decimale/attenuazione;
tensione=tensione*1000;
}
dp();



//calcolo migliaia
numero=tensione/1000;cifra1=numero;
if(numero >-1){unita=false;decina=false;centinaia=false;migliaia=true;visualizza();}
//calcolo centinaia
numero=(tensione-cifra1*1000)/100;cifra2=numero;
if(numero>-1){unita=false;decina=false;centinaia=true;migliaia=false;visualizza();}

//calcolo decina
numero=(tensione -cifra1*1000-cifra2*100)/10;cifra3=numero;
if(numero>-1){unita=false;decina=true;centinaia=false;migliaia=false;visualizza();}

// calcolo unitÓ
numero=tensione-cifra1*1000-cifra2*100-cifra3*10;
unita=true;decina=false;centinaia=false;migliaia=false;visualizza();


}

void visualizza()
{


switch(numero)
{
case 0:
{
if (unita==true)
{
digitalWrite(pin[8],0);digitalWrite(pin[9],0);digitalWrite(pin[10],0); }
else{if(decina==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],0);digitalWrite(pin[10],0);}
else{if (centinaia==true)
{digitalWrite(pin[8],0);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}
else{if(migliaia==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}}}}


zero();delay(ritardo);

for(I=8;I<12;I++) digitalWrite(pin[I],LOW);

case 1:
{
if (unita==true)
{
digitalWrite(pin[8],0);digitalWrite(pin[9],0);digitalWrite(pin[10],0); }
else{if(decina==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],0);digitalWrite(pin[10],0);}
else{if (centinaia==true)
{digitalWrite(pin[8],0);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}
else{if(migliaia==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}}}}


uno();delay(ritardo);
for(I=8;I<12;I++)digitalWrite(pin[I],LOW);

}
break;
case 2:
{
if (unita==true)
{
digitalWrite(pin[8],0);digitalWrite(pin[9],0);digitalWrite(pin[10],0); }
else{if(decina==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],0);digitalWrite(pin[10],0);}
else{if (centinaia==true)
{digitalWrite(pin[8],0);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}
else{if(migliaia==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}}}}


due();delay(ritardo);
for(I=8;I<12;I++) digitalWrite(pin[I],LOW);

}
break;
case 3:
{
if (unita==true)
{
digitalWrite(pin[8],0);digitalWrite(pin[9],0);digitalWrite(pin[10],0); }
else{if(decina==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],0);digitalWrite(pin[10],0);}
else{if (centinaia==true)
{digitalWrite(pin[8],0);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}
else{if(migliaia==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}}}}


tre();delay(ritardo);
for(I=8;I<12;I++) digitalWrite(pin[I],LOW);

}break;
case 4:
{
if (unita==true)
{
digitalWrite(pin[8],0);digitalWrite(pin[9],0);digitalWrite(pin[10],0); }
else{if(decina==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],0);digitalWrite(pin[10],0);}
else{if (centinaia==true)
{digitalWrite(pin[8],0);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}
else{if(migliaia==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}}}}


quattro(); delay(ritardo);
for(I=8;I<12;I++) digitalWrite(pin[I],LOW);

}break;
case 5:
{
if (unita==true)
{
digitalWrite(pin[8],0);digitalWrite(pin[9],0);digitalWrite(pin[10],0); }
else{if(decina==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],0);digitalWrite(pin[10],0);}
else{if (centinaia==true)
{digitalWrite(pin[8],0);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}
else{if(migliaia==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}}}}


cinque();delay(ritardo);
for(I=8;I<12;I++) digitalWrite(pin[I],LOW);

}break;
case 6:
{
if (unita==true)
{
digitalWrite(pin[8],0);digitalWrite(pin[9],0);digitalWrite(pin[10],0); }
else{if(decina==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],0);digitalWrite(pin[10],0);}
else{if (centinaia==true)
{digitalWrite(pin[8],0);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}
else{if(migliaia==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}}}}


seii();delay(ritardo);
for(I=8;I<12;I++) digitalWrite(pin[I],LOW);

}
break;
case 7:
{
if (unita==true)
{
digitalWrite(pin[8],0);digitalWrite(pin[9],0);digitalWrite(pin[10],0); }
else{if(decina==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],0);digitalWrite(pin[10],0);}
else{if (centinaia==true)
{digitalWrite(pin[8],0);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}
else{if(migliaia==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}}}}


sette();delay(ritardo);
for(I=8;I<12;I++) digitalWrite(pin[I],LOW);

}
break;
case 8:
{

if (unita==true)
{
digitalWrite(pin[8],0);digitalWrite(pin[9],0);digitalWrite(pin[10],0); }
else{if(decina==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],0);digitalWrite(pin[10],0);}
else{if (centinaia==true)
{digitalWrite(pin[8],0);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}
else{if(migliaia==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}}}}


otto();delay(ritardo);
for(I=8;I<12;I++) digitalWrite(pin[I],LOW);
}
break;
case 9:
{
if (unita==true)
{
digitalWrite(pin[8],0);digitalWrite(pin[9],0);digitalWrite(pin[10],0); }
else{if(decina==true)

{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],0);digitalWrite(pin[10],0);}
else{if (centinaia==true)
{digitalWrite(pin[8],0);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}
else{if(migliaia==true)
{digitalWrite(pin[8],HIGH);digitalWrite(pin[9],1);digitalWrite(pin[10],0);}}}}


nove();delay(ritardo);
for(I=8;I<12;I++) digitalWrite(pin[I],LOW);
}
break;
}
}
}
void uno()
{//{0000110} in binario
azzera_desplay();
digitalWrite(pin[0],0);//seg a
digitalWrite(pin[1],1);//seg b
digitalWrite(pin[2],1);//seg c
digitalWrite(pin[3],0);//seg d
digitalWrite(pin[4],0);//seg e
digitalWrite(pin[5],0);//seg f
digitalWrite(pin[6],0);//seg g


}
void due()
{//{1011011} in binario
azzera_desplay();
digitalWrite(pin[0],1);//seg a
digitalWrite(pin[1],1);//seg b
digitalWrite(pin[2],0);//seg c
digitalWrite(pin[3],1);//seg d
digitalWrite(pin[4],1);//seg e
digitalWrite(pin[5],0);//seg f
digitalWrite(pin[6],1);//seg g

}
void tre()
{//{1001111} in binario
azzera_desplay();
digitalWrite(pin[0],1);//seg a
digitalWrite(pin[1],1);//seg b
digitalWrite(pin[3],1);//seg c
digitalWrite(pin[2],1);//seg d
digitalWrite(pin[4],0);//seg e
digitalWrite(pin[5],0);//seg f
digitalWrite(pin[6],1);//seg g


}
void quattro()
{//{01100110} in binario
azzera_desplay();
digitalWrite(pin[0],0);//seg a
digitalWrite(pin[1],1);//seg b
digitalWrite(pin[2],1);//seg c
digitalWrite(pin[3],0);//seg d
digitalWrite(pin[4],0);//seg e
digitalWrite(pin[5],1);//seg f
digitalWrite(pin[6],1);//seg g
}
void cinque()
{//{01101101} in binario
azzera_desplay();
digitalWrite(pin[0],1);//seg a
digitalWrite(pin[1],0);//seg b
digitalWrite(pin[2],1);//seg c
digitalWrite(pin[3],1);//seg d
digitalWrite(pin[4],0);//seg e
digitalWrite(pin[5],1);//seg f
digitalWrite(pin[6],1);//seg g

}
void seii()
{//{01111100} in binario
azzera_desplay();
digitalWrite(pin[0],0);//seg a
digitalWrite(pin[1],0);//seg b
digitalWrite(pin[2],1);//seg c
digitalWrite(pin[3],1);//seg d
digitalWrite(pin[4],1);//seg e
digitalWrite(pin[5],1);//seg f
digitalWrite(pin[6],1);//seg g

}
void sette()
{//{00000111} in binario
azzera_desplay();
digitalWrite(pin[0],1);//seg a
digitalWrite(pin[1],1);//seg b
digitalWrite(pin[2],1);//seg c
digitalWrite(pin[3],0);//seg d
digitalWrite(pin[4],0);//seg e
digitalWrite(pin[5],0);//seg f
digitalWrite(pin[6],0);//seg g

}
void otto()
{//{01111111} in binario
azzera_desplay();
digitalWrite(pin[0],1);//seg a
digitalWrite(pin[1],1);//seg b
digitalWrite(pin[2],1);//seg c
digitalWrite(pin[3],1);//seg d
digitalWrite(pin[4],1);//seg e
digitalWrite(pin[5],1);//seg f
digitalWrite(pin[6],1);//seg g

}
void nove()
{//{01100111} in binario
azzera_desplay();
digitalWrite(pin[0],1);//seg a
digitalWrite(pin[1],1);//seg b
digitalWrite(pin[2],1);//seg c
digitalWrite(pin[3],0);//seg d
digitalWrite(pin[4],0);//seg e
digitalWrite(pin[5],1);//seg f
digitalWrite(pin[6],1);//seg g

}
void zero()
{//{00111111} in binario
azzera_desplay();
digitalWrite(pin[0],1);//seg a
digitalWrite(pin[1],1);//seg b
digitalWrite(pin[2],1);//seg c
digitalWrite(pin[3],1);//seg d
digitalWrite(pin[4],1);//seg e
digitalWrite(pin[5],1);//segf
digitalWrite(pin[6],0);//seg g

}
void azzera_desplay()
{
for(I=0;I<8;I++)

digitalWrite(pin[I],LOW);

}
void dp()
{
switch(cifre_decimali)
{
case 0:
break;
case 1:
{
digitalWrite(pin[8],HIGH);digitalWrite(pin[7],HIGH);
delay(ritardo);
digitalWrite(pin[8],LOW); digitalWrite(pin[7],LOW);
}
break;
case 2:
{ digitalWrite(pin[9],HIGH);digitalWrite(pin[7],HIGH);
delay(ritardo);
digitalWrite(pin[9],HIGH); digitalWrite(pin[7],LOW);
}


break;
case 3:
{
digitalWrite(pin[10],HIGH);digitalWrite(pin[7],HIGH);
delay(ritardo);
digitalWrite(pin[10],HIGH); digitalWrite(pin[7],LOW);
}
break;

case 4:
{
digitalWrite(pin[11],HIGH);digitalWrite(pin[7],HIGH);
delay(ritardo);
digitalWrite(pin[11],HIGH);digitalWrite(pin[7],LOW);
}
break;
}
}