Hi !
I know this topic is well disputed, but for me it's an older issue.
This is the program, with a few additions from me.
#include "TM1637.h"
//{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
//0~9,A,b,C,d,E,F,"-"," ",degree,r,h
#define CLK 4//Pins for TM1637
#define DIO 5
TM1637 tm1637(CLK,DIO);
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
//************Button*****************//
int P1=6; // Button SET MENU'
int P2=7; // Button +
int P3=8; // Button -
int menu = 0;
int hh, mm;
int state = 0;
int old = 0;
int pressbutton = 0;
void setup()
{
tm1637.init();
tm1637.set(5);
//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
pinMode(P1,INPUT);
pinMode(P2,INPUT);
pinMode(P3,INPUT);
menu = 0;
rtc.begin();
// Check if the RTC lost power and if so, set the time:
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// The following line sets the RTC to the date & time this sketch was compiled:
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// manual adjust
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
// automatic adjust
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
Serial.begin(9600);
}//end "setup()"
void loop(){
DateTime now = rtc.now();
hh = now.hour(), DEC;
mm = now.minute(), DEC;
// check if you press the p1SET button and increase the menu index
if(digitalRead(P1))
{
menu=menu+1;
}
// in which subroutine should we go?
if (menu==0)
{
Serial.println("P1 Default");
DisplayTime(); // void DisplayDateTime
}
if (menu==1) // if 1
{
Serial.println("P1 pressed 1");
SetHour();
}
if (menu==2) // if 2
{
Serial.println("P1 pressed 2");
SetMinute();
}
if (menu==3) // if 3
{
Serial.println("P1 pressed 3");
SaveTime();
delay(500);
menu = 0;
}
delay(100);
}// end loop()
void DisplayTime(){
tm1637.point(POINT_ON);
if ((hh/10) == 0) tm1637.display(0,17);
else
tm1637.display(0,hh/10); // hour
tm1637.display(1,hh%10);
tm1637.display(2,mm/10); // minutes
tm1637.display(3,mm%10); //
delay(500);
tm1637.point(POINT_OFF);
if ((hh/10) == 0) tm1637.display(0,17);
else
tm1637.display(0,hh/10); // hour
tm1637.display(1,hh%10);
tm1637.display(2,mm/10); // minutes
tm1637.display(3,mm%10); //
delay(500);
}//DisplayTime
void SetHour(){
DateTime now = rtc.now();
if(digitalRead(P2)==HIGH)
{
if(hh==23)
{
hh=0;
}
else
{
hh=hh+1;
}
}
if(digitalRead(P3)==HIGH)
{
if(hh==0)
{
hh=23;
}
else
{
hh=hh-1;
}
}
tm1637.display(0,hh/10); // hour
tm1637.display(1,hh%10);
tm1637.display(2,0);
tm1637.display(3,0);
delay(200);
}//SetHour
void SetMinute(){
// Setting the minutes
if(digitalRead(P2)==HIGH)
{
if (mm==59)
{
mm=0;
}
else
{
mm=mm+1;
}
}
if(digitalRead(P3)==HIGH)
{
if (mm==0)
{
mm=59;
}
else
{
mm=mm-1;
}
}
tm1637.display(0,0); // hour
tm1637.display(1,0);
tm1637.display(2,mm/10);
tm1637.display(3,mm%10);
delay(200);
}//SetMinute
void SaveTime(){
// Variable saving
rtc.adjust(DateTime(F(__DATE__),hh,mm,0));
delay(200);
}//SaveTime
I found a time adjustment method with if functions (I think I will replace them with switch cases) on a DS3231 & LCD project, and it works. It's pretty simple, but on TM6137 it doesn't work, I can switch from if 1 to if 2 or 3 (see the serial monitor message) but the commands for changing the hours or minutes do not have a correct effect. In short, I want to adjust the hours or minutes, I should see how the values increase / decrease on the display, press continuously and for nothing, but after calling the SaveTime function, time changes but chaotic. So the problem would be the chaotic change of time. Any suggestions ?