#include #include // Module connection pins (Digital Pins) #define CLK 2 #define DIO 3 #define SWPin 4 #define DT 5 #define CLK2 6 #define TON 7 // The amount of time (in milliseconds) between tests #define TEST_DELAY 2000 #include // must be included here so that Arduino library object file references work #include RtcDS3231 Rtc(Wire); TM1637Display display(CLK, DIO); #define countof(a) (sizeof(a) / sizeof(a[0])) void printDateTime(const RtcDateTime& dt) { char datestring[20]; snprintf_P(datestring, countof(datestring), PSTR("%02u/%02u/%04u %02u:%02u:%02u"), dt.Month(), dt.Day(), dt.Year(), dt.Hour(), dt.Minute(), dt.Second() ); Serial.print(datestring); } int zustand = 0; int auslesen() { int d = digitalRead(DT); int c = digitalRead(CLK2); if (d == 1 && c == 1) { zustand = 0; return 0; } if (zustand == 0 ) { if (c == 0) { zustand = 1; return zustand; } if (d == 0) { zustand = -1; return zustand; } } return 0; } int h = 0; int m = 0; void wecker() { while (digitalRead(SWPin) == 0) { delay(1); } uint8_t data_[] = {display.encodeDigit(0),display.encodeDigit(0),display.encodeDigit(0),display.encodeDigit(0)}; data_[1] = data_[1] | 0x80; display.setSegments(data_); delay(1000); while (digitalRead(SWPin)){ uint8_t data_h[] = {display.encodeDigit(h/10),display.encodeDigit(h%10),display.encodeDigit(0),display.encodeDigit(0)}; data_h[1] = data_h[1] | 0x80; display.setSegments(data_h); for (int i= 0; i<1000; i++) { int change = auslesen(); if (change != 0) { if (h+change != -1 && h+change != 24) { h = h+change; } if (h+change == -1) { h = 23; } if (h+change == 24) { h = 0; } break; } } data_h[0] = display.encodeDigit(h/10); data_h[1] = display.encodeDigit(h%10); data_h[2] = display.encodeDigit(0); data_h[3] = display.encodeDigit(0); data_h[1] = data_h[1] | 0x80; display.setSegments(data_h); for (int i= 0; i<1000; i++) { int change = auslesen(); if (change != 0) { if (h+change != -1 && h+change != 24) { h = h+change; } if (h+change == -1) { h = 23; } if (h+change == 24) { h = 0; } break; } } } while (digitalRead(SWPin) == 0) { delay(1); } while (digitalRead(SWPin)){ uint8_t data_m[] = {display.encodeDigit(h/10),display.encodeDigit(h%10),display.encodeDigit(m/10),display.encodeDigit(m%10)}; data_m[1] = data_m[1] | 0x80; display.setSegments(data_m); for (int i= 0; i<1000; i++) { int change = auslesen(); if (change != 0) { if (m+change != -1 && m+change != 60) { m = m+change; } if (m+change == -1) { m = 59; } if (m+change == 60) { m = 0; } break; } } data_m[0] = display.encodeDigit(h/10); data_m[1] = display.encodeDigit(h%10); data_m[2] = display.encodeDigit(m/10); data_m[3] = display.encodeDigit(m%10); data_m[1] = data_m[1] | 0x80; display.setSegments(data_m); for (int i= 0; i<1000; i++) { int change = auslesen(); if (change != 0) { if (m+change != -1 && m+change != 60) { m = m+change; } if (m+change == -1) { m = 59; } if (m+change == 60) { m = 0; } break; } } } //while (digitalRead(SWPin)){ //display.setSegments(data_); //delay(500); //uint8_t data_m[] = {display.encodeDigit(0),display.encodeDigit(0),0,0}; //data_m[1] = data_m[1] | 0x80; //display.setSegments(data_m); //delay(500); //} //while(1){} } void setup() { Rtc.Begin(); RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); printDateTime(compiled); Serial.println(); if (!Rtc.IsDateTimeValid()) { // Common Cuases: // 1) first time you ran and the device wasn't running yet // 2) the battery on the device is low or even missing Serial.println("RTC lost confidence in the DateTime!"); // following line sets the RTC to the date & time this sketch was compiled // it will also reset the valid flag internally unless the Rtc device is // having an issue Rtc.SetDateTime(compiled); } if (!Rtc.GetIsRunning()) { Serial.println("RTC was not actively running, starting now"); Rtc.SetIsRunning(true); } RtcDateTime now = Rtc.GetDateTime(); if (now < compiled) { Serial.println("RTC is older than compile time! (Updating DateTime)"); Rtc.SetDateTime(compiled); } else if (now > compiled) { Serial.println("RTC is newer than compile time. (this is expected)"); } else if (now == compiled) { Serial.println("RTC is the same as compile time! (not expected but all is fine)"); } Rtc.Enable32kHzPin(false); Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); display.setBrightness(0xff); pinMode(TON,OUTPUT); } int counter = 0; void loop() { RtcDateTime now = Rtc.GetDateTime(); //display.showNumberDec(now.Hour()*100+now.Minute(),true); uint8_t data[] = {display.encodeDigit(now.Hour()/10), display.encodeDigit(now.Hour()%10), display.encodeDigit(now.Minute()/10), display.encodeDigit(now.Minute()%10) }; display.setSegments(data); delay(500); data[1]=data[1] | 0x80; display.setSegments(data); delay(500); if (digitalRead(SWPin) == 0) { wecker(); } if (m == now.Minute() && h == now.Hour()) { digitalWrite(TON,HIGH); delay(100); digitalWrite(TON,LOW); } else { digitalWrite(TON,LOW); } }