Print to LCD when something happens?

I have an Arduino Uno connected with a 1602 LCD with i2c connections and all works fine. I have an LDR sensor and i want to print "Welcome" to the LCD when the light turns on and the "welcome" message should stay on for 5 seconds before the loop goes on and do the rest.

Every time the lights turns on in the room i want this message to show in 5 seconds.

I do not have the code here, but is there an general method to code this?

A general way to code this would be something like:

setup()  {
  // put your setup code here, to run once:

   initialize sensors
   initialize lcd
}

loop()  {
  // put your main code here, to run repeatedly:
  
  check sensor for light on
  print message to lcd
  delay(5000)  // 5000 miliseconds = 5 seconds

  and the rest of the code
}

That should be the general direction you should be looking in....

Randy

Ok, thanks...

...but that approach won't work if you intend doing other stuff in the five seconds.

TheMemberFormerlyKnownAsAWOL:
...but that approach won't work if you intend doing other stuff in the five seconds.

I know, and thats my problem.

My LCD shows temperature and humidity and that works fine. But when i turn the lights on, i want the display to show "Welcome" in 5 sec. before humidity and temperature shows again.

I want welcome to show in 5 sec. every time i turn on light.

The usual advice here is "take a look at the blink without delay example in the IDE".

TheMemberFormerlyKnownAsAWOL:
The usual advice here is "take a look at the blink without delay example in the IDE".

I see. I have read about millis several times, but i just don't understand how it works. i have tried to find an explanation for absolute dummies, but i just don't find it.

I'm understand most of the other functions, but millis i have trouble with...

Of course, i can just paste an code and try to change som numbers, but i also want to understand what happening and how.

I see. I have read about millis several times, but i just don't understand how it works. i have tried to find an explanation for absolute dummies, but i just don't find it.

Start by looking at Using millis() for timing. A beginners guide then move on to Several things at the same time

Write some code and come back with any queries

revolt_randy:
That should be the general direction you should be looking in....

TheMemberFormerlyKnownAsAWOL:
...but that approach won't work if you intend doing other stuff in the five seconds.

And apart from that, you need to check that the light has just gone on (ldr changed state dark to light) rather than is on, else you'll get the 5s message every time through loop() as long as the light's on.

fishboneDiagram:
And apart from that, you need to check that the light has just gone on (ldr changed state dark to light) rather than is on, else you'll get the 5s message every time through loop() as long as the light's on.

Yes, that's exactly what's happening now.

But i guess i ounce for all must learn how to use millis then...

Bjerknez:
But i guess i ounce for all must learn how to use millis then...

It's probably the single most useful tool in the Arduino toolbox; time spent on that for this project will bear fruit dozens of times on other projects.

(I hate it when people say "trust me", but, you know, "trust me" :wink: )

In this case though, you'll need to check the state change detect tutorial for how to see the ldr has "flipped".

I dug out a DHT11/lcd sketch I had, and bolted the ldr sensing and 5s message to it.

I'll post it if you want it, but not till you ask in case you want to "grapple" with it further :wink:

I put the ldr from a digital pin to ground with input pullup. It's high when dark and low in the light, so looking for room dark to light transition is an ldr state change from high to low. When that happens, display the message and make a note of the time with millis(). Then monitor the elapsed time....

(My existing sketch also uses millis() to read the dht only every second, and blinks a : on lcd so you can see it's alive if the numbers aren't changing.)

fishboneDiagram:
I dug out a DHT11/lcd sketch I had, and bolted the ldr sensing and 5s message to it.

I'll post it if you want it, but not till you ask in case you want to "grapple" with it further :wink:

I put the ldr from a digital pin to ground with input pullup. It's high when dark and low in the light, so looking for room dark to light transition is an ldr state change from high to low. When that happens, display the message and make a note of the time with millis(). Then monitor the elapsed time....

(My existing sketch also uses millis() to read the dht only every second, and blinks a : on lcd so you can see it's alive if the numbers aren't changing.)

Just post it. Thanks :slight_smile:

Bjerknez:
Just post it. Thanks :slight_smile:

Roger that.

Note: my libraries may not be the ones you have.

// https://forum.arduino.cc/index.php?topic=663849
// dht11 to lcd
// with welcome message if room light goes on
// ldr is from digital pin (input pullup) to ground
//     (high dark, low light... check it changes dark to light with normal state change detect)
// 10 feb 2020

#include <dht11.h>
dht11 DHT;
const byte DHT11_PIN = 5;
//dht read timing stuff
unsigned long lastDHTread;
int DHTreadInterval = 1000;
bool colonWigWag; //proof of life

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

//ldr
// this constant won't change:
const byte  button = 2; //ldr actually
// Variables will change:
bool buttonState;         // current state of the button
bool lastButtonState;     // previous state of the button
unsigned long roomGotLightAt;
int keepMessageFor = 5000;

void setup()
{
  Serial.begin(9600);
  Serial.println("dht, lcd, ldr, forum 663849");
  pinMode(button, INPUT_PULLUP);
  //initialize button states
  buttonState = digitalRead(button);
  lastButtonState = buttonState;
  lcd.begin();
  lcd.backlight();
  lcd.print("     DHT11");
  delay(2000);
  lcd.clear();
}

