/* Copyright (C) 2011 Mario Keller. All rights reserved. This programm is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. See file LICENSE.txt for further informations on licensing terms. */ //einbinden der notwendigen Libraries #include #include //Sensorobjekt erzeugen dht11 DHT11; //DHT11 Sensor steckt auf dem Analog-PIN 5 auf dem LCD-Board #define DHT11PIN A5 // festlegen der Pins für das LCD panel LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // werte und variablen die benötigt werden int lcd_key = 0; int adc_key_in = 0; #define btnRIGHT 0 #define btnUP 1 #define btnDOWN 2 #define btnLEFT 3 #define btnSELECT 4 #define btnNONE 5 #define LCDBacklightPin 10 //Werte für die Unterscheidung der Anzeige #define showNORM 1 #define showMAX 2 #define showMIN 3 int show = 1; //start-helligkeit des displays int dim = 150; //initialisieren der variablen für messwerte und die zeitmessung int temp,maxtemp,mintemp = 0; int hum,maxhum,minhum = 0; unsigned long time = 0; // read the buttons int read_LCD_buttons() { adc_key_in = analogRead(0); // read the value from the sensor // my buttons when read are centered at these valies: 0, 144, 329, 504, 741 // we add approx 50 to those values and check to see if we are close if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result if (adc_key_in < 50) return btnRIGHT; if (adc_key_in < 195) return btnUP; if (adc_key_in < 380) return btnDOWN; if (adc_key_in < 555) return btnLEFT; if (adc_key_in < 790) return btnSELECT; return btnNONE; // when all others fail, return this... } // einfache funktion zum anpassen der helligkeit. // @param int change - relative Änderung des aktuellen Wertes (wird nur verwendet wenn absolute == 0) // @param int absolute - setzt diesen wert direkt als helligkeitswert // void set_backlight(int change, int absolute = 0) { if ( absolute == 0 ) { dim = dim + change; } else { dim = absolute; } //prüfen auf erlaubten wertebereich 0 - 255 if ( dim < 0 ) dim =0; if (dim > 255 ) dim = 255; Serial.print("setting dim = "); Serial.println(dim); //schreiben der Helligkeit analogWrite(LCDBacklightPin,dim); } // abfragen des sensors und speichern der messwerte bei erfolg void read_dht11() { //first check the sensor int chk = DHT11.read(DHT11PIN); Serial.print("Read sensor: "); switch (chk) { //nur wenn lesen OK, dann werte speichern case 0: { Serial.println("OK"); temp = DHT11.temperature; hum = DHT11.humidity; break; } case -1: Serial.println("Checksum error"); break; case -2: Serial.println("Time out error"); break; default: Serial.println("Unknown error"); break; } } // funktion zum einzeigen der messwerte, abhängig vom übergebenen typ void show_values(int type) { int t = 0; int h = 0; String tail = " "; switch ( type ) { case showNORM: // aktuelle werte anzeigen t = temp; h = hum; break; case showMAX: // max werte anzeigen t = maxtemp; h = maxhum; tail = " (max)"; break; case showMIN: // min werte anzeigen t = mintemp; h = minhum; tail = " (min)"; break; } // ausgabe der werte auf dem display lcd.setCursor(0,0); lcd.print("Temp "); lcd.print(t); lcd.print(" "); lcd.write(223); lcd.print("C"); lcd.print(tail); lcd.setCursor(0,1); lcd.print("Hum "); lcd.print(h); lcd.print(" % "); lcd.print(tail); } ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// // MAIN ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// void setup() { //create some debug output on the serial monitor Serial.begin(9600); Serial.println("starting up"); //we have a 2x16 lcd display lcd.begin(16, 2); // start the library lcd.setCursor(0,0); //initiale werte ermitteln read_dht11(); //startwerte setzen maxtemp=temp; mintemp=temp; maxhum = hum; minhum = hum; //startzeit bestimmen time = millis(); } void loop() { //sensor auslesen nur im sekundentakt if ( millis() - time > 1000 ) { time = millis(); Serial.print("reading time :"); Serial.println( (millis() / 1000)); read_dht11(); } //min und max werte speichern if ( temp > maxtemp ) maxtemp = temp; if ( temp < mintemp ) mintemp = temp; if ( hum > maxhum ) maxhum = hum; if ( hum < minhum ) minhum = hum; //per default immer altuelle Werte anzeigen show = showNORM; //prüfen der Tasten lcd_key = read_LCD_buttons(); // read the buttons switch (lcd_key) { // depending on which button was pushed, we perform an action case btnRIGHT: { //max werte anzeigen show = showMAX; break; } case btnLEFT: { //min werte anzeigen show = showMIN; break; } case btnUP: { set_backlight(30); break; } case btnDOWN: { set_backlight( -30); break; } case btnSELECT: { break; } case btnNONE: { break; } } //anzeige show_values(show); delay(100); }