Blinking an LED but need delay too

Hi.

I’m not sure this will make sense but, here goes.

I need to blink an LED while there is a delay going on.
The delay needs to be there for a timing aspect of the sketch.
I know there is blink whithout delay but, I am using a delay.

I can get the sketch to work how i want by turning the LED on, waiting a second, then turning it off.
This works, I just wondered if there was a neater way that I could do the same thing.

Thank you.

#define RUN 2                   // Define the run/ stop switch pin.

#define LED1 13                 // Define the LED run indicator pin.
void setup() {
 
    pinMode(RUN, INPUT);        // Initialise the pin as an input.
    digitalWrite(RUN, HIGH);    // Set the pullup resister.
    pinMode(LED1, OUTPUT);      // Initialise the pin as an output.
    Keyboard.begin();           // Start keyboard emulation.
}

void loop() {
    
    while(digitalRead(RUN)) {}    // Stop the rest of the loop running until required.
    
    digitalWrite(LED1, HIGH);        // Turn the LED on.
    Keyboard.press(KEY_LEFT_ALT);    // Perform key press.
    Keyboard.press(KEY_TAB);         // Perform key press.
    Keyboard.releaseAll();           // Release all keys.
    delay(500);                      // Half second pause.
    Keyboard.press(KEY_F5);          // Perform key press.
    Keyboard.releaseAll();           // Release all keys.
    delay(1000);
    digitalWrite(LED1, LOW);        // Blink LED and pause for 10 seconds.
    delay(1000);
    digitalWrite(LED1, HIGH);
    delay(1000);
    digitalWrite(LED1, LOW);
    delay(1000);
    digitalWrite(LED1, HIGH);
    delay(1000);
    digitalWrite(LED1, LOW);
    delay(1000);
    digitalWrite(LED1, HIGH);
    delay(1000);
    digitalWrite(LED1, LOW);
    delay(1000);
    digitalWrite(LED1, HIGH);
    delay(1000);
    digitalWrite(LED1, LOW);
    delay(1000);
    
    
    
    }

Is there going to be any other tasks that arduino will run in your future code? If not, there is no need to do it another way. The other way is to use some timers to help you blink, or use state machine to help you keep a more complex blink sequence, instead of simple on 50% time off 50% time etc.

Colaboy:
I need to blink an LED while there is a delay going on.
The delay needs to be there for a timing aspect of the sketch.
I know there is blink whithout delay but, I am using a delay.

You said it. Blink Without Delay will show you the technique. delay() is fine, if you don’t need to do anything lese while the LED is on.

I need to blink an LED while there is a delay going on.
The delay needs to be there for a timing aspect of the sketch.
I know there is blink whithout delay but, I am using a delay.

delay() tells the processor to sit in a loop doing nothing until the delay-time is up. In most real-world programs you want to avoid that.

I can get the sketch to work how i want by turning the LED on, waiting a second, then turning it off.
This works, I just wondered if there was a neater way that I could do the same thing.

Whenever you find your program doing the same thing over-and-over, it's time for a loop. Besides the loop() function, the [u]for-loop[/u] is probably the most common. There are also while() loops and do-while loops.

You can change variables inside the loop - The simplest example is incrementing a variable to count how many times you go through the loop, or you can "sequence" LEDs by switching-on a different LED each time through the loop, etc. In your case, you can "toggle" the state of the LED each time through the loop if the time is up), so that if it's on it turns-off, and if it's off it turns-on. And, you can "break out" of the loop depending on the conditions you set-up.

Loops, conditional branching (if-statements, etc.), and math are the 3 basic concepts that make programming worthwhile!

I wrote a demo sketch in the first post in this Thread that shows how several things can be managed at the same time using the Blink Without Delay technique and State variables.

...R

Thanks for the replies everyone.

I don't need the code to do anything else, that is the whole sketch.

Thanks again.

If everything is working for you now it would be useful to others if you explain how you have solved your problem.

...R