Question: Running a loop while other things are happening

Hi,
I have a program where once you press a button, I want the arduino to wait 3 seconds before starting 2 LEDs flickering. After another 3 seconds I wish to send another pin HIGH (and still have the LEDs flickering).
My problem is that the LEDs take longer than 3 seconds to start flickering, and come on at the same time as the final pin is sent HIGH.

My code:

int buttonPin = 2;   
int ledPin1 = 11;
int ledPin2 = 10;
int elPin = 7;

int buttonState = 0;       
int ledState = 0;

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(elPin, OUTPUT);
  
  Serial.begin(9600);           // set up Serial library at 9600 bps
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if(buttonState == HIGH) {
    delay(3000); // Starting delay
    ledState = 1;
    delay(3000); // Delay before el wire
    digitalWrite(elPin, HIGH);   // set the EL Wire on
  }
  
  if(ledState == 1) {
    analogWrite(ledPin1, random(200)+55);
    analogWrite(ledPin2, random(200)+55);
    delay(random(50));  
  }
}

Thanks for your help!
:slight_smile:

Have a look at the blink without delay example, without delay.

Thanks worked a charm!