Hallo alle zusammen,
ich habe einen Arduino Uno und wollte mit einem DHT 11 (Temperatursensor) und LCD Display mit sechs Tasten eine Art Temperatursteuerung basteln. Das Auslesen des DHT11 funktioniert sehr gut und mein selbst gebasteltes Menü zur Auswahl der Temperaturminimum- und maximumwerte funktioniert einzeln auch. Wenn ich aber beide sketches zusammenlege, reagiert der Arduino nur sehr langsam auf meine Eingaben.
Das ist mein erstes Projekt mit dem Arduino und der Code ist bestimmt nicht der schönste. Statt den ganzen "if" Abfragen habe ich auch mit "switch" und "case" probiert, was aber von der Performance sehr langsam war?!
Hier der Code für den Temperatursensor DHT11 mit Anzeige auf LCD:
//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 values 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
// 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
// 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...
}
DHT dht(DHTPIN, DHTTYPE);
int SupplyDHT = 13; // Volts for DHT, cant use 5V Pin, already used by LCD
void setup()
{
lcd.begin(16, 2); // start the library
//lcd.begin(0, 2);
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!");
// initialize the digital pin as an output.
pinMode(SupplyDHT, OUTPUT);
dht.begin();
}
void loop()
{
digitalWrite(SupplyDHT, HIGH); // turn the DHT on (HIGH is the voltage level)
float h = dht.readHumidity();
float t = dht.readTemperature();
lcd.setCursor(6,0); // move cursor to second line "1" and 9 spaces over
lcd.print(t);
lcd.setCursor(6,1); // move cursor to second line "1" and 9 spaces over
lcd.print(h);
// check if returns are valid, if they are NaN (not a number) then something went wrong!
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(14,0); // move to the begining of the second line
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 ");
break;
}
case btnDOWN:
{
lcd.print("DOWN ");
break;
}
case btnSELECT:
{
lcd.print("SELECT");
break;
}
case btnNONE:
{
lcd.print("NONE ");
break;
}
}
}
Der Code für das Menü mit Temperatur Min- und Max-Auswahl, sowie Anzeige von Temperatur und Luftfeuchte (noch nicht hinzugefügt, erst im nächsten Sketch).
//Sample using LiquidCrystal library
#include <LiquidCrystal.h>
/*******************************************************
*
* 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
int tMax = 0; //Temperatur Maximum
int tMin = 0; //Temperature Minimum
int Select = 0; //State of the menu: 0 = nothin (later shows Temps), 1 select Temp Max, 2 select Temp Min
int Range = 1; //Value to increase or decrease Temps
int SupplyDHT = 13; //Power supply for DHT11, not installed
// 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(SupplyDHT, OUTPUT);
Serial.begin(9600);
}
void loop()
{
//Range to change Values
lcd.setCursor(14,0); // move cursor to second line "1" and 9 spaces over
lcd.print("~");
lcd.setCursor(15,0); // move cursor to second line "1" and 9 spaces over
lcd.print(Range); // shows Value of Range on LCD
//Display
//Menu
if (Select == 1) { //select Max Temp border
lcd.setCursor(0,0);
lcd.print("Temp Max ");
lcd.setCursor(9,0);
lcd.print(tMax);
}
if (Select == 2){ //select Min Temp border
lcd.setCursor(0,0);
lcd.print("Temp Min ");
lcd.setCursor(9,0);
lcd.print(tMin);
}
if (Select == 0){ //go back to Main manu
lcd.setCursor(0,0);
lcd.print("Push Setup ");
}
lcd.setCursor(13,1); // move to the end of the second line
//BUTTONS
lcd_key = read_LCD_buttons(); // read the buttons
Serial.println(tMax);
delay(1); // delay in between reads for stability
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; choose the option, like Max Temp...
{
Select ++;
lcd.print("SE");
delay(300);
if (Select == 3){ //go back to main manu
Select = 0;
}
break;
}
case btnNONE:
{
lcd.print("..");
break;
}
}
}
Fortsetzung im nächsten Beitrag...