arduino:schlafphasenwecker:programmversion_0.2
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende ÜberarbeitungNächste ÜberarbeitungBeide Seiten der Revision | ||
arduino:schlafphasenwecker:programmversion_0.2 [05.12.2016 10:20] – Frickelpiet | arduino:schlafphasenwecker:programmversion_0.2 [29.01.2017 21:13] – Frickelpiet | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
====== Schlafphasenwecker Programmversion 0.2 ====== | ====== Schlafphasenwecker Programmversion 0.2 ====== | ||
- | {{:arduino: | + | <box 30% green right|**Achtung**> |
Hilfreiche Links: | Hilfreiche Links: | ||
- | | + | * https:// |
- | * https:// | + | |
- | | + | |
* http:// | * http:// | ||
- | * https://www.arduino.cc/en/Reference/Serial | + | * http://henrysbench.capnfatz.com/henrys-bench/arduino-adafruit-gfx-library-user-guide/ |
+ | * http:// | ||
- | Sekunden als hh:mm:ss ausgeben: | + | < |
- | * http://forum.arduino.cc/ | + | // Schlafphasenwecker Version 0.2 |
- | Tags: #Arduino # | + | // Bibliotheken einbinden |
+ | #include < | ||
+ | #include " | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | // Definiert die Pins | ||
+ | #define DCF_PIN 2 // Connection pin to DCF 77 device | ||
+ | #define DCF_INTERRUPT 2 // Interrupt number associated with pin | ||
+ | |||
+ | |||
+ | // Definiert die DCF77-Bibliothek | ||
+ | DCF77 DCF = DCF77(DCF_PIN, | ||
+ | |||
+ | // The FT6206 uses hardware I2C (SCL/SDA) | ||
+ | Adafruit_FT6206 ctp = Adafruit_FT6206(); | ||
+ | |||
+ | // The display also uses hardware SPI, plus #9 & #10 | ||
+ | #define TFT_CS 10 | ||
+ | #define TFT_DC 9 | ||
+ | Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, | ||
+ | |||
+ | // pass in a number for the sensor identifier (for your use later) | ||
+ | Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); | ||
+ | |||
+ | |||
+ | // Definiert die Variablen | ||
+ | bool DCFTimeFound = false; | ||
+ | |||
+ | time_t DCFtime = 0; | ||
+ | time_t t = 0; | ||
+ | |||
+ | const unsigned long DefaultTime = 1477958400; // Nov 1 2016 | ||
+ | unsigned long lastDCFsignal; | ||
+ | unsigned long noDCFsignal; | ||
+ | unsigned long currentDCFsignal; | ||
+ | |||
+ | uint16_t ir; | ||
+ | uint16_t full; | ||
+ | uint32_t lum; | ||
+ | |||
+ | int menuePage = 0; // Nummer des Menüs. Das Menü mit der Nummer 0 soll zuerst aufgerufen werden. | ||
+ | int touchtouched = 0; // Touchscreen gedrückt? | ||
+ | int tx; | ||
+ | int ty; | ||
+ | int debouncetime = 50; // Zeit für Entprellung | ||
+ | unsigned long touchtime = 0; | ||
+ | |||
+ | |||
+ | // Definiert die Tracking-Variablen für die IF-Abfragen | ||
+ | unsigned long previousMillisSetRTC = 0; | ||
+ | unsigned long previousMillisTFTScreen = 0; | ||
+ | unsigned long previousMillisTouchScreen = 0; | ||
+ | unsigned long previousMillisSensorData = 0; | ||
+ | unsigned long previousMillisSerialPrint = 0; | ||
+ | |||
+ | |||
+ | // Definiert die Intervalle für die IF-Abfragen | ||
+ | int intervalSetRTC = 1000; // Delay für Holen der Zeit | ||
+ | int intervalTFTScreen = 50; // Delay für Anteuerung des TFT-Screens | ||
+ | int intervalTouchScreen = 50; // Delay für Anteuerung des kapazitiven Touchsreens | ||
+ | int intervalSensorData = 1000; // Delay für Auslesen der Sensoren | ||
+ | int intervalSerialPrint = 1000; // Delay für serielle Ausgabe | ||
+ | |||
+ | |||
+ | void setup() | ||
+ | // Initalisiert die Pins | ||
+ | // | ||
+ | pinMode(DCF_PIN, | ||
+ | |||
+ | |||
+ | // set the Time library to use Teensy 3.0's RTC to keep time | ||
+ | setSyncProvider(getTeensy3Time); | ||
+ | |||
+ | // Initialisiert den TFT-Bildschirm | ||
+ | tft.begin(); | ||
+ | tft.setRotation(1); | ||
+ | tft.fillScreen(ILI9341_BLACK); | ||
+ | |||
+ | // Initialisiert die serielle Schnittstelle | ||
+ | Serial.begin(115200); | ||
+ | |||
+ | delay(100); | ||
+ | if (timeStatus()!= timeSet) { | ||
+ | Serial.println(" | ||
+ | } else { | ||
+ | Serial.println(" | ||
+ | } | ||
+ | |||
+ | if (! ctp.begin(40)) { // Stellt u.a. die Sensitivität des Touchscreens ein | ||
+ | Serial.println(" | ||
+ | //while (1); | ||
+ | } else { | ||
+ | Serial.println(" | ||
+ | } | ||
+ | |||
+ | // Konfiguration des TSL2591 | ||
+ | // You can change the gain on the fly, to adapt to brighter/ | ||
+ | // | ||
+ | tsl.setGain(TSL2591_GAIN_MED); | ||
+ | // tsl.setGain(TSL2591_GAIN_HIGH); | ||
+ | |||
+ | // Changing the integration time gives you a longer time over which to sense light | ||
+ | // longer timelines are slower, but are good in very low light situtations! | ||
+ | tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); | ||
+ | // tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS); | ||
+ | // tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS); | ||
+ | // tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS); | ||
+ | // tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS); | ||
+ | // tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); | ||
+ | |||
+ | |||
+ | // Startet die Abfrage des DCF77-Moduls | ||
+ | DCF.Start(); | ||
+ | Serial.println(" | ||
+ | Serial.println(" | ||
+ | |||
+ | |||
+ | // Initialisiert den TSL2591 | ||
+ | tsl.begin(); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | void loop() { | ||
+ | // Aktuelle Zeit abfragen | ||
+ | unsigned long currentMillis = millis(); | ||
+ | |||
+ | |||
+ | // Stelle die Real Time Clock (RTC) auf die aktuelle Zeit | ||
+ | if ((unsigned long)(currentMillis - previousMillisSetRTC) >= intervalSetRTC) { | ||
+ | |||
+ | DCFtime = DCF.getTime(); | ||
+ | |||
+ | // Wenn die serielle Schnittstelle verfügbar ist, setze die RTC auf diese Zeit | ||
+ | if (Serial.available() && DCFtime < DefaultTime) { | ||
+ | t = processSyncMessage(); | ||
+ | if (t != 0) { | ||
+ | Teensy3Clock.set(t); | ||
+ | setTime(t); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Wenn ein Zeitsignal vom DCF77-Modul verfügbar ist, setze die RTC auf diese Zeit | ||
+ | if (DCFtime > DefaultTime) { | ||
+ | t = DCFtime; | ||
+ | Serial.println(" | ||
+ | Teensy3Clock.set(t); | ||
+ | setTime(t); | ||
+ | DCFTimeFound = true; | ||
+ | currentDCFsignal = millis(); | ||
+ | } | ||
+ | |||
+ | // Berechne die Zeit (in Sekunden) die seit dem Empfang des letzten gültigen DCF-Signals vergangen ist | ||
+ | if (DCFTimeFound == false) { | ||
+ | noDCFsignal = millis() /1000; | ||
+ | } | ||
+ | else { | ||
+ | lastDCFsignal = (millis() - currentDCFsignal) / 1000; | ||
+ | } | ||
+ | |||
+ | //Speichere die aktuelle Zeit in die zughörige Variable | ||
+ | previousMillisSetRTC = currentMillis; | ||
+ | } | ||
+ | |||
+ | |||
+ | // Sensoren /TSL 2591 | ||
+ | if ((unsigned long)(currentMillis - previousMillisSensorData) >= intervalSensorData) { | ||
+ | |||
+ | //Auslesen des Helligkeitssensors | ||
+ | lum = tsl.getFullLuminosity(); | ||
+ | ir = lum >> 16; | ||
+ | full = lum & 0xFFFF; | ||
+ | |||
+ | //Speichere die aktuelle Zeit in die zughörige Variable | ||
+ | previousMillisSensorData = currentMillis; | ||
+ | } | ||
+ | |||
+ | |||
+ | // TFT-Screen | ||
+ | if ((unsigned long)(currentMillis - previousMillisTFTScreen) >= intervalTFTScreen) { | ||
+ | |||
+ | // Hauptmenü (weiß) | ||
+ | if (menuePage == 0) { | ||
+ | // | ||
+ | tft.fillRect(0, | ||
+ | tft.fillRect(107, | ||
+ | tft.fillRect(214, | ||
+ | } | ||
+ | |||
+ | // Untermenü 1 (rot) | ||
+ | if (menuePage == 1) { | ||
+ | // | ||
+ | tft.fillRect(0, | ||
+ | tft.fillRect(107, | ||
+ | tft.fillRect(214, | ||
+ | } | ||
+ | |||
+ | // Untermenü 2 (gelb) | ||
+ | if (menuePage == 2) { | ||
+ | // | ||
+ | tft.fillRect(0, | ||
+ | tft.fillRect(107, | ||
+ | tft.fillRect(214, | ||
+ | } | ||
+ | |||
+ | // Untermenü 3 (grün) | ||
+ | if (menuePage == 3) { | ||
+ | // | ||
+ | tft.fillRect(0, | ||
+ | tft.fillRect(107, | ||
+ | tft.fillRect(214, | ||
+ | } | ||
+ | |||
+ | //Speichere die aktuelle Zeit in die zughörige Variable | ||
+ | previousMillisTFTScreen = currentMillis; | ||
+ | } | ||
+ | |||
+ | |||
+ | // Touchscreen | ||
+ | if ((unsigned long)(currentMillis - previousMillisTouchScreen) >= intervalTouchScreen) { | ||
+ | |||
+ | // Wait for a touch | ||
+ | if (ctp.touched()) { | ||
+ | // Retrieve a point | ||
+ | TS_Point p = ctp.getPoint(); | ||
+ | touchtime = millis(); | ||
+ | touchtouched = 1; | ||
+ | |||
+ | // Damit der Wert 0, 0 des Touchsensors mit dem Pixel 0, 0 des TFT-Screens übereinstimmt, | ||
+ | tx = map(p.y, 0, 320, 320, 0); | ||
+ | ty = p.x; | ||
+ | } | ||
+ | |||
+ | if (menuePage == 0 && touchtouched == 1 && (millis() - touchtime > debouncetime)) { // Wenn Menüseite 0 ausgewählt und der Touchscreen berührt wird und die debouncetime verstrichen ist ... | ||
+ | if ((tx >= 0) && (tx <= 106) && (ty >= 0) && (ty <= 30)) { // ... und der TouchScreen in dem angegebenen Bereich berührt wird ... | ||
+ | tft.drawRect(0, | ||
+ | menuePage = 1; // ... dann rufe Menüseite 1 auf. | ||
+ | } | ||
+ | else if ((tx >= 107) && (tx <= 213) && (ty >= 0) && (ty <= 30)) { | ||
+ | tft.drawRect(107, | ||
+ | menuePage = 2; | ||
+ | } | ||
+ | else if ((tx >= 214) && (tx <= 320) && (ty >= 0) && (ty <= 30)) { | ||
+ | tft.drawRect(214, | ||
+ | menuePage = 3; | ||
+ | } | ||
+ | touchtouched = 0; // Setze die Varibale auf 0, damit nicht die nächste if-Schleife aufgerufen wird | ||
+ | } | ||
+ | |||
+ | if (menuePage == 1 && touchtouched == 1 && (millis() - touchtime > debouncetime)) { | ||
+ | if ((tx >= 0) && (tx <= 106) && (ty >= 0) && (ty <= 30)) { | ||
+ | tft.drawRect(0, | ||
+ | menuePage = 0; | ||
+ | } | ||
+ | else if ((tx >= 107) && (tx <= 213) && (ty >= 0) && (ty <= 30)) { | ||
+ | tft.drawRect(107, | ||
+ | menuePage = 2; | ||
+ | } | ||
+ | else if ((tx >= 214) && (tx <= 320) && (ty >= 0) && (ty <= 30)) { | ||
+ | tft.drawRect(214, | ||
+ | menuePage = 3; | ||
+ | } | ||
+ | touchtouched = 0; | ||
+ | } | ||
+ | |||
+ | if (menuePage == 2 && touchtouched == 1 && (millis() - touchtime > debouncetime)) { | ||
+ | if ((tx >= 0) && (tx <= 106) && (ty >= 0) && (ty <= 30)) { | ||
+ | tft.drawRect(0, | ||
+ | menuePage = 0; | ||
+ | } | ||
+ | else if ((tx >= 107) && (tx <= 213) && (ty >= 0) && (ty <= 30)) { | ||
+ | tft.drawRect(107, | ||
+ | menuePage = 1; | ||
+ | } | ||
+ | else if ((tx >= 214) && (tx <= 320) && (ty >= 0) && (ty <= 30)) { | ||
+ | tft.drawRect(214, | ||
+ | menuePage = 3; | ||
+ | } | ||
+ | touchtouched = 0; | ||
+ | } | ||
+ | |||
+ | if (menuePage == 3 && touchtouched == 1 && (millis() - touchtime > debouncetime)) { | ||
+ | if ((tx >= 0) && (tx <= 106) && (ty >= 0) && (ty <= 30)) { | ||
+ | tft.drawRect(0, | ||
+ | menuePage = 0; | ||
+ | } | ||
+ | else if ((tx >= 107) && (tx <= 213) && (ty >= 0) && (ty <= 30)) { | ||
+ | tft.drawRect(107, | ||
+ | menuePage = 1; | ||
+ | } | ||
+ | else if ((tx >= 214) && (tx <= 320) && (ty >= 0) && (ty <= 30)) { | ||
+ | tft.drawRect(214, | ||
+ | menuePage = 2; | ||
+ | } | ||
+ | touchtouched = 0; | ||
+ | } | ||
+ | |||
+ | //Speichere die aktuelle Zeit in die zughörige Variable | ||
+ | previousMillisTouchScreen = currentMillis; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | // Ausgabe an die serielle Schnittstelle | ||
+ | if ((unsigned long)(currentMillis - previousMillisSerialPrint) >= intervalSerialPrint) { | ||
+ | |||
+ | // Gibt die aktuelle Zeit aus | ||
+ | Serial.print(hour()); | ||
+ | printDigits(minute()); | ||
+ | printDigits(second()); | ||
+ | Serial.print(" | ||
+ | Serial.print(day()); | ||
+ | Serial.print(" | ||
+ | Serial.print(month()); | ||
+ | Serial.print(" | ||
+ | Serial.print(year()); | ||
+ | printDCFsyncTime(); | ||
+ | Serial.print(menuePage); | ||
+ | Serial.println(); | ||
+ | |||
+ | //Speichere die aktuelle Zeit in die zughörige Variable | ||
+ | previousMillisSerialPrint = currentMillis; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | time_t getTeensy3Time() | ||
+ | { | ||
+ | return Teensy3Clock.get(); | ||
+ | } | ||
+ | |||
+ | |||
+ | /* code to process time sync messages from the serial port */ | ||
+ | #define TIME_HEADER | ||
+ | |||
+ | unsigned long processSyncMessage() { | ||
+ | unsigned long pctime = 0L; | ||
+ | |||
+ | if(Serial.find(TIME_HEADER)) { | ||
+ | | ||
+ | | ||
+ | if( pctime < DefaultTime) { // check the value is a valid time (greater than Nov 1 2016) | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | return pctime; | ||
+ | } | ||
+ | |||
+ | |||
+ | void tftprintDigits(int digits) { | ||
+ | // utility function for digital clock display: prints preceding colon and leading 0 | ||
+ | tft.print(":" | ||
+ | if(digits < 10) | ||
+ | tft.print(' | ||
+ | tft.print(digits); | ||
+ | } | ||
+ | |||
+ | |||
+ | void printDigits(int digits) { | ||
+ | // utility function for digital clock display: prints preceding colon and leading 0 | ||
+ | Serial.print(":" | ||
+ | if(digits < 10) | ||
+ | Serial.print(' | ||
+ | Serial.print(digits); | ||
+ | } | ||
+ | |||
+ | |||
+ | void tftprintDCFsyncTime() { | ||
+ | if (DCFTimeFound == false){ | ||
+ | tft.println(" | ||
+ | tft.print(noDCFsignal); | ||
+ | tft.print(" | ||
+ | } | ||
+ | else { | ||
+ | tft.println(" | ||
+ | tft.print(lastDCFsignal); | ||
+ | tft.print(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | void printDCFsyncTime() { | ||
+ | if (DCFTimeFound == false){ | ||
+ | Serial.print(" | ||
+ | Serial.print(noDCFsignal); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | else { | ||
+ | Serial.print(" | ||
+ | Serial.print(lastDCFsignal); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | Der Sketch verwendet 42.108 Bytes (4%) des Programmspeicherplatzes. Das Maximum sind 1.048.576 Bytes. | ||
+ | Globale Variablen verwenden 6.216 Bytes (2%) des dynamischen Speichers, 255.928 Bytes für lokale Variablen verbleiben. Das Maximum sind 262.144 Bytes. | ||
+ | |||
+ | Tags: #Arduino # |
arduino/schlafphasenwecker/programmversion_0.2.txt · Zuletzt geändert: 18.05.2023 12:34 von 127.0.0.1