What is a simple alternative to delay? How would it look in my code?

Hi I am very much a newbie at this I have been using delay for my code but it is causing problems for executing keyboard presses. I am trying to find a simple alternative. I have tried Millis and it didn’t work well for me. I am just trying to get LEDs to blink at a 500 milliseconds on off pattern and a 250 milliseconds on off pattern. I just want a simple line of code to go on top of where I digitalWrite my LEDs. Something like ‘Blink(500)’ or ‘On(500)’. How would I define something like that? Thanks for any help you can provide! Code is below.

const int PowerSwitch = 12;
const int Floorlight = 11;
const int FloorButton = 10;
const int Stopresetlight = 9;
const int StopresetButton = 8;
const int Stoplight = 7;
const int StopButton = 6;
const int GatesSwitch = 5;
const int Harnesslight = 4; 
const int HarnessButton = 3;
const int Dispatchlight = 2;
const int DispatchButton = 1;
boolean DispatchButtonbuttonPressed; 
boolean GatesSwitchbuttonPressed;
#include <Keyboard.h>


void setup() {
 
   pinMode(Dispatchlight,OUTPUT); 
  pinMode(DispatchButton, INPUT_PULLUP);
  pinMode(Harnesslight, OUTPUT);
  pinMode(HarnessButton, INPUT_PULLUP);
  pinMode(GatesSwitch, INPUT_PULLUP);
  pinMode(StopButton, INPUT_PULLUP);
  pinMode(Stoplight, OUTPUT);
  pinMode(StopresetButton, INPUT_PULLUP);
  pinMode(Stopresetlight, OUTPUT);
  pinMode(FloorButton, INPUT_PULLUP);
  pinMode(Floorlight, OUTPUT);
  pinMode(PowerSwitch, INPUT_PULLUP);
  
  digitalWrite(DispatchButton, HIGH);
  digitalWrite(HarnessButton, HIGH);
  digitalWrite(GatesSwitch, HIGH);
  digitalWrite(StopButton, HIGH);
  digitalWrite(StopresetButton, HIGH);
  digitalWrite(FloorButton, HIGH);
  digitalWrite(PowerSwitch, HIGH);

}
void loop() {

if(digitalRead(GatesSwitch) == LOW) {
  GatesSwitchbuttonPressed = true;
  }else{
    GatesSwitchbuttonPressed = false; 
}
if(GatesSwitchbuttonPressed == true){
  
 Keyboard.press(0xCB);
        Keyboard.release(0xCB);
        digitalWrite(Dispatchlight, LOW);
        digitalWrite(Stopresetlight,LOW);
        digitalWrite(Harnesslight,LOW);
        digitalWrite(Floorlight,LOW);
        digitalWrite(Stoplight,LOW);

        
}if(GatesSwitchbuttonPressed == false){

  
   Keyboard.press(0xCC);
        Keyboard.release(0xCC);

        
if (digitalRead(DispatchButton) == HIGH) {
  DispatchButtonbuttonPressed = true;
 } else {
  DispatchButtonbuttonPressed = false;
 }

    if(DispatchButtonbuttonPressed == true) {
       Keyboard.press(0xCD);
        Keyboard.release(0xCD);
        digitalWrite(Dispatchlight, HIGH);
        digitalWrite(Stopresetlight,HIGH);
        digitalWrite(Harnesslight,HIGH);
        digitalWrite(Floorlight,HIGH);
        digitalWrite(Stoplight,HIGH);
        delay(500);
        digitalWrite(Stoplight,LOW);
        delay(500);
        
    }
   else {
        digitalWrite(Dispatchlight, HIGH);
        digitalWrite(Stopresetlight,HIGH);
        digitalWrite(Harnesslight,HIGH);
        digitalWrite(Floorlight,HIGH);
        digitalWrite(Stoplight,HIGH);
        delay(500);
        digitalWrite(Dispatchlight, LOW);
        digitalWrite(Stopresetlight,LOW);
        digitalWrite(Harnesslight,LOW);
        digitalWrite(Floorlight,LOW);
        digitalWrite(Stoplight,LOW);
        delay(500);
    }

 
}
   
}

"I have tried Millis and it didn't work well for me."

I'm sorry but this is kind of like saying, "I have tried downhill skiing but skiis didn't work well for me". There are other methods that you can use, for example interrupts, but those are even more complicated.

We would like to see your failed attempts at using millis(), then maybe we can help.

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Look at BlinkWithoutDelay. You can use the code in the example loop in a separate function and call that function every time through loop() in order to blink the LED. Replicate the function for other LEDs you want to blink. Add flags for turning the LEDs on and off and either call the functions conditionally or have the function check the flag and return if there is nothing to do. Make sure if you want to stop flashing that you end in a state where the LED is off.

