If... else....

I have a piece of code, it looks good to me, but it doesn't work properly.
Arduino nano with LCD display, DHT11 and photo resistor. The purpose of the photo resistor is, if the light in the room is off, i need to turn off the display. All is good, but the else statement doesn't seem to ever evaluate.

Partial code below:
if (lightLevel > 40) {
isLcdLightOn = true;
digitalWrite(LCD_LIGHT_PIN, HIGH);
lcd.display();
lcd.print("Display ON");
}
else {
isLcdLightOn = false;
digitalWrite(LCD_LIGHT_PIN, LOW);
lcd.clear();
lcd.noDisplay();
}

I print the value of the integer variable "lightLevel", the value changes when I cover the photoresistor. When I print lightLevel > 40 while the light is on, I get a 1. When I print the value when the light is off, I get a 0, so the variables are fine, but the else statement never executes. What am I not seeing?

You know those irellevant pieces of code at the top of the file? Those ones that say things like int lightLevel;?

We need to see those parts of the code to debug your problem. The piece of code you posted is obviously the piece you understand. The problem is in the parts you don't understand.

Why do you think else is not executed versus your LCD functions not working?

Partial code below:

http://snippets-r-us.com/

Hi JML

I tried the functions individually.

  • In the setup, I start with the back light turned off. It works.
  • I print the value of the integer lightLevel variable right before I go into the if statement. The value changes as expected when I cover the sensor or turn off the room light.
  • I also tested the lightLevel > 40… lcd.print(lightLevel > 40) prints a 1 on the screen. lcd.print(lightLevel < 40) prints a 0.

Below is the complete code. I cleaned it a bit.

#include <LiquidCrystal.h>
#include <dht.h>

#define LCD_LIGHT_PIN A4 // The number of the pin where anode of the display backlight is.
#define LCD_LIGHT_ON_TIME 60000 // How long (in milliseconds) should lcd light stay on?

#define PhotoResistorPin A2
// We'll also set up some global variables for the light level:
int lightLevel;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int greenPin = A0;
dht sensor;


boolean isLcdLightOn;

void setup()
{
 pinMode(PhotoResistorPin, 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);
 
 
 lcd.begin(16,2); //16 by 2 character display
 lcd.clear();
 lcd.setCursor(4,0);
 lcd.print("Hello...");
}

void loop()
{
 delay(1000); //wait a sec (recommended for DHT11)
 sensor.read11(greenPin);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Temp = ");
 lcd.print(sensor.temperature);  
 lcd.setCursor(0,1);
 lcd.print("Humidity = ");
 lcd.print(sensor.humidity);

 lightLevel = analogRead(PhotoResistorPin);
 
 delay(2000);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Light Level = ");
 lcd.print(lightLevel);  

 lcd.setCursor(0,1);
 if (lightLevel > 40) {
   digitalWrite(LCD_LIGHT_PIN, HIGH);
   lcd.display();
   lcd.print("Display ON");
 }  
 else {
   digitalWrite(LCD_LIGHT_PIN, LOW);
   lcd.clear();
   lcd.noDisplay();    
 }

}

An LCD backlight can draw more then the maximum current that an arduino pin can provide. Do you have a current limiting resistor? (If not you may have damaged the pin you connected the backlight to directly)

J-M-L:
An LCD backlight can draw more then the maximum current that an arduino pin can provide. Do you have a current limiting resistor?

Yes. I also have a separate power source going to the breadboard. GND are tied together.

Reply #2 asked how you know that the else is not executed.

Add debugging statements; e.g. serial.print

void setup()
{
  Serial.begin(9600);

  ...
  ...
}

void loop()
{
  ...
  ...

  if (lightLevel > 40)
  {
    Serial.println(">40");
    delay(2000);

    digitalWrite(LCD_LIGHT_PIN, HIGH);
    lcd.display();
    lcd.print("Display ON");
  }
  else
  {
    Serial.println("<=40");
    delay(2000);

    digitalWrite(LCD_LIGHT_PIN, LOW);
    lcd.clear();
    lcd.noDisplay();
  }
}

Open the serial monitor and check the output. That will confirm if the else is executed; I’ve added an additional delay to make sure that power issues will not interfere with the serial transmission.

Thanks. That did it. The example I blindly copied from was using 25 as the compare value. I changed it to 40 to suit my need, but now when i printed to the serial monitor I found it is actually 400. I didn't see it because I was using the lcd screen to debug and the last digit was truncated. Now it works perfectly.
Thanks again.

Good !!! Merry Xmas

Thanks. Merry Christmas to you too :slight_smile: