Hi! I'm programing an Arduino Leonardo in order to have a clock and a timer showed in a LCD (SainSmart 1.3" SPI Serial 128X64 Blue) I use the RTC DS1307. I want to change the our of the clock and the timer with 2 buttons. The problems I've got are:
- Minutes don't stop at 60 and change into 00.
- Hours don't stop at 24 and change into 00.
- When I press the buttons they don't change the numbers one by one, they change them 6 by 6 or 7 by 7.
- Also the time doesn't start with the time I set.
Any solutions? Thanks
Here is the code.
#include "U8glib.h"
U8GLIB_SH1106_128X64 u8g(7, 6, 4, 5, 9);
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
int but_h = 12;
int but_m = 11;
int val;
int val1;
String texto;
void setup() {
Serial.begin(57600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(12,01,20));
}
pinMode (12, INPUT);
pinMode (11, INPUT);
}
void draw(void)
{
if (val != 1 && val1 != 1) { //mientras los botones no esten pulsados haz
DateTime now = rtc.now();
u8g.setFont(u8g_font_fur20);
u8g.setPrintPos(8, 50);
// u8g.print(texto);
if(now.hour() < 10) {u8g.print('0');} // si horas menor que 10, escribe 0 delante
u8g.print(now.hour(), DEC); //escribe horas
u8g.print(':');
if(now.minute() < 10) {u8g.print('0');} //si minutos menos que 10, escribe 0 delante
u8g.print(now.minute(), DEC);
u8g.print(':');
u8g.print(now.second(), DEC);
}
else buttons(); //si los botones estan pulsados, realiza la funcion botones
}
void loop() {
val = digitalRead(but_m);
val1 = digitalRead(but_h);
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
delay(100);
}
void buttons() {
DateTime now = rtc.now();
if (val1 == 1) rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour () +1, now.minute() , 0));
if (val == 1) rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour() , now.minute () +1, 0));
}