// IGE 27.08.2021 // LEd-Timer, Eier-Uhr // LedControl Doku http://wayoda.github.io/LedControl/ // http://wayoda.github.io/LedControl/pages/software.html#SetupBrightness #include "LedControl.h" #include #include #include //#include // FB-Codes Siemens / Fujitsu von TV-Tuner const byte irStart = 0x14; const byte irStop = 0x1A; const byte irOff = 0x16; const byte irReset = 0x11; const byte irIncHour = 0x3; const byte irDecHour = 0x1; const byte irIncMinute = 0x1D; const byte irDecMinute = 0x1F; const byte irIncSecond = 0x1B; const byte irDecSecond = 0x15; // PIN-Definition const byte BuzzerPin = 9; const byte irReceivePin = 8; const byte ledDinPin = 12; const byte ledClkPin = 11; const byte ledCsPin = 10; const byte wakeUpPin = 2; // Digit Position const byte posHour = 7; const byte posMinute = 4; const byte posSecond = 1; // global defaults const byte cHour = 0; const byte cMinute = 7; const byte cSecond = 30; // Lib-Init // DIN, CLK, CS LedControl lc=LedControl(ledDinPin,ledClkPin,ledCsPin,1); IRrecv irrecv(irReceivePin); decode_results results; // global Vars int thour = 0; int tminute = 0 ; int tsecond = 0; int irReceiveCommand = 0x00; // global control bool startCountdown = false; bool startSleep = false; void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(wakeUpPin,INPUT_PULLUP); lc.shutdown(0,false); lc.setIntensity(0,8); lc.clearDisplay(0); lc.setDigit(0, 7, 1, false); lc.setDigit(0, 6, 2, false); lc.setChar(0,5,'-',false); lc.setChar(0,2,'-',false); lc.setDigit(0, 0, 8, false); /* showTouple(posHour,0); showTouple(posMinute,7); showTouple(posSecond,30); */ //lc.clearDisplay(0); //Serial.begin(9600); // Serial.print("Start "); // IR Setup IrReceiver.begin(irReceivePin, ENABLE_LED_FEEDBACK, LED_BUILTIN); // defaults startCountdown = false; thour = cHour; tminute = cMinute ; tsecond = cSecond; // showTimer(); //Serial.begin(115200); } void loop() { if (IrReceiver.decode()) { //IrReceiver.printIRResultShort(&Serial); //Serial.println(); IrReceiver.resume(); // Enable receiving of the next value irReceiveCommand = IrReceiver.decodedIRData.command; switch (irReceiveCommand) { case irOff: if (startSleep) { wakeUpOn(); } else { setSleepOn(); } break; case irStart: startCountdown = true; break; case irStop: startCountdown = false; break; case irReset: startCountdown = false; thour = cHour; tminute = cMinute ; tsecond = cSecond; showTimer(); break; // Timer Einstellung case irIncSecond: if (startCountdown == false) { tsecond++; if(tsecond > 59) { tsecond=0;} showTimer(); } break; case irDecSecond: if (startCountdown == false) { tsecond--; if(tsecond < 0) { tsecond=59;} showTimer(); } break; case irIncMinute: if (startCountdown == false) { tminute++; if(tminute > 59) { tminute=0;} showTimer(); } break; case irDecMinute: if (startCountdown == false) { tminute--; if(tminute < 0) { tminute=59;} showTimer(); } break; case irIncHour: if (startCountdown == false) { thour++; if(thour > 99) { thour=0;} showTimer(); } break; case irDecHour: if (startCountdown == false) { thour--; if(thour < 0) { thour=99;} showTimer(); } break; default: irReceiveCommand = 0x00; if (startSleep) { setSleepOn(); } break; } } if ( startCountdown ) { countdown(); } if( (thour == 0) and (tminute==0) and (tsecond==0) and (startCountdown) ){ lc.clearDisplay(0); myAlarmTone(); // Hier gibt es scheinbar ein Timer.-Problem mit der Tone-Lib , daher neu initialisieren lc=LedControl(ledDinPin,ledClkPin,ledCsPin,1); lc.shutdown(0,false); showTimer(); startCountdown = false; } } void countdown() { if (tsecond == 0) { if(tminute > 0 ) { tminute--; showTouple(posMinute,tminute); tsecond = 59; } else { if(thour > 0) { thour--; tminute = 59; tsecond = 59; } } } else { tsecond--; } showTouple(posHour,thour); showTouple(posMinute,tminute); showTouple(posSecond,tsecond); delay(1000); } // Zeigt ein Touple Stunde/Minute oder die Sekunden an void showTouple(int pos, int value) { int no; no = value % 10; lc.setDigit(0, pos-1, no, false); no = (value / 10) % 10; lc.setDigit(0, pos, no, false); } void showTimer() { showTouple(posHour,thour); showTouple(posMinute,tminute); showTouple(posSecond,tsecond); lc.setChar(0,5,'-',false); lc.setChar(0,2,'-',false); } void myAlarmTone() { int duration = 300; int del = 900; for (int i = 0; i < 4; i++) { // IR-Lib und Tone benuten den selben Timer NewTone(BuzzerPin, 2200, duration); delay(del); } noNewTone(); pinMode(BuzzerPin,INPUT); } void setSleepOn() { startSleep = true; lc.shutdown(0,true); sleep_enable();//Enabling sleep mode attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2 set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep sleep_cpu();//activating sleep mode // Hier geht es weiter } void wakeUpOn() { startCountdown = false; lc.shutdown(0,false); showTimer(); startSleep = false; } void wakeUp(){ sleep_disable();//Disable sleep mode detachInterrupt(0); //Removes the interrupt from pin 2; }