two actions with different delays at the same time?


I have a pushbutton that will trigger three LEDs to blink. No problem of course.

But I also would like to use LCD to print a text “Welcome, please press arrow down” at the same time.

But how do I get the timing correct? The code below delays both the LED and the LCD (of course) in the loop.

How could I have to “separate actions” at the same time? For instance a delay for the LED that the LCD does not care about and vice versa?

With best regards

const int buttonPin = 1;
const int ledPin3 = 3;
const int ledPin5 = 5;
const int ledPin6 = 6;

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 8, 4, 7, 2);

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin3, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
  lcd.begin(16, 2);

void loop() {
  int reading = digitalRead(buttonPin);
  digitalWrite(ledPin3, !reading); 
  digitalWrite(ledPin5, !reading);
  digitalWrite(ledPin6, !reading);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
        // Print a message to the LCD.
     delay (1000);
  lcd.setCursor(0, 0);
    lcd.print("PLEASE PRESS");
  lcd.setCursor(0, 1);
    lcd.print("ARROW DOWN!");
    delay (1500); 

How could I have to "separate actions" at the same time?

You can't. You only have one processor. On the other hand, the few hundred nanoseconds between actions is probably not going to matter.

The blink without delay example will show you how to banish delay() from you repertoire.

Don't use delay(). Instead maintain a timer variable for each the LCD and the LEDs that you update using millis(). Then trigger your actions based of those timers.

Alternatively, have a single timer variable, and trigger based of certain times.

I would also look at using a state machine. It will make the whole thing simpler to work with.