Help, How to delay using millis

Hi forums.

i really having difficult for using delay, i know theres a lot libary, but the procedure is too complex for me, all i need is just blink using millis when the trigger occurs.

example usecase

when button is pressed
turn on led for 1 sec
then off

heres the code.


void loop()
{

if (digitalRead(ShifterPin)==HIGH)
        {
          if(digitalRead(ShifterPin)==HIGH && ShiftState==0)
            digitalWrite(RelayPin,HIGH);
            digitalWrite(ShiftLight,HIGH);
            delay(QC_Duration); << i want this using millis instead of delay
            digitalWrite(RelayPin,LOW);
	        digitalWrite(ShiftLight,HIGH);
            ShiftState=1;
        }
        else 
        {
          ShiftState=0;
        }
}

see the BlinkWithout Delay example in the IDE

A couple more millis() timing tutorials:

Beginner's guide to millis().
Several things at a time.

Here is an example of one way to do what you want. The stated change method and switch wiring is covered in the state change for active low inputs tutorial. The timer is covered in the previously linked tutorials on millis() timing.

// LED lights for 1 second upon a button press
// Button swich wired to ground and pin (INPUT_PULLUP)
// by c gouling aka groundFungus

const byte buttonPin = 4;
const byte ledPin = 7;

const unsigned long ledOnTime = 1000;  // 1 second on time

void setup()
{
    Serial.begin(115200);
    pinMode(buttonPin, INPUT_PULLUP);
    pinMode(ledPin, OUTPUT);
    
}

void loop()
{
   static unsigned long timer = 0;
   static bool runTimer = false;
   
   // state change detection to sense switch closure
   static bool lastButtonState = HIGH;
   bool buttonState = digitalRead(buttonPin);
   if(buttonState != lastButtonState)
   {      
      //  button is active low and locked out during on time
      if(buttonState == LOW && runTimer == false) 
      {
         runTimer = true;
         timer = millis();
         digitalWrite(ledPin, HIGH);
      }
      lastButtonState = buttonState;
   }

   // LED on timer
   if(runTimer == true)
   {
      if(millis() - timer >= ledOnTime)
      {
         runTimer = false;
         digitalWrite(ledPin, LOW);
      }
   }
}

Hello an3728

Do you have experience using C++?

How many button/led combinations do you have?

This is actually brilliant, looks convincing, definitely will try with your code

I see you are using button input as trigger the timer right? If so, that's actually smart way to do delay, i never tough this method before

Actually i never play with C++ before , but i do have the fundamental for coding, because I know other programing languages like javascript and SQL

About the button and led

Actually i have more than 2 input as a buttons
And 2 output, 1 for relay and 1 for led

But don't mind that, once i understand the logic to implement millis with button trigger, I'm pretty sure I can implement it to others

For shure.
But you will create code duplicates.
Part of the code for the LED and another part of the code for the relay. This will continue with other devices.

OOP with C++ prevents this behaviour by using objects and methods that do the processing.

For your project, you will have an object that contains the data for the button, the output and the timing data for debouncing and turning on the output.
Two services take care of debouncing the button and switching on the outputs.

Sure thing, i would make separate variabel for each function, i maybe use button library for make it simple and easy to read. Thanks for the guidance