LCD Nightlight [Liquid Crystal Display!!!]

Hello everyone,
This is a really big project for me and I have a really big problem.
I put together a circuit shown below that acts as a nightlight. A photo resistor is connected to Analog Pin 0 and the RGB LED is connected to pin 5. The LCD is connected to pins 12, 11, 10, 9, 8, 7. The potentiometer is hooked up with it.

My goal is when the photo resistor senses darkness, it should display the text “Its Dark!” and light up the RGB LED blue. When it senses light, it should display “Daytime!” and turn the RGB off.

When I uploaded this code

int Sensepin = 0;
int Bpin = 5;
#include <LiquidCrystal.h>
LiquidCrystal LCD(12, 11, 10, 9, 8, 7);



void setup() {
analogReference(DEFAULT);
pinMode(Bpin, OUTPUT);
LCD.begin(16, 2);
Serial.begin(9600);
}

void loop() {
  int val = analogRead(Sensepin);
  if(val<800) {
     digitalWrite(Bpin, HIGH);
     LCD.print("Turn LED on!");
  }
 
  else {
    LCD.print("Daytime!");
  digitalWrite(Bpin, LOW);
  }
  
}

NOTHING HAPPENED except the LCD backlight and light turn on. The potentiometer works and all. But the photo resistor doesn’t nor does the LED. When I put my hand over it, it didn’t do anything. The screen just had a bunch of white rectangles (which I know is supposed to happen) and no text. Is my program wrong or is my circuit wrong.

I am using an Arduino Leonardo and a Prototype Shield and using apple machine.

The board is fine and the code loaded properly. Is my code or circuit wrong?
I am a newbie so I might not know some words that other great people would use to help me.

(sorry, i named the pictures randomly.)

5.jpg

2.jpg

1.jpg

I might be impatient but I really need help. If my program is completely messed up, please just tell me.

Well, you waited nearly 2 1/2 hours before you “bumped” this and I’m not sure how long since, but no bites.
So, OK.
I’m sure that now that I’m posting everyone else will want his say, too.

I can’t see how you have your LDR set up.
Perhaps you could take a pic, or sketch out how you’ve wired that.

I took out the LCD lines and put in a Serial.print, so that “we” can see how that LDR is working via Serial Monitor.
You might want to use something more than your hand to darken the LDR.
Pretty much leaving you to troubleshoot that aspect.

int Sensepin = 0;
int Bpin = 5;
//#include <LiquidCrystal.h>
//LiquidCrystal LCD(12, 11, 10, 9, 8, 7);

void setup() 
{
  //analogReference(DEFAULT);
  pinMode(Bpin, OUTPUT);
  digitalWrite(Bpin, LOW);
  //LCD.begin(16, 2);
  Serial.begin(9600);
}

void loop() 
{
  int val = analogRead(Sensepin);
  Serial.print(val, DEC);
  if(val<800) 
  {
    digitalWrite(Bpin, HIGH);
  }
  else 
  {
    digitalWrite(Bpin, LOW);
  }
  delay(1000);
}

The LDR is connected to the Analog pin 0.
Is this picture good enough?

4.jpg

I uploaded the code, it works fine. The LED turns on when I put my hand over it. But, for the LCD display what would I have to add to the code to make it work. Would the serial print be in the void setup?

Thank you, s_tanay

s_tanay:
I uploaded the code, it works fine. The LED turns on when I put my hand over it.
But, for the LCD display what would I have to add to the code to make it work. Would the serial print be in the void setup?

Thank you,
s_tanay

The original post has the LCD in the if{} which is where it could go (if not a separate function.)

I believe the issue is that you need to initialize the LCD in setup{} as you have done with lcd.begin() and then you likely need to lcd.clear() and lcd.setCursor() each time in the “if” section.

something like:

  if(val<800) 
  {
   LCD.clear() ;
   LCD.setCursor(0, 0) ;
   LCD.print("Turn LED on!");
    digitalWrite(Bpin, HIGH);
  }
  else 
  {
   LCD.clear() ;
   LCD.setCursor(0, 0) ;
   LCD.print("Daytime!");
    digitalWrite(Bpin, LOW);
  }

Note: Technically, LCD.clear() will do the LCD.setCursor(0, 0) for you, but putting that in the code is consider good practice in case you wish to edit to something like LCD.setCursor(1, 0).