Set delay relay high low and text on lcd

Hi all, I have a problem with my program code. I want to make the relay high and low alternately, but when the conditions are high and low, it displays different text. what is happening now, when the relay is low, it doesn't display text immediately, it has to delay 9000ms first, then text appears, and the text only appears once, and the relay immediately becomes the opposite condition, I want to make a 9000ms delay relay, so that the text appears alternately looping

//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <LiquidCrystal_I2C.h>
int relay =2;

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
  lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("Hello, world!");
  lcd.setCursor(2,1);
  lcd.print("Ywrobot Arduino!");
  lcd.setCursor(0,2);
  lcd.print("Arduino LCM IIC 2004");
  lcd.setCursor(2,3);
  lcd.print("Power By Ec-yuan!");
  delay(2000);
  lcd.clear();
  pinMode(relay, OUTPUT);
}


void loop()
{
digitalWrite(relay,HIGH);delay(9000);
lcd.setCursor(3,0);
lcd.print("Hello, world!");
delay(3000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Ywrobot Arduino!");
delay(3000);
lcd.clear();

digitalWrite(relay,LOW);delay(9000);
lcd.setCursor(0,0);
lcd.print("Arduino LCM IIC 2004");
delay(3000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Power By Ec-yuan!");
delay(3000);
lcd.clear();
}

As a quick and dirty fix you could print the message before the delay()

1 Like

You really should look into millis() timing, and if you want to make it any more flexible, state machines are a must.

2 Likes

as already mentioned, print before the delay:

//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <LiquidCrystal_I2C.h>
int relay = 2;

LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
  lcd.init();                      // initialize the lcd
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(3, 0);
  lcd.print("Hello, world!");
  lcd.setCursor(2, 1);
  lcd.print("Ywrobot Arduino!");
  lcd.setCursor(0, 2);
  lcd.print("Arduino LCM IIC 2004");
  lcd.setCursor(2, 3);
  lcd.print("Power By Ec-yuan!");
  delay(2000);
  lcd.clear();
  pinMode(relay, OUTPUT);
}


void loop()
{
  //step 1
  digitalWrite(relay, HIGH); 
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Relay is HIGH");
  delay(9000);
  
  // step 2
  digitalWrite(relay, LOW);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Relay is LOW");
  delay(9000);
}
1 Like

The microcontroller will run through your code line by line and then, in loop, it will continue to the end then loop back and do it again anon. Try reading your code and translating into English. You can comment it out and include it in the code using //
You should have a good understanding of what each line does and keep in mind the repetition of loop.

1 Like

is it possible to add text that is displayed alternately during relay high or low? For example, when the relay is high, the RELAY IS HIHG display, displays 1 second, then displays , RELAY IS 1, for 1 second as well, and returns to RELAY IS HIGH, and continues to do so, before the relay returns to a low state.

x

Yes that is possible.
Try it.

1 Like

how to add delay for text with 1000ms delay? while there is a delay for the relay 9000ms?

replace 9000ms and do 9 times the delay of 1000ms

1 Like

try this

1 Like

thanks bro, it works

x
You can mark one answer as solution so anyone can see that this thread is solved.
You can say thank you to people by pressing the heart if you think that their post was helpful for you. Recap your thread and read again the answers - there were several good answers!

1 Like

Thanks for reminding me, sorry if I'm new to this forum

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.