Please explain what you really want to do, and what this means:

it is causing problems for executing keyboard presses.

Also, please clean up your code by using ^T and putting if statements on separate lines, as shown below:

const int PowerSwitch = 12;
const int Floorlight = 11;
const int FloorButton = 10;
const int Stopresetlight = 9;
const int StopresetButton = 8;
const int Stoplight = 7;
const int StopButton = 6;
const int GatesSwitch = 5;
const int Harnesslight = 4;
const int HarnessButton = 3;
const int Dispatchlight = 2;
const int DispatchButton = 1;
boolean DispatchButtonbuttonPressed;
boolean GatesSwitchbuttonPressed;
#include <Keyboard.h>


void setup() {

  pinMode(Dispatchlight, OUTPUT);
  pinMode(DispatchButton, INPUT_PULLUP);
  pinMode(Harnesslight, OUTPUT);
  pinMode(HarnessButton, INPUT_PULLUP);
  pinMode(GatesSwitch, INPUT_PULLUP);
  pinMode(StopButton, INPUT_PULLUP);
  pinMode(Stoplight, OUTPUT);
  pinMode(StopresetButton, INPUT_PULLUP);
  pinMode(Stopresetlight, OUTPUT);
  pinMode(FloorButton, INPUT_PULLUP);
  pinMode(Floorlight, OUTPUT);
  pinMode(PowerSwitch, INPUT_PULLUP);

  digitalWrite(DispatchButton, HIGH);
  digitalWrite(HarnessButton, HIGH);
  digitalWrite(GatesSwitch, HIGH);
  digitalWrite(StopButton, HIGH);
  digitalWrite(StopresetButton, HIGH);
  digitalWrite(FloorButton, HIGH);
  digitalWrite(PowerSwitch, HIGH);

}
void loop() {

  if (digitalRead(GatesSwitch) == LOW) {
    GatesSwitchbuttonPressed = true;
  } else {
    GatesSwitchbuttonPressed = false;
  }
  if (GatesSwitchbuttonPressed == true) {

    Keyboard.press(0xCB);
    Keyboard.release(0xCB);
    digitalWrite(Dispatchlight, LOW);
    digitalWrite(Stopresetlight, LOW);
    digitalWrite(Harnesslight, LOW);
    digitalWrite(Floorlight, LOW);
    digitalWrite(Stoplight, LOW);


  }

  if (GatesSwitchbuttonPressed == false) {
    Keyboard.press(0xCC);
    Keyboard.release(0xCC);
    if (digitalRead(DispatchButton) == HIGH) {
      DispatchButtonbuttonPressed = true;
    } else {
      DispatchButtonbuttonPressed = false;
    }
    if (DispatchButtonbuttonPressed == true) {
      Keyboard.press(0xCD);
      Keyboard.release(0xCD);
      digitalWrite(Dispatchlight, HIGH);
      digitalWrite(Stopresetlight, HIGH);
      digitalWrite(Harnesslight, HIGH);
      digitalWrite(Floorlight, HIGH);
      digitalWrite(Stoplight, HIGH);
      delay(500);
      digitalWrite(Stoplight, LOW);
      delay(500);
    }
    else {
      digitalWrite(Dispatchlight, HIGH);
      digitalWrite(Stopresetlight, HIGH);
      digitalWrite(Harnesslight, HIGH);
      digitalWrite(Floorlight, HIGH);
      digitalWrite(Stoplight, HIGH);
      delay(500);
      digitalWrite(Dispatchlight, LOW);
      digitalWrite(Stopresetlight, LOW);
      digitalWrite(Harnesslight, LOW);
      digitalWrite(Floorlight, LOW);
      digitalWrite(Stoplight, LOW);
      delay(500);
    }
  }
}

