Hi everyone,
So i've got a standard DHT temp/humidity monitor displaying to a 16x2 I2C lcd screen working no problems, but i want to add a momentary button to toggle the backlight on and off.
The problem im having is finding a way to impliment the backlight controls
lcd.backlight()
and
lcd.nobacklight()
Im using the NewLiquidCrystal library.
The compiler is seeing the lcd.backlightstate as a boolean value
exit status 1
could not convert 'lcd.LiquidCrystal_I2C::.LCD::backlight()' from 'void' to 'bool'
is there an easier way to do this?
I am aware the code is a mess, ive cut and pasted bits of code from other sketches and havent had a chance to tidy it up and make it logical yet. Hopefullt ive commented it enough for everyone to understand my train of thought though.
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <Wire.h>
#define DHTTYPE DHT22 // DHT 22 (AM2301)
#define DHTPIN 2 // what digital pin we're connected to
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
/***********************************************************************
Digital pin 3 normaly low
momentary button connected to toggle LCD backlight state when pressed.
using millis() for debounce
***********************************************************************/
const int buttonPin = 3; //digital pin button is connected to
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() /*----( SETUP: RUNS ONCE )----*/
{
pinMode(buttonPin, INPUT); //initialise button pin as input
lcd.begin(16, 2); // initialize the lcd for 20 chars 4 lines, turn on backlight
lcd.backlight();
dht.begin();
}/*--(end setup )---*/
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
/********************************************************************
*Read DHT sensor and display to I2C LCD screen
********************************************************************/
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
lcd.setCursor(0, 0); //Set cursor at start of first line
lcd.print("Temp ");
lcd.print(t);
lcd.print((char)223); //Degree symbol (°)
lcd.print("C");
lcd.setCursor(0, 1); //Set cursor at start of second line
lcd.print("Humidity ");
lcd.print(h);
lcd.print("%");
}
/*****************************************************************************
Button debounce code
*****************************************************************************/
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// change the backlight state
if (buttonState != lastButtonState) { //if buttonState is not equal to lastButtonState
{ if (lcd.backlight()) {
lcd.nobacklight()
};
else if (lcd.nobacklight()) {
lcd.backlight()
};
}
}
}
}
}
/* --(end main loop )-- */