Benutzer-Werkzeuge

Webseiten-Werkzeuge


arduino:schlafphasenwecker:programmversion_0.3

Dies ist eine alte Version des Dokuments!


Schlafphasenwecker Programmversion 0.3

Achtung

Dies ist eine alte Programmversion.

Da viele weckertypische Funktionen (zum Beispiel eine Weckfunktion) eine Eingabemöglichkeit erfordern, legt diese Programmversion den Fokus auf das grafische Menü.

  • Der Touchsensor löst erst aus, wenn es losgelassen wird.
  • Das Backlight dunkelt nach einer einstellbaren Zeit ab und wird erst bei Berührung wieder aktiviert.
  • Damit das TFT nicht flackert, wird es nur aktualisiert, wenn es nötig ist.
  • Die Kommentierung des Programmcodes wurde ergänzt.
  • Das Adafruit 9-DOF IMU Breakout - L3GD20H + LSM303 wird eingebunden.

ToDos:

Hilfreiche Links:

// Schlafphasenwecker Version 0.2

// Bibliotheken einbinden
#include <TimeLib.h>               // Stellt verschiedene Zeitfunktionen zur Verfügung
#include <TimeAlarms.h>            // Stellt verschiedene Alarmfunktionen zur Verfügung
#include "DCF77.h"                 // Bibliothek für das DCF77-Modul
#include <Adafruit_GFX.h>          // Core graphics library
#include <SPI.h>                   // this is needed for display
#include <Adafruit_ILI9341.h>      // TFT-Display
#include <Wire.h>                  // this is needed for FT6206
#include <Adafruit_FT6206.h>       // Kapazitiver Touchsensor
#include <Adafruit_Sensor.h>       // Adafruit Unified Sensor Driver
#include "Adafruit_TSL2591.h"      // Bibliothek für den Lichtsensor TSL2591
#include <Adafruit_LSM303_U.h>     // Bibliothek für den Beschleunigungssensor LSM303
#include <Adafruit_Simple_AHRS.h>
#include <Fonts/FreeSans9pt7b.h>  // Font
//#include <Fonts/FreeSans18pt7b.h>  // Font
//#include <Fonts/FreeSans24pt7b.h>  // Font

// Definiert die Pins
#define DCF_PIN 2                  // Connection pin to DCF 77 device
#define DCF_INTERRUPT 2            // Interrupt number associated with pin
#define TFTbackLightPin 7          // PWM Backlight TFT

// Definiert die Farben für das Menü (im RGB585-Format)
#define white 0xFFFF               // weiß
#define black 0x0000               // schwarz
#define red 0xF800                 // rot
#define orange  0xFB20             // orange

// Definiert die DCF77-Bibliothek
DCF77 DCF = DCF77(DCF_PIN, DCF_INTERRUPT, false); 

// 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                  // ChipSelect-Signal an Teensy-Pin 10
#define TFT_DC 9                   // Data/Command-Signal an Teensy-Pin 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

//  pass in a number for the sensor identifier (for your use later)
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);

/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Accel_Unified accel(30301);
Adafruit_LSM303_Mag_Unified   mag(30302);

// Create simple AHRS algorithm using the above sensors.
Adafruit_Simple_AHRS          ahrs(&accel, &mag);

// Definiert die Variablen
boolean IMUconnected = false;                 // Wird wahr, wenn die IMU gefunden wurde.
boolean DCFtimesignalFound = false;       // Wird wahr, sobald ein DCF77-Zeitzeichen erfolgreich empfangen wurde.

time_t DCFtime = 0;                       // Das aktuelleste erfolgreich empfangene Zeitzeichen im Variablenformat time_t (Sekunden seit dem 1.1.1970)
time_t t = 0;                             // Die Zeit der Real Time Clock im Variablenformat time_t (Sekunden seit dem 1.1.1970)
time_t DefaultTime = 1477958400;          // Die bis zum 1.11.2016 verstrichene Zeit in Sekunden seit dem 1.1.1970.
unsigned long lastDCFsignal;              // Zeit in Millisekunden, sie seit dem letzten erfolgrich empfangenen DCF77-Zeitzeichen vergangen ist.
unsigned long noDCFsignal;                // Zeit in Millisekunden, die seit dem Systemstart bis zum ersten erfolgreich empfangenen DCF-Zeitzeichen vergangen ist.
unsigned long currentDCFsignal;
unsigned long receivedDCFsignals = 0;     // Zählt die Anzahl der erfolgreich empfangenen DCF77-Zeitzeichen seit dem Systemstart.
float DCFsuccessRate = 0;                 // Wert für die Quote der erfolgreich empfangenen DCF77-Zeitzeichen seit dem Systemstart.

