Hi,
I already had a thread running for a push button to toggle the backlight, well I seem to be having persistent problems so I thought it would be easier to start another thread to explain the issues.
I've got a temp/humidity project with a 16x2 display. Was all working perfectly. Installed code so that the backlight could be toggled via a button. Decided to get another screen which has a I2C adapter soldered on, which I got working with my program but without the button to toggle the light. The light remains on.
working code I2C (with no toggle working). There is still code in there for the button when the I2C display was not being used, but just showing the code to show what was working
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include "DHT.h"
#define DHTPIN A0 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22
// Set pins for lcd display backlight
#define BUTTON_PIN A5 // The number of the push-button pin.
#define LCD_LIGHT_PIN A4 // The number of the pin where anode of the display backlight is.
#define LCD_LIGHT_ON_TIME 10000 // How long (in milliseconds) should lcd light stay on
unsigned int currentLcdLightOnTime = 0;
// For calculating the lcd light on time.
unsigned long lcdLightOn_StartMillis;
boolean isLcdLightOn;
// For checking push-button state.
int buttonState = 0;
#define NUM_LEDS 3
const byte pinLED_LOW = 6; // Blue LED
const byte pinLED_MED = 9; // Green LED
const byte pinLED_HIGH = 10; // Red LED
const byte pinArray[NUM_LEDS] =
{
pinLED_LOW,
pinLED_MED,
pinLED_HIGH
};
//LiquidCrystal_I2C lcd(0x27, 16, 2); //define pins used for LCD display
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600);
for ( int i=0; i<NUM_LEDS; i++ )
pinMode( pinArray[i], OUTPUT );
lcd.begin(16, 2); //16 by 2 character display
// Set the push-button pin as an input for LCD backlight
pinMode(BUTTON_PIN, INPUT);
// Set the lcd display backlight anode pin as an output.
pinMode(LCD_LIGHT_PIN, OUTPUT);
// Set the lcd display backlight anode pin to low - lcd light off.
digitalWrite(LCD_LIGHT_PIN, LOW);
isLcdLightOn = false;
// maybe need }
dht.begin();
}
void loop()
{
delay(2000);
// Set as 1000 for DHT11 and 2000 for DHT22 as slow sensors and only read at these intervals
// 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();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: ");
//lcd.print(t);
lcd.print(t,1);
lcd.print("\337C");
lcd.setCursor(0,1);
lcd.print("Humid: ");
//lcd.print(h);
lcd.print(h,1);
lcd.print("%");
//the 3-led setup process
if (t<=20.9)
{
digitalWrite(pinLED_LOW, HIGH);
digitalWrite(pinLED_MED, LOW);
digitalWrite(pinLED_HIGH, LOW);
}
else if (t>21 && t<22.9)
{
digitalWrite(pinLED_LOW, LOW);
digitalWrite(pinLED_MED, HIGH);
digitalWrite(pinLED_HIGH, LOW);
}
else if (t>=23)
{
digitalWrite(pinLED_LOW, LOW);
digitalWrite(pinLED_MED, LOW);
digitalWrite(pinLED_HIGH, HIGH);
}//
// Check the state of the push-button.
buttonState = digitalRead(BUTTON_PIN);
if (buttonState == HIGH){
// Button pressed.
lcdLightOn_StartMillis = millis();
currentLcdLightOnTime = 0;
isLcdLightOn = true;
digitalWrite(LCD_LIGHT_PIN, HIGH);
}
else{
// Button not pressed.
//Serial.println("Button not pressed - LOW");
if(isLcdLightOn){
currentLcdLightOnTime = millis() - lcdLightOn_StartMillis;
if(currentLcdLightOnTime > LCD_LIGHT_ON_TIME){
isLcdLightOn = false;
digitalWrite(LCD_LIGHT_PIN, LOW);
}
}
}
}
I've been looking online, reading loads and trying different codes, to try and get my head around it all. Well I copied a code which apparently worked, so that I could understand how it worked.
I started having issues, not uploading to the Uno, or problems with the code itself. After reading a bunch of threads, I uninstalled all of the lcd libraries which I'd been using and installed the fmalpartida library.
Also I came across the guesser descriptor script which worked perfectly. Using the data found in the script, I copied that over to the original code which again, worked but without the backlight toggle.
Persisting with the sample code, I wrote in the LCD details found in the guesser script, but I only get errors. Firstly it was to do with 'POSITIVE' although that's gone away for now.