/******************************************************************************* * * Projeto 49: Relógio Digital Arduino * http://squids.com.br/arduino * *******************************************************************************/ #include // Inicializa o display no endereco 0x27 com MÓDULO I2C LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3, POSITIVE); // inicializa Time display em 12:59:45 PM (altere se desejar) int h=12; int m=59; int s=45; int flag=1; //PM // Botões de ajuste de hora e minutos int button1; int button2; // Definição dos botões de ajuste int hs=0;// pin 0 para Horas (for Hours Setting) int ms=1;// pin 1 para Minutos (for Minutes Setting) // Para maior precisão, utilize Arduino Real Time Clock e não apenas delay() static uint32_t tempo_corrido, now = 0; // Real Timae Control //Definições para o backlight int setLight_ini = 60; int setLight = setLight_ini; int backlight = 1; void setup() { pinMode(hs,INPUT_PULLUP); // botão 1 (hora) pinMode(ms,INPUT_PULLUP);// botão 2 (minutos) now=millis(); // lê hora atual lcd.begin(16,2); lcd.setBacklight(HIGH); } void loop(){ // Mostra TIME em horas, minuto e segundo + AM/PM lcd.setCursor(0,0); lcd.print("Time "); if(h<10)lcd.print("0");// sempre 2 dígitos lcd.print(h); lcd.print(":"); if(m<10)lcd.print("0"); lcd.print(m); lcd.print(":"); if(s<10)lcd.print("0"); lcd.print(s); if(flag==0) lcd.print(" AM"); if(flag==1) lcd.print(" PM"); lcd.setCursor(1,1);// linha 2 lcd.print("Digital Clock"); for (int i=0;i<5;i++) { while (now - tempo_corrido < 200) { now = millis(); } tempo_corrido = now; //le botões se estão acionados ou não button1=digitalRead(hs);// lê botões se estão acionados button2=digitalRead(ms); // ajusta hora e minutos if ((!button1 && backlight == 1) || (!button2 && backlight == 1)) { setLight = setLight_ini; setTimer(); } // acende o backlight if ((!button1 && backlight == 0) || (!button2 && backlight == 0)) { lcd.setBacklight(HIGH); setLight = setLight_ini; backlight = 1; } } // fim do for bakclight(); //soma segundos s=s+1; makeTimer(); } // fim do loop void setTimer() { if(!button1){ h=h+1; makeTimer(); displaySetTimer(); } if(!button2){ s=0; m=m+1; makeTimer(); displaySetTimer(); } } void makeTimer() { // ---- cria segundos, minutos, hora e flag ---- if(s==60){ s=0; m=m+1; } if(m==60) { m=0; h=h+1; } if(h==13) { h=1; flag=flag+1; if(flag==2)flag=0; } } void displaySetTimer() { // Mostra TIME em horas, minuto e segundo + AM/PM lcd.setCursor(0,0); lcd.print("Time "); if(h<10)lcd.print("0");// sempre 2 dígitos lcd.print(h); lcd.print(":"); if(m<10)lcd.print("0"); lcd.print(m); lcd.print(":"); if(s<10)lcd.print("0"); lcd.print(s); if(flag==0) lcd.print(" AM"); if(flag==1) lcd.print(" PM"); lcd.setCursor(1,1);// linha 2 lcd.print("Digital Clock"); } void bakclight() { setLight--; if (setLight == 0) { lcd.setBacklight(LOW); backlight = 0; } }