uint16_t ir;                              // Helligkeitswert für den Infrarotanteil des Lichts
uint16_t full;                            // Helligkeitswert für das Infrarotanteil und das sichtbare Licht
uint32_t lum;

boolean TFTbackLightStatus = false;       // Wird wahr, sobald das TFT Backlight an ist
byte TFTbrightness = 0;                   // Wert für die aktuelle Helligkeit des TFT Backlights
//byte TFTmaxbrightness = 255;            // Wert für die maximale Helligkeit des TFT backlights
//byte TFTminbrightness = 0;              // Wert für die minimale Helligkeit des TFT backlights
const unsigned long TFTbackLightdelay = 20000; // Zeit in Millisekunden, nach der das TFT Backlight abgeschaltet wird.
unsigned long TFTbackLightTime = 0;   
boolean touchtouched2 = false;            // Wird true, wenn der Touchscreen berührt wird
boolean forceRefresh = false;             // Wird true, wenn ein Menü kontinuierlich aktualisiert werden soll

//int i;                                  // Zählwert für Fadein und Fadeout des TFT Backlights

byte menuPage = 0;                        // Wert für das aktuell auf dem TFT angezeigten Menü. Gestartet wird mit Menü Nr. 0.
byte previousmenuPage = 0;                // Wert für die Menüseite, die in dem vormaligen Durchlauf aktuell war.
boolean touchtouched = false;             // Wird true, wenn der Touchscreen berührt wird
int tx;                                   // Auf dem Touchscreen gedrücktes Pixel auf der x-Achse. Pixelspalte Nr. 0 ist links.
int ty;                                   // Auf dem Touchscreen gedrücktes Pixel auf der y-Achse. Pixelzeile Nr. 0 ist oben.
const unsigned long debouncetime = 5;     // Zeit für "Entprellung" der Eingaben auf dem Touchscreen.
unsigned long touchtime = 0;              // Speichert die Zeit, zu der der Touchscreen berührt wird.


float accelX;
float accelY;
float accelZ;

float roll;
float pitch;
float heading;


// 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 in Millisekunden
const unsigned long intervalSetRTC = 1000;        // Delay für Holen der Zeit
const unsigned long intervalTFTScreen = 100;       // Delay für Anteuerung des TFT-Screens
const unsigned long intervalTouchScreen = 50;     // Delay für Anteuerung des kapazitiven Touchsreens
const unsigned long intervalSensorData = 1000;    // Delay für Auslesen der Sensoren
const unsigned long intervalSerialPrint = 1000;   // Delay für serielle Ausgabe


