Hier die Fortsetzung:
Code für Sensor DHT11 und Menü zusammen. Der Arduino reagiert nur noch auf lang gedrückte Tasten und oder mehrfache Eingaben?! Ich habe auch schon Teile des Codes deaktiviert und getestet. Leider konnte ich nichts finden. Was bremst den Guten nun aus?
//Sample using LiquidCrystal library
#include <LiquidCrystal.h>
#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
/*******************************************************
*
* This program will test the LCD panel and the buttons
* Mark Bramwell, July 2010
*
********************************************************/
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some tMs used by the panel and buttons
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
//Sensor data
int tMax = 30; //Temperatur Maximum
int tMin = 19; //Temperature Minimum
int Select = 0; //State to select Temps
int SupplyDHT = 13; // Power supply for DHT
DHT dht(DHTPIN, DHTTYPE);
//Variabels
int ledMax = 12; //LED to show that Temp is higher than selected Temp Max
int ledMin = 11; //same as above for Temp Min
int Range = 1; //Value to change Temp borders
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the tM from the sensor
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
// For V1.0 comment the other threshold and use the one below:
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...
}
void setup()
{
lcd.begin(16, 2); // start the library
pinMode(ledMax, OUTPUT);
pinMode(ledMin, OUTPUT);
pinMode(SupplyDHT, OUTPUT);
Serial.begin(9600);
digitalWrite(SupplyDHT, HIGH); // turn the DHT on (HIGH is the voltage level - pin13 for power supply)
}
void loop()
{
// read sensors
float h = dht.readHumidity();
float t = dht.readTemperature();
//Display
//Menu
if (Select == 0){ //go back to Main manu and show all Values od Temp and Humidity
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.setCursor(12,0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humid:");
lcd.setCursor(12,1);
lcd.print("%Rel");
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
lcd.setCursor(5,0); //print Temp on LCD
lcd.print(t);
lcd.setCursor(6,1); // print Humidity in LCD
lcd.print(h);
}
if (Select == 1) { //select Max Temp border
lcd.setCursor(0,0);
lcd.print("Temp Max ");
lcd.setCursor(9,0);
lcd.print(tMax);
lcd.setCursor(0,1);
lcd.print("use up or down to select Max");
}
if (Select == 2){ //select Min Temp border
lcd.setCursor(0,0);
lcd.print("Temp Min ");
lcd.setCursor(9,0);
lcd.print(tMin);
}
// check if returns are valid, if they are NaN (not a number) then something went wrong!
//for debug reason
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
}
else {
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
}
lcd.setCursor(16,1); // move to the end of the second line (hide button output)
//BUTTONS
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
lcd.print("RIGHT ");
break;
}
case btnLEFT:
{
lcd.print("LEFT ");
break;
}
case btnUP:
{
lcd.print("UP ");
if (Select == 1){ //Increase tMax or tMin when pushing the button and Select is on (Value 1 or 2)
tMax ++;
delay(300);
}
if (Select == 2){
tMin ++;
delay(300);
}
break;
}
case btnDOWN:
{
lcd.print("DOWN ");
if (Select == 1){ //decrease tMax or tMin when pushing the button and Select is on (Value 1 or 2)
tMax --;
delay(300);
}
if (Select == 2){
tMin --;
delay(300);
}
break;
}
case btnSELECT: //open Select menu; chose the option, like Max Temp...
{
Select ++;
lcd.print("SE");
delay(50);
if (Select == 3){ //go back to main manu
Select = 0;
}
break;
}
case btnNONE:
{
break;
}
}
//next code switches on LEDs to show when the Temp is lower or higher as the preselection of Temp Min and Max
if (Select == 0){ //do this just if your not in the selection menu
if (t >= tMax){ //If Temp is too high or minimal switch on LED13
digitalWrite(ledMax, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50);
}
else {
digitalWrite(ledMax, LOW); // turn the LED off (LOW is the voltage level)
delay(50);
}
if (t <= tMin){ //If Temp is too high or minimal switch on LED13
digitalWrite(ledMin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50);
}
else {
digitalWrite(ledMin, LOW); // turn the LED off (LOW is the voltage level)
delay(50);
}
}
}
Ich würde mich riesig über einen Tipp freuen warum der Arduino Uno so langsam reagiert und wäre sehr dankbar. Zum besseren Verständnis mache ich gerne ein Abbild mit fritzing, falls gewünscht.
Beste Grüße und Dank im Vorraus
Herr Schmidt