#include <Arduino.h>
#include <U8g2lib.h>
#include "RTClib.h"
RTC_DS1307 rtc; // set the real time clock module
U8G2_MAX7219_32X8_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 11, /* data=*/ 12, /* cs=*/ 10, /* dc=*/ U8X8_PIN_NONE, /* reset=*/ U8X8_PIN_NONE);
char time_string[10]; // string to hold the current time to be displayed
const byte pins = 3;
const byte adjustPin[] = {2, 3, 5};
bool adjustMerker[3] = {false};
uint32_t adjustPressTime[3] = {0};
const uint32_t debounceTime = 50;
int lastSecond = 66; // Merker für letzte Sekunde
DateTime now;
void setup(void)
{
pinMode(adjustPin, INPUT_PULLUP);
pinMode(4, OUTPUT); // we are using pin 4 to power the real time clock module,
digitalWrite(4, HIGH); // since the +5V pin is already used for the display
u8g2.begin(); // begin function is required for u8g2 library
u8g2.setContrast(200); // set display contrast 0-255
if (! rtc.begin()) // start the RTC module
{
abort();
}
// following line sets the RTC to the date & time this sketch was compiled
// uncomment this when uploading to Arduino, otherwise the time will be 0:00 and not increasing
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop(void)
{
now = rtc.now(); // get current time
timeAdjust();
if (now.second() != lastSecond)
{
lastSecond = now.second();
displayData();
}
}
void timeAdjust()
{
for (byte b = 0; b < pins; b++)
{
if (!digitalRead(adjustPin[b])) // Taste gedrückt? (editiert)
{
if (adjustMerker[b] == false) // Vorher nicht gedrückt
{
adjustMerker[b] = true; // Merken dass gedrückt wurde
adjustPressTime[b] = millis(); // Zeit merken wann gedrückt wurde
char dateAdjust[10] = {'\0'};
char timeAdjust[10] = {'\0'};
switch (b)
{
case 0: // erster Pin - Stunde -
sprintf(timeAdjust, "%02d:%02d:%02d", now.hour() + 1, now.minute(), now.second());
break;
case 1: // zweiter Pin - Minute +
sprintf(timeAdjust, "%02d:%02d:%02d", now.hour(), now.minute() + 1, now.second());
break;
case 2: // dritter Pin - Minute -
sprintf(timeAdjust, "%02d:%02d:%02d", now.hour(), now.minute() - 1, now.second());
break;
}
sprintf(dateAdjust, "%02d:%02d:%02d", now.day(), now.month(), now.year());
sprintf(timeAdjust, "%02d:%02d:%02d", now.hour() + 1, now.minute(), now.second());
rtc.adjust(DateTime(dateAdjust, timeAdjust));
displayData(); // Sofort anzeigen
}
sprintf(time_string, "%02d:%02d", now.hour(), now.minute()); // add hours, colon symbol and minutes into a string
}
else // Taste nicht gedrückt
if (adjustMerker[b] == true) // Aber vorher gedrückt
{
if (millis() - adjustPressTime[b] > debounceTime) // DebounceTime abgelaufen
{
adjustMerker[b] = false; // Merker löschen
}
}
}
}
void displayData()
{
u8g2.setFont(u8g2_font_t0_11b_tn); // choose a suitable font
u8g2.clearBuffer(); // clear the internal u8g2 memory
u8g2.drawStr(2, 8, time_string); // draw the time string to the display
u8g2.sendBuffer(); // transfer internal memory to the display
}
muss das nicht
if (!digitalRead(adjustPin[b]))
sein?
ja.
Danke, ich probiere es aus bin mir nur nicht sicher ob ich es heute schaffe.
Hallo nochmal, ich habs gerade ausprobiert es funktioniert auch alles das problem ist nur das die sich die Stunden beim drücken des Tasters zwar erhöhen dies endet aber leider bei 24 nicht und die Uhr zählt dann 25,26 etc.
#include <Arduino.h>
#include <U8g2lib.h>
#include "RTClib.h"
//int now.hour;
int uhr = 0;
RTC_DS1307 rtc; // set the real time clock module
U8G2_MAX7219_32X8_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 11, /* data=*/ 12, /* cs=*/ 10, /* dc=*/ U8X8_PIN_NONE, /* reset=*/ U8X8_PIN_NONE);
char time_string[10]; // string to hold the current time to be displayed
void setup(void)
{
pinMode(2, INPUT_PULLUP);
pinMode(4, OUTPUT); // we are using pin 4 to power the real time clock module,
digitalWrite(4, HIGH); // since the +5V pin is already used for the display
u8g2.begin(); // begin function is required for u8g2 library
u8g2.setContrast(200); // set display contrast 0-255
if (! rtc.begin()) // start the RTC module
{
abort();
}
// following line sets the RTC to the date & time this sketch was compiled
// uncomment this when uploading to Arduino, otherwise the time will be 0:00 and not increasing
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop(void)
{
DateTime now = rtc.now(); // get current time
uhr = digitalRead(2);
if ( uhr == 0)
{
char dateAdjust[10] = {'\0'};
char timeAdjust[10] = {'\0'};
sprintf(dateAdjust, "%02d:%02d:%02d", now.day(), now.month(), now.year());
sprintf(timeAdjust, "%02d:%02d:%02d", now.hour() + 1, now.minute(), now.second());
rtc.adjust(DateTime(dateAdjust, timeAdjust));
}
sprintf(time_string, "%02d:%02d", now.hour(), now.minute()); // add hours, colon symbol and minutes into a string
u8g2.setFont(u8g2_font_t0_11b_tn); // choose a suitable font
u8g2.clearBuffer(); // clear the internal u8g2 memory
u8g2.drawStr(2, 8, time_string); // draw the time string to the display
u8g2.sendBuffer(); // transfer internal memory to the display
delay(1000); // wait one second
}
Ja das ist das alte Programm ich wollte es aber zuerst mit dem probieren.
Danke
Ich hatte ja schon geschrieben, dass es die schlechteste lib ist...
Aber gut, Du willst es so.
Du musst Dir eine Variable definieren.
Dann musst Du diese mit dem Wert füllen aus .hour()+1 und dann die Prüfung mit entsprechender Reaktion selbst machen.
Das Gleiche passiert Dir im Übrigen bei den Minuten auch.
Edit: Am Beispiel Stunde
case 0: // erster Pin - Stunde -
byte myHour = now.hour() + 1;
if (myHour > 23)
{ myHour = 0; }
sprintf(timeAdjust, "%02d:%02d:%02d", myHour, now.minute(), now.second());
break;