Wednesday, December 22, 2010

Orologio

Ecco come realizzare un'orologio con Arduino senza usare nessun RTC(Real Time Clock) ovviamente la prima volta dovrà essere settato e dovrà restare alimentato come tutti gli orologi normali.

Avete bisogno di:
1x LCD Hitachi HD44780
2x Bottoni

Dopo aver montato l'LCD, bottoni e aver effettuato i collegamenti uploadate lo sketch in Arduino.

Dopodichè seguite le istruzioni sul Display.

Funzioni:
Primo bottone: Aumenta il valore
Secondo bottone: Sceglie tra Ore,Minuti e conferma.

#include <LiquidCrystal.h>
#include <Button.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Button buttonadd = Button(6,PULLDOWN);
Button buttonconf = Button(7,PULLDOWN);

int h = 0;
int m = 0;
int s = 0;
int step = 0;

void setup() {
  lcd.begin(16, 2);   

  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);  

  lcd.print("Imposta l'ora");
  makestring();
}

void loop() {
  if (buttonadd.uniquePress()){
    if (step ==0){   
      if (h == 23)
      {
        h=0;
      }
      else
      {
        h++;
      }
      lcd.clear();
      lcd.print("Imposta l'ora");
      makestring();           
    }

    if (step ==1){   
      if (m == 59)
      {
        m=0;
      }
      else
      {
        m++;
      }

      lcd.clear();
      lcd.print("Imposta i min");
      makestring();      
    }
  }

  if (buttonconf.uniquePress()){
    if (step ==1){   
      step++;
    }

    if (step ==0){   
      step++;      

      lcd.clear();
      lcd.print("Imposta i min"); 
      makestring(); 
    }  
  }
  
  if (step == 2)
  {  
    startTimeEngine();
  }
}

void startTimeEngine() {  
  lcd.clear();
  lcd.print("Orologio");
  
  if (s == 59){
    m++;
    s = 0;
  }
  else
  {
    s++;
  }

 if (m == 60){
    h+=1;
    m = 0;
  }  
  
  if (h == 24){
    h=0;
  } 
  
  makestring();
  
  delay(1000);
}

void makestring() {  
  lcd.setCursor(0,1);

  if (h<10){
    lcd.print("0");
    lcd.print(h);
  }
  else
  {
    lcd.print(h);
  }

  lcd.print(":");

  if (m<10){
    lcd.print("0");
    lcd.print(m);
  }
  else
  {
    lcd.print(m);
  }

  lcd.print(":");

  if (s<10){
    lcd.print("0");
    lcd.print(s);
  }
  else
  {
    lcd.print(s);
  }
}

Countdown su LCD

Sostituisci tutti i 5000 con i secondi desiderati e modifica 250 per il refresh dell'lcd tipo con 250 vedrai apparire 4750,4500 ecc


#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);  
  lcd.print("Countdown");

  pinMode(7,OUTPUT);
  digitalWrite(7,HIGH);
}

int ms = 5000;
int refreshms = 250;
int restart = 0;

void loop() {
  if (restart== 0){ 
    for (int i = 1; i <= 5000 / refreshms ; i++){    
      lcd.clear();
      lcd.print("Countdown");
      lcd.setCursor(0, 1);

      ms -= refreshms;
      lcd.print(ms);
      delay(refreshms);

      if (i == 5000 / refreshms)
      {
        //operazioni da eseguire alla fine del countdown
        ms = 5000;
        restart = 1;// mettere 0 se si vuole far ripartire il countdown quando finisce
      }   
    } 
  }
}