void setup()  {
  // Initalisiert die Pins
  pinMode(DCF_PIN, INPUT_PULLUP);                 // DFC77-Modul
  pinMode(TFTbackLightPin, OUTPUT);               // PWM für TFT Backlight
  
  // 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);

  // Zeigt auf dem TFT einen Startbildschirm an
  //StartupScreen();
  
  delay(100);
  if (timeStatus()!= timeSet) {
    Serial.println("Unable to sync with the RTC");
  } else {
    Serial.println("RTC has set the system time");
  }

  if (! ctp.begin(40)) {                               // Stellt u.a. die Sensitivität des Touchscreens ein
    Serial.println("Couldn't start FT6206 touchscreen controller");
    //while (1);
  } else {
    Serial.println("Capacitive touchscreen started");
  }

  /* Initiolisiere den Beschleunigungssensor LSM303 */
  if(!accel.begin()) {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
    //while(1);
  } else {
    IMUconnected = true;
  }
  if(!mag.begin()) {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
    //while(1);
  }

  
// Konfiguration des TSL2591
  // You can change the gain on the fly, to adapt to brighter/dimmer light situations
  //tsl.setGain(TSL2591_GAIN_LOW);    // 1x gain (bright light)
  tsl.setGain(TSL2591_GAIN_MED);      // 25x gain
  // tsl.setGain(TSL2591_GAIN_HIGH);   // 428x gain
  
  // 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);  // shortest integration time (bright light)
  // 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);  // longest integration time (dim light)


  // Startet die Abfrage des DCF77-Moduls
  DCF.Start();
  Serial.println("Waiting for DCF77 time ... ");
  Serial.println("It will take at least 2 minutes until a first update can be processed.");


  // Initialisiert den TSL2591
  tsl.begin(); //Lichtsensor

  // Die folgenden Einstellungen sorgen dafür, dass das TFT Backlight nach dem Systemstart abdunkelt. 
  // (Die Variable TFTbackLightTime wird ansonsten erst nach dem Berühren des Touchscreens auf millis gesetzt.)
  TFTbackLightTime = millis();
  TFTbackLightStatus = true;
}


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();              // Check if new DCF77 time is available

  // 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);             // set the RTC
      setTime(t);
    }
  }

  // Wenn ein Zeitsignal vom DCF77-Modul verfügbar ist, setze die RTC auf diese Zeit
  if (DCFtime > DefaultTime) {
    t = DCFtime;
    Serial.println("RTC has been updated to DCF77 time");
    Teensy3Clock.set(t);               // set the RTC
    setTime(t);
    DCFtimesignalFound = true;
    receivedDCFsignals = receivedDCFsignals + 1;
    currentDCFsignal = millis();
  }

  // Berechne die Zeit (in Sekunden) die seit dem Empfang des letzten gültigen DCF-Signals vergangen ist
  if (DCFtimesignalFound == false) {
    noDCFsignal = millis() /1000;
  }
  else {
    lastDCFsignal = (millis() - currentDCFsignal) / 1000;
  }

  // Berechnet die Quote erfolgreich empfangener DCF77-Signale seit dem letzten Systemstart
  DCFsuccessRate = (receivedDCFsignals / ((millis() / 60000))) * 100;
  
  //Speichere die aktuelle Zeit in die zughörige Variable
  previousMillisSetRTC = currentMillis;
  }


// Abfrage verschiedener Sensoren
  if ((unsigned long)(currentMillis - previousMillisSensorData) >= intervalSensorData) {

    if (IMUconnected == true) {
      // Auslesen des Beschleunigungs- / Lagesensors und Magnetometers LSM303
      sensors_event_t event;
      accel.getEvent(&event);
      sensors_vec_t   orientation;
      ahrs.getOrientation(&orientation);

      accelX = event.acceleration.x;
      accelY = event.acceleration.y;
      accelZ = event.acceleration.z;

      roll = orientation.roll;
      pitch = orientation.pitch;
      heading = orientation.heading;
    }

  
  //Auslesen des Helligkeitssensors TSL2591
  lum = tsl.getFullLuminosity();
  ir = lum >> 16;
  full = lum & 0xFFFF;

  //Speichere die aktuelle Zeit in die zughörige Variable
  previousMillisSensorData = currentMillis;
  }


