sabato 25 ottobre 2014

CRONOMETRO HIGH/LOW



Lo sketch permette di cronometrare il tempo di ON e di OFF di un onda quadra.


   /* Cronómetro modificato, creado por 
      Javier Sirgo Blanco http://zygzax.com/webpcronometro/
      
      Ingresso al pin 2.
      Misura il tempo di ON e di OFF di un segnale ad onda quadra.
      I due tempi si azzerano al reinizio del ciclo.
  */
    
    #include <LiquidCrystal.h>                
   
    # define SET   2
   
    LiquidCrystal lcd(8, 9, 10, 11, 12, 13); 
    int m, mu=0,md=0;                          
    int s, su=0,sd=0;
    int l, lu=0,ld=0,lc=0;
    int  pin2;
    long int tempo,inizio;
         
    void setup()
        
         { pinMode(SET, INPUT);                      
           pin2=LOW;
           lcd.begin(16, 2);                      
           lcd.setCursor(0, 0); 
           lcd.print("ON");         
           lcd.setCursor(5, 0);                  
           lcd.print("00:00:000");                
           lcd.setCursor(0, 1); 
           lcd.print("OFF");         
           lcd.setCursor(5, 1);                  
           lcd.print("00:00:000");
                                }
     
    
    void loop() {
//**************** Tempo in cui il pin 2 è HIGH ********************
      pin2=digitalRead(2);                 
      if ( pin2==HIGH)
         { inizio=millis();                      
         //Cronometra e trasforma in sessagesimali il tempo di ON
           while (pin2==HIGH)
                { tempo=millis()-inizio;            
                  m=(tempo/1000)/60;                 
                  mu=m%10;                           
                  md=(m-mu)/10;                       
                  s=(tempo/1000)%60;                 
                  su=s%10;                          
                  sd=(s-su)/10;                       
                  l=(tempo%1000);                    
                  lu=l%10;                            
                  ld=((l-lu)/10)%10;                  
                  lc=(l-(ld*10)-lu)/100;              
               // Stampa sulla prima riga il tempo di ON
                  lcd.setCursor(5, 0);                
                  lcd.print(md);                      
                  lcd.print(mu);              
                  lcd.print(":");
                  lcd.print(sd);
                  lcd.print(su);
                  lcd.print(":");
                  lcd.print(lc);
                  lcd.print(ld);
                  lcd.print(lu);
                  pin2=digitalRead(SET); } } // Fine sottoprogramma tempo di HIGH
                  
 //**************** Tempo in cui il pin 2 è LOW ********************                 
      pin2=digitalRead(2);                 
      if ( pin2==LOW)
         { inizio=millis();                      
         //Cronometra e trasforma in sessagesimali il tempo di OFF
           while( pin2==LOW)
                { tempo=millis()-inizio;            
                  m=(tempo/1000)/60;                 
                  mu=m%10;                           
                  md=(m-mu)/10;                       
                  s=(tempo/1000)%60;                 
                  su=s%10;                          
                  sd=(s-su)/10;                       
                  l=(tempo%1000);                    
                  lu=l%10;                            
                  ld=((l-lu)/10)%10;                  
                  lc=(l-(ld*10)-lu)/100;              
               // Stampa sulla seconda riga il tempo di OFF
                  lcd.setCursor(5, 1);                
                  lcd.print(md);                      
                  lcd.print(mu);              
                  lcd.print(":");
                  lcd.print(sd);
                  lcd.print(su);
                  lcd.print(":");
                  lcd.print(lc);
                  lcd.print(ld);
                  lcd.print(lu);
                  pin2=digitalRead(SET); }}  // Fine sottoprogramma tempo di LOW                   
                                            
           }//----------Fine Programma

Nessun commento:

Posta un commento