Hallo zusammen,
ich habe ein LCD Display (16x2) mit Keypad (6 Tasten) auf einen Arduino UNO gepfropft.
Die Display Helligkeit wird über einen LM393 Fotosensor gesteuert.
Hier der Code:
#include <LiquidCrystal.h>
#include <LCDKeypad.h>
LCDKeypad lcd;
uint8_t BL = 120; //BackLight LED_LCD initial positon 50% on 120/240 of 255
uint8_t DeltaDim = 3;
int LightAnalogValue; // Holds the last analog value
int oldLightAnalogValue;
// counter for the temperature check, starting at TEMP_CHECK_INTERVAL to read the temperature and humidity on first loop
#define TEMP_CHECK_INTERVAL 1000
unsigned long temp_check_count = TEMP_CHECK_INTERVAL;
// pins of the light sensor
#define LIGHT_PWR_PIN 11
#define LIGHT_SENSOR_PIN A1
byte BACKLIGHT_MODE = 1; // 0 = manaual, 1 = auto
byte KEY_MODE = 0; //0 = no key pressed, 1 = select key pressed
// type of the DHT sensor
#define DHT_TYPE 22
// pins of the DHT sensor
#define DHT_PWR_PIN 12
#define DHT_SENSOR_PIN 13
// 0 = Celsius; 1 = Fahrenheit
#define TEMP_UNIT 0
//include the library to work with the DHT
#include "DHT.h"
DHT dht(DHT_SENSOR_PIN, DHT_TYPE);
void setup() {
pinMode(LIGHT_PWR_PIN, OUTPUT);
pinMode(DHT_PWR_PIN, OUTPUT);
digitalWrite(LIGHT_PWR_PIN, HIGH);
digitalWrite(DHT_PWR_PIN, HIGH);
pinMode(DHT_SENSOR_PIN, INPUT);
dht.begin();
lcd.begin(16, 2);
lcd.print("DHT Sensor Data");
}
void loop() {
readBtn();
readLightSensor();
analogWrite(10, BL); //BackLight of the LCD
// temperature and humidity check
temp_check_count++;
if (temp_check_count >= TEMP_CHECK_INTERVAL) {
temp_check();
temp_check_count = 0;
}
}
void temp_check() {
lcd.setCursor(0, 1);
// temperature and humidity
float t = 0;
float h = 0;
if (getSensorData(t, h)) {
//scale sensor data
t = t - (t * 0.013);
h = h - (h * 0.028);
}
if (TEMP_UNIT == 0) {
t = (t >= 100) ? 99.9 : t;
h = (h >= 100) ? 99.9 : h;
}
// print the results to the lcd
lcd.print("T:");
lcd.print(t, 1);
lcd.print((TEMP_UNIT == 0) ? "\337C H:" : "F H:");
lcd.print(h, 1);
lcd.print("%");
}
void readBtn() {
if (BL == 0) {
lcd.noDisplay();
}
else {
lcd.display();
}
int button = lcd.button();
/*
if (button == KEYPAD_NONE) {
return;
}
*/
switch (button) {
// SELECT KEY //
case KEYPAD_SELECT:
break;
// UP KEY //
case KEYPAD_UP:
BACKLIGHT_MODE = 0;
//set BL Min to Max
BL += (BL >= 240) ? 0 : DeltaDim;
break;
// DOWN KEY //
case KEYPAD_DOWN:
BACKLIGHT_MODE = 0;
//set BL Max to Min
BL -= (BL == 0) ? 0 : DeltaDim;
break;
}
BL = BL <= 0 ? 0 : BL;
delay(30);
}
void readLightSensor() {
LightAnalogValue = roundTo(analogRead(LIGHT_SENSOR_PIN), 10);
if ((LightAnalogValue > 0) && (abs(LightAnalogValue - oldLightAnalogValue) > 20)) { //prevent display flicker
if (BACKLIGHT_MODE == 1) { // dim display auto with light sensor data
BL = 250 - (uint8_t)(ceil((LightAnalogValue / 4)));
BL = BL < 10 ? 10 : BL > 240 ? 240 : BL;
}
/*
lcd.setCursor(0, 0);
lcd.print("L: ");
lcd.print(LightAnalogValue);
lcd.print(" BL: ");
lcd.print(BL);
lcd.print(" ");
*/
oldLightAnalogValue = LightAnalogValue;
}
}
//round to next 10, 100 or 1000
int roundTo(int x, int roundTo) {
x /= roundTo;
return (int)(floor(x + 0.5) * roundTo);
}
bool getSensorData(float &temp, float &hum) {
float t = dht.readTemperature((TEMP_UNIT == 0) ? false : true);
float h = dht.readHumidity();
//if the values could not be read
if (isnan(t) || isnan(h)) {
return false;
}
else {
temp = t;
hum = h;
return true;
}
}
Der Spaß funktioniert so weit ganz gut. T und RH werden angezeigt und das Display flimmert nicht.
Kann man das so machen oder ist in dem Code viel Müll?
Zusätzlich will ich den LM393 einsparen und nur einen Fotowiderstand an einem analogen Eingang anschließen. Wie müsste dann so ein Spannungsteiler aussehen?