// Bildschirmanzeige
  if (((unsigned long)(currentMillis - previousMillisTFTScreen) >= intervalTFTScreen) && (previousmenuPage != menuPage || forceRefresh == true)) {

  forceRefresh = false;

// Die Nummerierung der Menüseiten erfolgt nach dem folgendendem Muster:
// Hauptmenü = 0; von dort Verzweigungen zu den Untermenüs 1 bis 5

  // Hauptmenü
  if (menuPage == 0) {
    // Linke Spalte des Menüs
    tft.fillRect(0, 0, 120, 59, white);
    tft.fillRect(0, 60, 120, 59, white);
    tft.fillRect(0, 120, 120, 59, white);
    tft.fillRect(0, 180, 120, 59, white);

    tft.setTextColor(black, white);
    tft.setFont(&FreeSans9pt7b);
    tft.setCursor(10, 35);
    tft.print("Snoozle");
    tft.setCursor(10, 95);
    tft.print("Ziffernblatt");
    tft.setCursor(10, 155);
    tft.print("Einstellung");
    tft.setCursor(10, 215);
    tft.print("Information");

    //Rechte Spalte des Menüs
    
    }

// Untermenü "Wecker"
  if (menuPage == 5) {
    // Zurück zum Hauptmenü
    tft.fillRect(0, 0, 49, 240, white);

    // Wecker stellen
    tft.setTextColor(white, black);
    tft.setFont(&FreeSans9pt7b);
    tft.setCursor(50, 120);
    tft.print("Menue Wecker stellen");
    }

// Untermenü "Snoozle"
  if (menuPage == 1) {
    // Zurück zum Hauptmenü
    tft.fillRect(0, 0, 49, 240, white);

    // Snoozle stellen
    tft.setTextColor(white, black);
    tft.setFont(&FreeSans9pt7b);
    tft.setCursor(50, 120);
    tft.print("Menue Snoozle stellen");
    }

// Untermenü "Ziffernblatteffekte"
  if (menuPage == 2) {
    // Zurück zum Hauptmenü
    tft.fillRect(0, 0, 49, 240, white);

    // Effekte einstellen
    tft.setTextColor(white, black);
    tft.setFont(&FreeSans9pt7b);
    tft.setCursor(50, 120);
    tft.print("Menue Effekte einstellen");
    }

  // Untermenü "Einstellungen"
  if (menuPage == 3) {
    // Zurück zum Hauptmenü
    tft.fillRect(0, 0, 49, 240, white);

    // Einstellungen stellen
    tft.setTextColor(white, black);
    tft.setFont(&FreeSans9pt7b);
    tft.setCursor(50, 120);
    tft.print("Menue Einstellungen");
    }

  // Untermenü "Informationen"
  if (menuPage == 4) {
    // Zurück zum Hauptmenü
    tft.fillRect(0, 0, 49, 240, white);

    // Linke Spalte
    tft.fillRect(51, 0, 133, 59, white);
    tft.fillRect(51, 60, 133, 59, white);
    tft.fillRect(51, 120, 133, 59, white);
    tft.fillRect(51, 180, 133, 59, white);

    tft.setTextColor(black, white);
    tft.setFont(&FreeSans9pt7b);
    tft.setCursor(61, 35);
    tft.print("Zeitsignal");
    tft.setCursor(61, 95);
    tft.print("LSM303");
    tft.setCursor(61, 155);
    tft.print("frei");
    tft.setCursor(61, 215);
    tft.print("frei");

   // Rechte Spalte
    tft.fillRect(186, 0, 133, 59, white);
    tft.fillRect(186, 60, 133, 59, white);
    tft.fillRect(186, 120, 133, 59, white);
    tft.fillRect(186, 180, 133, 59, white);

    tft.setTextColor(black, white);
    tft.setFont(&FreeSans9pt7b);
    tft.setCursor(196, 35);
    tft.print("TSL2591");
    tft.setCursor(196, 95);
    tft.print("frei");
    tft.setCursor(196, 155);
    tft.print("frei");
    tft.setCursor(196, 215);
    tft.print("frei");
    }

  // Unteruntermenü "DCF-Empfang"
  if (menuPage == 41) {
    // Zurück zum Untermenü Informationen
    tft.fillRect(0, 0, 49, 240, white);

    tft.setFont();
    tft.setTextSize(1);

    // Datenausgabe
    tft.setFont();
    tft.setTextSize(1);
    
    if (DCFtimesignalFound == false){
      tft.setCursor(60, 10);
      tft.setTextColor(white, black);
      tft.print("Kein Zeitzeichen erfolgreich empfangen");
      tft.setCursor(60, 20);
      tft.print("seit ");
      tft.setTextColor(red, black);
      tft.print(noDCFsignal);
      tft.setTextColor(white, black);
      tft.print(" Sekunden.");
    }
    else {
      tft.setCursor(60, 10);
      tft.setTextColor(white, black);
      tft.println("Letztes Zeitsignal erfolgreich empfangen");
      tft.setCursor(60, 20);
      tft.print("vor ");
      tft.setTextColor(red, black);
      tft.print(lastDCFsignal);
      tft.setTextColor(white, black);
      tft.print(" Sekunden.");
    }

    tft.setCursor(60, 40);
    tft.setTextColor(white, black);
    tft.print("Anzahl erfolgreich empfangener Zeitzeichen");
    tft.setCursor(60, 50);
    tft.print("seit dem letzten Systemstart: ");
    tft.setTextColor(red, black);
    tft.print(receivedDCFsignals);
    tft.setTextColor(white, black);
    tft.print(".");

    tft.setCursor(60, 70);
    tft.setTextColor(white, black);
    tft.print("Anteil erfolgreich empfangener Zeitzeichen");
    tft.setCursor(60, 80);
    tft.print("seit dem letzten Systemstart: ");
    tft.setTextColor(red, black);
    tft.print(DCFsuccessRate);
    tft.setTextColor(white, black);
    tft.print(" %");

    tft.setCursor(60, 100);
    tft.setTextColor(white, black);
    tft.print("(System ist seit ");
    tft.setTextColor(red, black);
    tft.print(millis()/60000);
    tft.setTextColor(white, black);
    tft.print(" Minuten in Betrieb.)");

    forceRefresh = true;
  }

  // Unteruntermenü "LSM303"
  if (menuPage == 42) {
    // Zurück zum Untermenü Informationen
    tft.fillRect(0, 0, 49, 240, white);
    
    // Datenausgabe
    tft.setFont();
    tft.setTextSize(1);

    tft.setCursor(60, 10);
    tft.setTextColor(white, black);
    tft.print("Beschleunigungswerte: ");
    tft.setCursor(60, 20);
    tft.print("X: ");
    tft.setTextColor(red, black);
    tft.print(accelX);
    tft.setTextColor(white, black);
    tft.print(" Y: ");
    tft.setTextColor(red, black);
    tft.print(accelY);
    tft.setTextColor(white, black);
    tft.print(" Z: ");
    tft.setTextColor(red, black);
    tft.print(accelZ);

    tft.setCursor(60, 40);
    tft.setTextColor(white, black);
    tft.print("Orientierung: ");
    tft.setCursor(60, 50);
    tft.print("Roll: ");
    tft.setTextColor(red, black);
    tft.print(roll);
    tft.setTextColor(white, black);
    tft.print("Pitch: ");
    tft.setTextColor(red, black);
    tft.print(pitch);
    tft.setTextColor(white, black);            
    tft.print("Heading: ");
    tft.setTextColor(red, black);
    tft.print(heading);    
   

    forceRefresh = true;
  }
    
  // Unteruntermenü "TSL2591"
  if (menuPage == 45) {
    // Zurück zum Untermenü Informationen
    tft.fillRect(0, 0, 49, 240, white);
    
    // Datenausgabe
    tft.setFont();
    tft.setTextSize(1);
    
    tft.setCursor(60, 10);
    tft.setTextColor(white, black);
    tft.print("Infrarot: ");
    tft.setTextColor(red, black);
    tft.println(ir);
    tft.setCursor(60, 30);
    tft.setTextColor(white, black);
    tft.print("Full: ");
    tft.setTextColor(red, black);
    tft.println(full);
    tft.setCursor(60, 50);
    tft.setTextColor(white, black);
    tft.print("Visible: ");
    tft.setTextColor(red, black);
    tft.println(full - ir);
    tft.setCursor(60, 70);
    tft.setTextColor(white, black);
    tft.print("Lux: ");
    tft.setTextColor(red, black);
    tft.println(tsl.calculateLux(full, ir));

    forceRefresh = true;
  }

    // Unteruntermenü "DHT22"
  if (menuPage == 46) {
    // Obere Reihe des Menüs
    tft.fillRect(0, 0, 106, 30, white);
    tft.setTextSize(1);
    tft.setTextColor(black, white);
    tft.setCursor(35, 12);
    tft.print("Hauptmenü");
    tft.setCursor(145, 12);
    tft.print("Informationen DHT22");

    // Bereich der Datenausgabe
    tft.print("Der Sensor ist noch nicht in das System eingebunden");
  }

    // Unteruntermenü "Systeminformationen"
  if (menuPage == 48) {
    // Obere Reihe des Menüs
    tft.fillRect(0, 0, 106, 30, white);
    tft.fillRect(107, 0, 106, 30, white);

    tft.setTextSize(1);
    tft.setTextColor(black, white);
    tft.setCursor(35, 12);
    tft.print("Hauptmenü");
    tft.setCursor(145, 12);
    tft.print("Informationen");
    tft.setTextColor(white, black);
    tft.setCursor(255, 7);
    tft.print("System-");
    tft.setCursor(245, 18);
    tft.print("informationen");

    // Bereich der Datenausgabe
    tft.print("Das System ist seit ");
    tft.print(millis() / 60000);
    tft.print(" Minuten in Betrieb");
  }

    // Unteruntermenü "Wecker aus/Schlummern"
  if (menuPage == 7) {
    // Obere Reihe des Menüs
    tft.fillRect(0, 0, 239, 240, red);
    tft.fillRect(0, 240, 80, 240, orange);

    tft.setTextSize(3);
    tft.setTextColor(black, red);
    tft.setCursor(150, 120);
    tft.print("Wecker");
    tft.setCursor(180, 130);
    tft.print("aus");
    tft.setTextColor(black, orange);
    tft.setCursor(245, 120);
    tft.print("Schlummern");
  }

  // Damit der TFT-Screen nicht flackert, wird diese Schleife nur ausgeführt, wenn ein neues Menü ausgewählt wurde.
  previousmenuPage = menuPage;

  //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();
    // Damit der Wert 0, 0 des Touchsensors mit dem Pixel 0, 0 des TFT-Screens übereinstimmt, müssen p.x und p.y rekodiert werden
    tx = map(p.y, 0, 320, 320, 0);
    ty = p.x;

    //TFTbackLightStatus = true;
    TFTbackLightTime = millis();
    touchtouched2 = true;
    
    // Damit der Touchsenso erst beim Loslassen des Screens eine Aktion auslöst, darf so lange er gedrückt wird nichts passieren.
    // Daher wird touchtime so lange auf die aktuelle Zeit gesetzt, wie der Touchscreen gedrückt wird.
    // Ausgelöst wird die Aktion erst dann, wenn der Touchscreen nicht mehr gedrückt ist und die bebouncetime verstrichen ist.
    touchtime = millis();                      // aktualisiere Touchzeit
    touchtouched = true;
  }

  
  // Es folgen für jedes einzelne Menü die Definitionen der jeweils sensiblen Bereiche einschließlich der Befehle, die jeweils ausgelöst werden sollen
  if (menuPage == 0 && touchtouched == true && TFTbackLightStatus == true && (millis() - touchtime > debouncetime)) {  // Wenn Menüseite 0 ausgewählt und der Touchscreen berührt wird und die debouncetime verstrichen ist  ...
    if ((tx >= 0) && (tx <= 120) && (ty >= 0) && (ty <= 59)) {                           // ... und der TouchScreen in dem angegebenen Bereich berührt wird ... 
      menuPage = 1;                                                                      // ... dann rufe Menüseite 1 auf.
    } 
    else if ((tx >= 0) && (tx <= 120) && (ty >= 60) && (ty <= 120)) {
      menuPage = 2;
    }
    else if ((tx >= 0) && (tx <= 120) && (ty >= 121) && (ty <= 180)) {
      menuPage = 3;
    }
    else if ((tx >= 0) && (tx <= 120) && (ty >= 181) && (ty <= 240)) {
      menuPage = 4;
    } 
    else if ((tx >= 121) && (tx <= 320) && (ty >= 1) && (ty <= 240)) {
      menuPage = 5;
    }
    touchtouched = false;
    tft.fillScreen(ILI9341_BLACK);
  }

  // Menü Snoozle
  if (menuPage == 1 && touchtouched == true && TFTbackLightStatus == true && (millis() - touchtime > debouncetime)) {
    // Zurück zum Hauptmenü
    if ((tx >= 0) && (tx <= 40) && (ty >= 0) && (ty <= 240)) {
      menuPage = 0;
    } 
    else if ((tx >= 41) && (tx <= 320) && (ty >= 0) && (ty <= 240)) {
      menuPage = 0;
    }
    touchtouched = false;
    tft.fillScreen(ILI9341_BLACK);
  }

  // Menü Ziffernblatt
  if (menuPage == 2 && touchtouched == true && (millis() - touchtime > debouncetime)) {
    // Zurück zum Hauptmenü
    if ((tx >= 0) && (tx <= 40) && (ty >= 0) && (ty <= 240)) {
      menuPage = 0;
    } 
    else if ((tx >= 41) && (tx <= 320) && (ty >= 0) && (ty <= 240)) {
      menuPage = 0;
    }
    touchtouched = false;
    tft.fillScreen(ILI9341_BLACK);
  }

  // Menü Einstellungen
  if (menuPage == 3 && touchtouched == true && TFTbackLightStatus == true && (millis() - touchtime > debouncetime)) {
    // Zurück zum Hauptmenü
    if ((tx >= 0) && (tx <= 40) && (ty >= 0) && (ty <= 240)) {
      menuPage = 0;
    } 
    else if ((tx >= 41) && (tx <= 320) && (ty >= 0) && (ty <= 240)) {
      menuPage = 0;
    }
    touchtouched = false;
    tft.fillScreen(ILI9341_BLACK);
  }

  // Menü Informationen
  if (menuPage == 4 && touchtouched == true && TFTbackLightStatus == true && (millis() - touchtime > debouncetime)) {
    // Zurück zum Hauptmenü
    if ((tx >= 0) && (tx <= 40) && (ty >= 0) && (ty <= 240)) {
      menuPage = 0;
    } 
    // Zum Uhntermenü DCF Zeitsignal
    else if ((tx >= 51) && (tx <= 184) && (ty >= 0) && (ty <= 59)) {                           // ... und der TouchScreen in dem angegebenen Bereich berührt wird ... 
      menuPage = 41;                                                                      // ... dann rufe Menüseite 1 auf.
    } 
    // Zum Untermenü LSM303
    else if ((tx >= 51) && (tx <= 184) && (ty >= 60) && (ty <= 120)) {
      menuPage = 42;
    }
    /* // Zum Menü "frei"
    else if ((tx >= 51) && (tx <= 184) && (ty >= 121) && (ty <= 180)) {
      menuPage = 43;
    }
    // Zum Menü "frei"
    else if ((tx >= 51) && (tx <= 184) && (ty >= 181) && (ty <= 240)) {
      menuPage = 44;
    } 
    */// Zum Menü TSL2591-Sensor
    else if ((tx >= 186) && (tx <= 319) && (ty >= 0) && (ty <= 59)) {                           // ... und der TouchScreen in dem angegebenen Bereich berührt wird ... 
      menuPage = 45;                                                                      // ... dann rufe Menüseite 1 auf.
    } 
    /*// Zum Menü "frei"
    else if ((tx >= 186) && (tx <= 319) && (ty >= 60) && (ty <= 120)) {
      menuPage = 46;
    }
    // Zum Menü "frei"
    else if ((tx >= 186) && (tx <= 319) && (ty >= 121) && (ty <= 180)) {
      menuPage = 47;
    }
    // Zum Menü "frei"
    else if ((tx >= 186) && (tx <= 319) && (ty >= 181) && (ty <= 240)) {
      menuPage = 48;
    } 
    */
    touchtouched = false;
    tft.fillScreen(ILI9341_BLACK);
  }

  // Untermenü DCF77 Zeitsignal
  if (menuPage == 41 && touchtouched == true && TFTbackLightStatus == true && (millis() - touchtime > debouncetime)) {
    // Zurück zum Untermenü Informationen
    if ((tx >= 0) && (tx <= 40) && (ty >= 0) && (ty <= 240)) {
      menuPage = 4;
    } 
    else if ((tx >= 41) && (tx <= 320) && (ty >= 0) && (ty <= 240)) {
      menuPage = 4;
    }
    touchtouched = false;
    tft.fillScreen(ILI9341_BLACK);
  }
  
  // Untermenü TSL2591 Sensor
  if (menuPage == 45 && touchtouched == true && TFTbackLightStatus == true && (millis() - touchtime > debouncetime)) {
    // Zurück zum Untermenü Informationen
    if ((tx >= 0) && (tx <= 40) && (ty >= 0) && (ty <= 240)) {
      menuPage = 4;
    } 
    else if ((tx >= 41) && (tx <= 320) && (ty >= 0) && (ty <= 240)) {
      menuPage = 4;
    }
    touchtouched = false;
    tft.fillScreen(ILI9341_BLACK);
  }
   
  // Untermenü TSL303 Sensor
  if (menuPage == 42 && touchtouched == true && TFTbackLightStatus == true && (millis() - touchtime > debouncetime)) {
    // Zurück zum Untermenü Informationen
    if ((tx >= 0) && (tx <= 40) && (ty >= 0) && (ty <= 240)) {
      menuPage = 4;
    } 
    else if ((tx >= 41) && (tx <= 320) && (ty >= 0) && (ty <= 240)) {
      menuPage = 4;
    }
    touchtouched = false;
    tft.fillScreen(ILI9341_BLACK);
  }
   
  // Menü Wecker stellen
  if (menuPage == 5 && touchtouched == true && TFTbackLightStatus == true && (millis() - touchtime > debouncetime)) {
    // Zurück zum Hauptmenü
    if ((tx >= 0) && (tx <= 40) && (ty >= 0) && (ty <= 240)) {
      menuPage = 0;
    } 
    else if ((tx >= 41) && (tx <= 320) && (ty >= 0) && (ty <= 240)) {
      menuPage = 0;
    }
    touchtouched = false;
    tft.fillScreen(ILI9341_BLACK);
  }
    
    // Das TFT Backlight soll beim Berühren des Touchscreens eingeschaltet werden. Nach einer eingestellten Zeit soll die Beleuchtung wieder ausgeschaltet werden.
    // Damit Eigentlich soll der Touchsensor bei der ersten Berührung nur eingeschaltet werden, und erst wenn das Licht an ist, d.h. bei der zweiten Berührung Menüpunkte auswählen.
    // Das Funktioniert aber noch nicht. 
    if (touchtouched2 == true && (millis() - touchtime > debouncetime)) { 
      if (millis() <= TFTbackLightTime + TFTbackLightdelay) {
        TFTbackLightStatus = true;
        TFTbrightness = 255;
      }
      analogWrite(TFTbackLightPin, TFTbrightness);
      touchtouched2 = false;
    }
    if ((millis() > TFTbackLightTime + TFTbackLightdelay) && TFTbackLightStatus == true) {
      TFTbrightness = TFTbrightness - 1;
      if (TFTbrightness == 0) {
        TFTbackLightStatus = false;
        tft.fillScreen(ILI9341_BLACK);
      }
      analogWrite(TFTbackLightPin, TFTbrightness);
    }

  //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(" MenuPage: ");
    Serial.print(menuPage);
    Serial.print("; TFT Helligkeit: ");
    Serial.print(TFTbrightness);

    Serial.print("; empf. DCF-Sig.: ");
    Serial.print(receivedDCFsignals);

    /* Display the results (acceleration is measured in m/s^2) */
    Serial.print("X: "); Serial.print(accelX); Serial.print("  ");
    Serial.print("Y: "); Serial.print(accelY); Serial.print("  ");
    Serial.print("Z: "); Serial.print(accelZ); Serial.print("  ");Serial.print("m/s^2 ");

    /* 'orientation' should have valid .roll and .pitch fields */
    Serial.print(F("; Orientation: "));
    Serial.print(roll);
    Serial.print(F(" "));
    Serial.print(pitch);
    Serial.print(F(" "));
    Serial.print(heading);
    Serial.print(F(""));

   
    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  "T"   // Header tag for serial time sync message