void loop()
{
  doDHT();
  checkLDR();
  clearMessage();
}//loop

void doDHT()
{
  if (millis() - lastDHTread >= DHTreadInterval)
  {
    lastDHTread = millis();
    DHT.read(DHT11_PIN);    // READ DATA
    lcd.setCursor(0, 0); //along, down
    if(colonWigWag)
    {
    lcd.print("Temp:");
    lcd.print(DHT.temperature);
    lcd.print((char)223);
    lcd.print("C  RH ");
    lcd.print(DHT.humidity);
    }
    else
    {
    lcd.print("Temp ");
    lcd.print(DHT.temperature);
    lcd.print((char)223);
    lcd.print("C  RH:");
    lcd.print(DHT.humidity);
    }
    colonWigWag=!colonWigWag;   
  }
}//dht

void checkLDR()
{
  // read the button: (actually the ldr but the state change detect example's for a button
  //                   and I didn't bother to change it)
  buttonState = digitalRead(button);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) // != means not equal, so it changed one way or the other
  {
    if (buttonState == LOW) //... and if it's now low, that's a press
    {
      Serial.print("newly light");
      roomGotLightAt = millis();
      lcd.setCursor(0, 1);
      lcd.print("    Welcome     ");
    }// change to low
    else
    {
      Serial.println(", newly dark");
    }// change to high

    // Delay a little bit to avoid bouncing
    delay(50);
  }//change
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}//ldr

void clearMessage() //if time's up
{
  if (millis() - roomGotLightAt >= keepMessageFor)
  {
    lcd.setCursor(0, 1);
    lcd.print("                ");
  }
}//clear message

Bjerknez:
But i guess i ounce for all must learn how to use millis then...

Here's Nick Gammon's tutorial: Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs

Ok, i'm a little stuck here. But i have my code that works fine as for now. I just want the LCD to show "Welcome" in five seconds when the light in room turns on and turns LCD on. Before temp and humidity shows again.

All works fine, in the code you se under, but i want to add an Welcome message when the light in the room turns on for five seconds, and i want this to happend every time the light turns on and LCD lights up.

Here is my code i want to add that possibility to. Can you lease help me with that?

#include <DHT.h>;
#include <LiquidCrystal_I2C.h>

#define DHTPIN 19     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

int lcdColumns = 16;
int lcdRows = 2;

LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

int powerLed = 5;
int sensorPin = 34;
int sensorValue = 0;
int relay = 23;


char auth[] = "''''";

char ssid[] = "''''";
char pass[] = "''''";

void setup()
{
  // Debug console
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600);

// some code that activates and the same time turns LCD in to "sleep mode"
  lcd.init();
  lcd.noDisplay();
  lcd.noBacklight();

  dht.begin();
  pinMode(powerLed, OUTPUT);
  pinMode(relay, OUTPUT);
  pinMode(sensorPin, INPUT);
  digitalWrite(relay, HIGH);
  digitalWrite(powerLed, HIGH);
}

void loop() {
  
// Some code that enables Blynk
  Blynk.run();
  
// some code for writing temperature and humidity values to serial monitor
  hum = dht.readHumidity();
  temp = dht.readTemperature();
  Serial.print("Fuktighet: ");
  Serial.print(hum);
  Serial.print(" %, Temperatur: ");
  Serial.print(temp);
  Serial.println(" Celsius");
  
// code for showing temperature values on LCD display
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temp);

// code for showing the celcius symbol on LCD display
  lcd.write(0b11011111);
  lcd.setCursor(12, 0);
  lcd.print("C");

// code for showing humidity values on LCD display
  lcd.setCursor(1, 1);
  lcd.print("Hum: ");
  lcd.print(hum);
  lcd.print("%");
  
// some code that makes the value "sensorValue"
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); //prints the values coming from the sensor on the

// some statements that turns on and off the LCD display regards to light in the room
  if (sensorValue > 100) {
    lcd.noDisplay();
    lcd.noBacklight();
    digitalWrite(powerLed, LOW);
  }
  if(sensorValue < 100) {
    lcd.display();
    lcd.backlight();
    digitalWrite(powerLed, HIGH);
  }

// some code that sends virtual data to the Blynk app
  Blynk.virtualWrite(V5, temp);
  Blynk.virtualWrite(V6, hum);

// Some delay for not blowing the Blynk server up
  delay(3000);
}

Didn't you like my sketch from #13?

I'm sorry, can you just give me the code i need for my code and scip all the other. I feel i just need the facit to understand this and try more on my one... :slight_smile:

Skip all what other??

My sketch calls 3x functions from loop():

void loop()
{
  doDHT();
  checkLDR();
  clearMessage();
}//loop
  • doDHT() displays the T&H, updates every second, blinks : as proof-of-life
  • checkLDR() monitors the ldr and if room goes from dark to light, displays Welcome
  • clearMessage() cancels the Welcome message after 5s