Hello I've made an alarm and count down clock and I've got a problem with the hours; when it's 9:59 h and the time has to change to 10:00h the screen writes characters that don't make sens. Could someone help me please?
#include "Time.h"
#include "U8glib.h"
U8GLIB_SH1106_128X64 u8g(7,6,4,5,9);
#include <OneWire.h> //Se importan las librerías
#include <DallasTemperature.h>
OneWire ourWire(A0); //Se establece el pin declarado como bus para la comunicación OneWire
DallasTemperature sensors(&ourWire);
int btn_menu=11; // button menu ----> CAMBIAR PIN3 A PIN2 EN MINIPRO
int btn_hour=12; // button hours
int btn_minute=13; // button minutes
int ah=0; // hours of the alarm
int am=0; // minutes of the alarm
int as=0;
int ah2=0;
int am2=0;
int as2=0;
int th=0; // hours of the time set
int tm=0; // minutes of the time set
int ch=0; // hours of the countdown
int ch2=0;
int cm=0; // minutes of the countdown
int cm2=0;
int cs=0;
int cs2=0;
int varT=0; // contador h/m en set time
int varA=0; // contador h/m en set alarm
int varC=0; // contador h/m en set count
String text,title,temp;
int grados;
int bounce_time=350;
int cont=0;
int state=0;
int a=0;
int b=0;
int c=0;
time_t t;
void setup(){
setTime(00,00,01,01,01,01); // set time
pinMode(A0, INPUT); // temp sensor
pinMode(3,INPUT); // water sensor pin
pinMode(10,OUTPUT); // buzzer
pinMode(btn_menu,INPUT);
pinMode(btn_hour,INPUT);
pinMode(btn_minute,INPUT);
sensors.begin();
Serial.begin(9600);
// attachInterrupt(0, alarm, FALLING); // PIN2 in PRO MINI CUANDO PONGA EL PUTO SENSOR
}
void draw(String title, String text){
u8g.setFont(u8g_font_fur20);
if(title=="0"){ // hace que la pantalla imprima los numeros
u8g.setPrintPos(8,40);
}
else{
u8g.setPrintPos(0,25);
u8g.print(title); // title es la cabecera de la pantalla, indica el modo
u8g.setPrintPos(5,55);
}
u8g.print(text); // imprime el valor (numeros o texto)
text="";
}
void loop(){
u8g.firstPage();
do{
if(digitalRead(btn_menu)==HIGH) button(); // Si se pulsa el boton del menu
settings(); // hace funcion settings
draw(title,text); //imprime las variables
} while(u8g.nextPage());
if(int(hour())==ah&&int(minute())==am&&int(second())==0&int(second())==as) alarm();// si horas=horas de alarma y minutos=minutos de alarma y segundos =segundos alarma hace la funcion alarma
delay(5);
}
void button(){ //funcion boton
cont++; //contador
delay(bounce_time);
if(cont==5) cont=0; //cuando se haya apretado 5 veces el boton menu el contador vuelve a cero
}
void settings(){
switch(cont){ // inicia contador
case 1: //presiona una vez el boton menu
if(second()%3==0){
sensors.requestTemperatures(); // sensor devuelve temperatura
//delay(10);
}
grados=sensors.getTempCByIndex(0)*100.0;
temp=String(grados/100)+"."+(grados%10)+" C"; //Como escribir los grados
title=" TEMP";
text=temp;
if(second()%3==0) cont=0;
break;
case 2: //presiona 2 veces el boton menu
if(varA==0){ //modo alarma
title="Set Alarm"; //aparece en pantalla
text="OK";
if(varC=0){ //si contador count =0
ah=hour();
am=minute();
}
}
else{
title="Set Alarm";
as=0;
text=time(ah,am,as);
varC=0;
}
if(digitalRead(btn_hour)==HIGH){ // si se pulsa el boton de horas
delay(bounce_time);
varA=1; // contador de horas +1
if(ah<23) ah=ah++; //si horas menor que 23 incrementa horas de uno en uno
else if(ah==23) ah=0; // sino 00
}
if(digitalRead(btn_minute)==HIGH){ //si se pulsa el boton de minutos
delay(bounce_time);
varA=1; //contador de horas +1
if(am<59) am=am++; // si minutos menor que 59 incrementa minutos de uno en uno
if(am==59) am=0; // sino 00
}
break;
case 3:
if(varC==0){ // mientras no se toquen los botones de hora o minutos en la pantalla aparecera set count ok
t = now();
title="Set Count";
text="OK";
ch=0;
cm=0;
cs=0;
}
else{
title="Set Count"; // al pulsar los botones de horas o minutos
varA=0;
ch2=ah-int(hour());
cm2=am-int(minute());
cs2=as-int(second());
if(cs2<0){
cs2=as-int(second())+60;
if(cm2>0) cm2=cm2--;
}
if(cm2<0){
cm2=am-int(minute())+60;
if(ch>0) ch=ch--;
}
text=time(ch2,cm2,cs2);
}
if(digitalRead(btn_hour)==HIGH){
delay(bounce_time);
varC=1;
ch=ch++;
setTime(hour(t),minute(t),60,01,01,01);
ah=hour()+ch;
am=minute()+cm;
as=second();
if(am>59) {
am=0;
ah=ah++;
}
if(as>59) {
as=0;
am=am++;
}
}
if(digitalRead(btn_minute)==HIGH){
delay(bounce_time);
varC=1;
if(cm<59) cm=cm++;
if(cm==60) {
cm=0;
ch=ch++;
}
setTime(hour(t),minute(t),60,01,01,01);
ah=hour()+ch;
am=minute()+cm;
as=second();
if(am>59) {
am=0;
ah=ah++;
}
if(as>59) {
as=0;
am=am++;
}
}
break;
case 4:
if(varT==0) {
title=" Set Time";
text="OK";
}
else{
title=" Set Time";
text=time(hour(),minute(),0);
}
if(digitalRead(btn_hour)==HIGH){
delay(bounce_time);
varT=1;
if(hour()<23) setTime(th++,minute(),second(),01,01,01);
else if(ah==23) setTime(0,minute(),second(),01,01,01);
title=" Set Time";
text=time(hour(),minute(),0);
}
if(digitalRead(btn_minute)==HIGH){
delay(bounce_time);
varT=1;
if(am<59) setTime(hour(),tm++,0,01,01,01);
if(am==59) setTime(hour(),0,0,01,01,01);
title=" Set Time";
text=time(hour(),minute(),0);
}
break;
default:
varT=0;
title="0";
text=time(int(hour()),int(minute()),int(second()));
if(second()%5==0)cont=1;
break;
}
}
String time(int a,int b,int c){ // funcion para como poner cero delante del tiempo cuando h/m<10 (Esto no se toca)
if(a<10)text="0";
text=text+a;
text=text+":";
if(b<10)text=text+'0';
text=text+b;
text=text+":";
if(c<10)text=text+'0';
text=text+c;
return text;
}
void alarm(){
for(int x=0;x<5;x++){
if(digitalRead(btn_menu)==HIGH){
alarmOFF();
return;
}
digitalWrite(10, HIGH);
delay(1000);
if(digitalRead(btn_menu)==HIGH){
alarmOFF();
return;
}
digitalWrite(10, LOW);
delay(2000);
if(digitalRead(btn_menu)==HIGH){
alarmOFF();
return;
}
}
if(digitalRead(btn_menu)==HIGH){
alarmOFF();
return;
}
delay(1000);
for(int x=0;x<5;x++){
if(digitalRead(btn_menu)==HIGH){
alarmOFF();
return;
}
digitalWrite(10, HIGH);
delay(1000);
if(digitalRead(btn_menu)==HIGH){
alarmOFF();
return;
}
digitalWrite(10, LOW);
delay(1000);
}
if(digitalRead(btn_menu)==HIGH){
alarmOFF();
return;
}
delay(2000);
for(int x=0;x<5;x++){
if(digitalRead(btn_menu)==HIGH){
alarmOFF();
return;
}
digitalWrite(10, HIGH);
delay(1000);
if(digitalRead(btn_menu)==HIGH){
alarmOFF();
return;
}
digitalWrite(10, LOW);
delay(500);
}
if(digitalRead(btn_menu)==HIGH){
alarmOFF();
return;
}
delay(2500);
digitalWrite(10, HIGH);
if(digitalRead(btn_menu)==HIGH){
alarmOFF();
return;
}
delay(5000);
digitalWrite(10, LOW);
if(digitalRead(btn_menu)==HIGH){
alarmOFF();
return;
}
}
void alarmOFF() {
varA=0;
varC=0;
am=0;
ah=0;
as=61;
digitalWrite(10, LOW);
delay(1500);
}