Then, study this large if block, starting here, and note that the computer spends 99.999% of the time in delay().

  if (GatesSwitchbuttonPressed == false) {

Don’t place the emphasis on ‘simple’.

There are many examples of using the BWD technique on this web site, see Robin2’s discussion and the examples in the ‘Tutorial’ forum.

Also study the ‘State Machine’ examples in the ‘Tutorial’ section.

a "simple" replacement for delay would be something like:

if ((millis() % 1000) == 0) {
   // do something once every second
   // ...
   while ((millis()%1000) == 0) {
      // wait doing nothing until we're not in the current millisecond...
   }
}

(there are complications. Every once in a while it will miss a second...)

So I am trying to make a control panel for No Limits 2. I found a good video that explains a lot. It goes over what the best way to do button presses is. Here is the link: Build a NL2 Control Panel: Getting Started with Button Presses and LEDs - YouTube the part where he talks about the buttons blinking starts at 5:43. I am trying to use the code that he made for the 500 millisecond blinks:

void blink500() { 
  unsigned long c500 = millis();
  if(c500 - p500> 500) {
    p500 = c500;
    if(m500 == false) {
      m500= true;
    } else{ 
      m500 = false;
    }
  }
}

It gives me a ‘p500 was not declared in this scope’ I am assuming that the ‘c500’ statement is going to give me a similar error. In the video I pulled this code from he didn’t show how he declared any of this in his setup. How would I declare this in my setups? Is this even valid for what I want to do? Later in the code he just uses an ‘if(!m500)’ to use the blink code. It is perfect and super simple for what I want to do. Will it work for me? My whole code is below. Thanks for any help.

const int PowerSwitch = 12;
const int Floorlight = 11;
const int FloorButton = 10;
const int Stopresetlight = 9;
const int StopresetButton = 8;
const int Stoplight = 7;
const int StopButton = 6;
const int GatesSwitch = 5;
const int Harnesslight = 4; 
const int HarnessButton = 3;
const int Dispatchlight = 2;
const int DispatchButton = 1;
boolean DispatchButtonbuttonPressed; 
boolean GatesSwitchbuttonPressed;
#include <Keyboard.h>


void setup() {
 
   pinMode(Dispatchlight,OUTPUT); 
  pinMode(DispatchButton, INPUT_PULLUP);
  pinMode(Harnesslight, OUTPUT);
  pinMode(HarnessButton, INPUT_PULLUP);
  pinMode(GatesSwitch, INPUT_PULLUP);
  pinMode(StopButton, INPUT_PULLUP);
  pinMode(Stoplight, OUTPUT);
  pinMode(StopresetButton, INPUT_PULLUP);
  pinMode(Stopresetlight, OUTPUT);
  pinMode(FloorButton, INPUT_PULLUP);
  pinMode(Floorlight, OUTPUT);
  pinMode(PowerSwitch, INPUT_PULLUP);
  
  digitalWrite(DispatchButton, HIGH);
  digitalWrite(HarnessButton, HIGH);
  digitalWrite(GatesSwitch, HIGH);
  digitalWrite(StopButton, HIGH);
  digitalWrite(StopresetButton, HIGH);
  digitalWrite(FloorButton, HIGH);
  digitalWrite(PowerSwitch, HIGH);

}
void loop() {

if(digitalRead(GatesSwitch) == LOW) {
  GatesSwitchbuttonPressed = true;
  }else{
    GatesSwitchbuttonPressed = false; 
}
if(GatesSwitchbuttonPressed == true){
  
 Keyboard.press(0xCB);
        Keyboard.release(0xCB);
        digitalWrite(Dispatchlight, LOW);
        digitalWrite(Stopresetlight,LOW);
        digitalWrite(Harnesslight,LOW);
        digitalWrite(Floorlight,LOW);
        digitalWrite(Stoplight,LOW);

        
}if(GatesSwitchbuttonPressed == false){

  
   Keyboard.press(0xCC);
        Keyboard.release(0xCC);

        
if (digitalRead(DispatchButton) == HIGH) {
  DispatchButtonbuttonPressed = true;
 } else {
  DispatchButtonbuttonPressed = false;
 }

    if(DispatchButtonbuttonPressed == true) {
       Keyboard.press(0xCD);
        Keyboard.release(0xCD);
        digitalWrite(Dispatchlight, HIGH);
        digitalWrite(Stopresetlight,HIGH);
        digitalWrite(Harnesslight,HIGH);
        digitalWrite(Floorlight,HIGH);
        digitalWrite(Stoplight,HIGH);
        delay(500);
        digitalWrite(Stoplight,LOW);
        delay(500);
        
    }
   else {
        digitalWrite(Dispatchlight, HIGH);
        digitalWrite(Stopresetlight,HIGH);
        digitalWrite(Harnesslight,HIGH);
        digitalWrite(Floorlight,HIGH);
        digitalWrite(Stoplight,HIGH);
        delay(500);
        digitalWrite(Dispatchlight, LOW);
        digitalWrite(Stopresetlight,LOW);
        digitalWrite(Harnesslight,LOW);
        digitalWrite(Floorlight,LOW);
        digitalWrite(Stoplight,LOW);
        delay(500);
    }

 
}
   
}