uxomm:
Hier der Code aus dem Ausgangspost (ich habe etliche Leerzeilen entfernt und "Auto Format" angewendet):
#include <SPI.h>
#include <SD.h>
#include "DHT.h"
#include <Wire.h>
#include <DS3231.h>
#include "SparkFunMPL3115A2.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
MPL3115A2 myPressure;
DS3231 clock;
RTCDateTime dt;
const int chipSelect = 4;
const int trigPin = 24;
const int echoPin = 22;
long duration;
int distance;
int button = 40; // Variable für Taster
int buttonStatus = 1; // Hier wird die Information gespeichert, ob Taster gedrückt oder nicht.
int buttonStatusAlt = 1; // Hier ist die vorherige Information des Tasters gespeichert
int zaehler = 0; // Zählt wie oft auf den Taster gedrückt wurde
#define DHT22PIN 32
#define DHT22TYPE DHT22
DHT dht22(DHT22PIN, DHT22TYPE);
void setup() {
pinMode(button, INPUT); // Taster als Eingang definieren
digitalWrite(button, HIGH); // Pull- Up- Widerstand aktivieren
Wire.begin();
Serial.println("Initialize DS3231");
clock.begin();
clock.setDateTime(DATE, TIME);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
myPressure.begin();
dht22.begin();
myPressure.setModeBarometer();
myPressure.setOversampleRate(7);
myPressure.enableEventFlags();
while (!Serial) {
;
}
Serial.print("Initialisiere SD-Karte");
if (!SD.begin(chipSelect)) {
Serial.println("Karte nicht gefunden");
return;
}
Serial.println("Karte erfolgreich initialisiert");
}
void loop() {
dt = clock.getDateTime();
float humidyDHT22 = dht22.readHumidity(); //relative Luftfeuchtigkeit vom Sensor DHT22 lesen
float tempDHT22 = dht22.readTemperature(); //Temperatur vom Sensor DHT22 lesen
if (isnan(humidyDHT22) || isnan(tempDHT22)) {
Serial.println("DHT22 konnte nicht ausgelesen werden");
} else {
printValues("DHT22", humidyDHT22, tempDHT22);
}
delay(1500); //2,5 sek. Warten, der DHT11 Sensor stellt alle 2 sek. neue Werte bereit.
}
void printValues(String sensor, float humidy, float temp) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
float helligkeit = analogRead(A0);
float standorthoehe = 278 ;
float pressure = myPressure.readPressure();
float press_hPa = pressure / 100 ;
float differenz = standorthoehe / 8 ;
float pressure_1 = differenz + press_hPa ;
float pressure_hPa = pressure_1 ;
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.print(clock.dateFormat("d-m-Y", dt));
dataFile.print(" ");
dataFile.print(clock.dateFormat("Hi", dt));
dataFile.print(" ");
dataFile.print(helligkeit); //Speichern der Messwerte in der geöffneten Datei
dataFile.print(" "); //Trennzeichen für die .txt (DATALOG.txt) Date
dataFile.print(distance);
dataFile.print(" ");
dataFile.print(humidy);
dataFile.print(" ");
dataFile.print(temp);
dataFile.print(" ");
dataFile.print(pressure_hPa, 1);
dataFile.println(" ");
dataFile.close();
Serial.print(clock.dateFormat("d-m-Y", dt));
Serial.print(";");
Serial.print(clock.dateFormat("H:i:s", dt));
Serial.print(";");
Serial.print(helligkeit);
Serial.print(";");
Serial.print(distance);
Serial.print(";");
Serial.print(humidy);
Serial.print(";");
Serial.print(temp);
Serial.print(";");
Serial.print(pressure_hPa, 1);
Serial.println(";");
buttonStatus = digitalRead(button); // Prüfe, ob Taster gedrückt oder nicht
if (buttonStatusAlt != buttonStatus) { // Wenn der alte Status nicht gleich dem neuen Status ist
if (buttonStatus == LOW) { // Und wenn der Taster gedrückt wurde
if (zaehler == 3) { // Und der Zähler gleich 7 ist
zaehler = 0; // Setze den Zähler zurück auf 0
}
else {
zaehler++; // Wenn der Zähler nicht gleich 7 ist, erhöhe den Zähler um 1
// Bsp:. Wenn Zähler = 3 -> mach Zähler = 4
}
}
}
buttonStatusAlt = buttonStatus; // Setze den alten Status = den neuen Status
if (zaehler == 0) { // Wenn Zähler gleich null
lcd.begin(15, 2); // set up the LCD's number of columns and rows
lcd.setCursor(3, 0);
lcd.print(clock.dateFormat("d-m-Y", dt)); // Print a message to the LCD.
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(5, 1);
// print the number of seconds since reset:
lcd.print(clock.dateFormat("H:i", dt));
}
if (zaehler == 1) { // Wenn Zähler gleich 1
// set up the LCD's number of columns and rows:
lcd.begin(15, 2);
// Print a message to the LCD.
lcd.setCursor(0, 0);
lcd.print("Luftdr.");
lcd.print(pressure_hPa);
lcd.print("hPa");
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print("Temperatur");
lcd.print(temp);
}
if (zaehler == 2) {
lcd.begin(15, 2);
lcd.setCursor(0, 0);
lcd.print("Luftfeucht.");
lcd.print(humidy);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("HELLIGKEIT");
lcd.print(helligkeit);
}
if (zaehler == 3) {
lcd.begin(15, 2);
lcd.setCursor(0, 0);
lcd.print("FERTIG");
}
// Prüfe alles von neu
} else {
Serial.println("Fehler beim Öffnen!");
}
delay(1000);
}
Es gibt ein paar "Eigenartigkeiten":
Warum wird im Laufe des Codes das LCD 4mal und praktisch vor jeder Ausgabe mit lcd.begin initialisiert?
lcd.begin gehört doch in setup.
Es gibt insgesamt 2 längere Delays einmal in loop (sic!):
delay(1500); //2,5 sek. Warten, der DHT11 Sensor stellt alle 2 sek. neue Werte bereit.
und einmal in printValues (1 Sekunde).
Alleine das sorgt wohl schon dafür, dass auf Tasterdruck nur noch sehr träge reagiert wird.
Eine "Zeitüberwachung" mit millis() wäre nötig.
hallo, danke fuer die schnelle antworten, ich habe jetzt das lcd.begin() in das setup gesetzt und alle delay() entfernt, jedoch hat sich an der situation nichts geändert.