unsigned long processSyncMessage() {
  time_t pctime = 0L;
  //unsigned long pctime = 0L;

  if(Serial.find(TIME_HEADER)) {
     pctime = Serial.parseInt();
     return pctime;
     if( pctime < DefaultTime) { // check the value is a valid time (greater than Nov 1 2016)
       pctime = 0L; // return 0 to indicate that the time is not valid
     }
  }
  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('0');
  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('0');
  Serial.print(digits);
}


void tftprintDCFsyncTime() {
  if (DCFtimesignalFound == false){
    tft.println("Kein Zeitsignal empfangen seit ");
    tft.print(noDCFsignal);
    tft.print(" Sek.");
  }
  else {
    tft.println("Zeitsignal empfangen vor ");
    tft.print(lastDCFsignal);
    tft.print(" Sek.");
  }
}


void printDCFsyncTime() {
  if (DCFtimesignalFound == false){
    Serial.print(" no DCF77 sync since ");
    Serial.print(noDCFsignal);
    Serial.print(" sec.");
  }
  else {
    Serial.print(" last DCF77 sync ");
    Serial.print(lastDCFsignal);
    Serial.print(" sec. ago");
  }
}


void tftprintDCFsyncCycle() {
  if (DCFtimesignalFound == false){
    tft.println("Kein Zeitsignal empfangen seit ");
    tft.print(noDCFsignal / 60);
    tft.print(" Zyklen");
  }
  else {
    tft.println("Zeitsignal empfangen vor ");
    tft.print(lastDCFsignal /60);
    tft.print(" Zyklen");
  }
}


void StartupScreen() {
  // Zeigt einen Startup-Screen
  tft.println("Schlafphasenwecker");
  tft.println("Version 0.2 alpha");
}

Der Sketch verwendet 62.504 Bytes (5%) des Programmspeicherplatzes. Das Maximum sind 1.048.576 Bytes. Globale Variablen verwenden 6.496 Bytes (2%) des dynamischen Speichers, 255.648 Bytes für lokale Variablen verbleiben. Das Maximum sind 262.144 Bytes.

Tags: #Arduino #Schlafphasenwecker #Teensy

arduino/schlafphasenwecker/programmversion_0.3.1485720803.txt.gz · Zuletzt geändert: 18.05.2023 12:16 (Externe Bearbeitung)