Problem with delaying the blinking for second LED using BWD and a button

Hello all,

i am quite new at this programming.
And iam trying to look for this answer, but until now still unsuccesfull.
i want to achieve that after a switch is flipped the BWD loop is running.
but then i want the second led to start after a certain time.

anyone can guide me in the direction how to achieve that?

// constants won't change. Used here to set a pin number:
const int ledPin =  0;// the number of the LED pin
const int ledPin1 =  1;// the number of the LED pin
const int buttonPin = 4;     // the number of the pushbutton pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
int ledState1 = LOW;             // ledState used to set the LED
int buttonState = 0;         // variable for reading the pushbutton status

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated
unsigned long previousMillis1 = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 350;           // interval at which to blink (milliseconds)
const long interval1 = 350;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
    // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}





void loop() {
  // here is where you'd put code that needs to be running all the time.
    // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  
    // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();
  unsigned long currentMillis1 = millis();

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW) {
  
  
      if(currentMillis - previousMillis >= interval){
        //save this reading!
      previousMillis = currentMillis;

        //figure out if you should turn the LED on or off
      if(ledState == LOW){
        ledState = HIGH;
          }else{
            ledState = LOW;
    }
          digitalWrite(ledPin, ledState);
    }
          if(currentMillis1 - previousMillis1 >= interval1){
        //save this reading!
      previousMillis1 = currentMillis1;

        //figure out if you should turn the LED on or off
      if(ledState1 == LOW){
        ledState1 = HIGH;
          }else{
            ledState1 = LOW;
    }
          digitalWrite(ledPin1, ledState1);
    }
    }
    
   else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin1, LOW);
   }
   }
   

"flipped"? Is this a toggle or a push button switch?

sorry to clarify ; a toggle switch

What must happen when the switch is returned to the "off" position?

Horrible formatting fixed:

// constants won't change. Used here to set a pin number:
const int ledPin =  0;// the number of the LED pin
const int ledPin1 =  1;// the number of the LED pin
const int buttonPin = 4;     // the number of the pushbutton pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
int ledState1 = LOW;             // ledState used to set the LED
int buttonState = 0;         // variable for reading the pushbutton status

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated
unsigned long previousMillis1 = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 350;           // interval at which to blink (milliseconds)
const long interval1 = 350;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();
  unsigned long currentMillis1 = millis();

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    if (currentMillis - previousMillis >= interval) {
      //save this reading!
      previousMillis = currentMillis;

      //figure out if you should turn the LED on or off
      if (ledState == LOW) {
        ledState = HIGH;
      } else {
        ledState = LOW;
      }
      digitalWrite(ledPin, ledState);
    }
    if (currentMillis1 - previousMillis1 >= interval1) {
      //save this reading!
      previousMillis1 = currentMillis1;

      //figure out if you should turn the LED on or off
      if (ledState1 == LOW) {
        ledState1 = HIGH;
      } else {
        ledState1 = LOW;
      }
      digitalWrite(ledPin1, ledState1);
    }
  }

  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin1, LOW);
  }
}

Why two? They almost always have the same value...

Solution hint: How would you know the last time the switch was seen to be inactive? Because that is your "certain time" that you would reference the second LED timing to.

like said , iam new to all this , so between copy and pasting iam trying to understand everything.
(and english is not my native language, so there are some issues also in translation)

But i guess the answer has something to relate with the ButtonState ? but i am not sure how to define then to enter a "delay" in starting the blinking of the second LED

also some additional info; iam uploading this sketch trough Arduino UNO as ISP to a ATTINY85
(its going to be used in an RC verhicle)

Whenever you detect that the switch is "off", record the time. Once the switch is "on" you can use that as a reference for your timer, by subtracting it from the current time.

thanks i tried solving it with your pointers, but apparantly iam missing something, because its still not working.

// constants won't change. Used here to set a pin number:
const int ledPin =  0;// the number of the LED pin
const int ledPin1 =  1;// the number of the LED pin
const int buttonPin = 4;     // the number of the pushbutton pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
int ledState1 = LOW;             // ledState used to set the LED
int buttonState = 0;         // variable for reading the pushbutton status

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated
unsigned long previousMillis1 = 0;        // will store last time LED was updated
unsigned long buttonOnMillis;
unsigned long turnOnDelay = 500;

// constants won't change:
const long interval = 80;           // interval at which to blink (milliseconds)
const long interval1 = 350;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();
  unsigned long currentMillis1 = millis();

  // check if the pushbutton is pressed.
  if (buttonState == LOW) {
    buttonOnMillis = currentMillis;
        
    if (currentMillis - previousMillis >= interval) {
      //save this reading!
      previousMillis = currentMillis;

      //figure out if you should turn the LED on or off
      if (ledState == LOW) {
        ledState = HIGH;
      } else {
        ledState = LOW;
      }
      digitalWrite(ledPin, ledState);
    }
     if (buttonOnMillis - currentMillis1 >= turnOnDelay){
      if (currentMillis1 - previousMillis1 >= interval1) {
      //save this reading!
      previousMillis1 = currentMillis1;

        //figure out if you should turn the LED on or off
        if (ledState1 == LOW) {
          ledState1 = HIGH;
        } else {
        ledState1 = LOW;
        }
       digitalWrite(ledPin1, ledState1);
      }
     }
  }

  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin1, LOW);
  }
}

I can easily see that you didn't follow this advice:
'Whenever you detect that the switch is "off", record the time.'

Instead, you record the time when the switch is ON.

Perhaps that will help?

Hello Beertje87
Take a piece of paper and pen and show a timing diagram.
I´ve study your sketch and was complete derailed.
Have a nice day and enjoy coding in C++.

It's disappointing, and a bad sign, that you didn't pay attention to my observation in reply #5 about duplicate millis variables when you updated your code. That is a simple one. You need to get a lot of dross out of the way so you can see what's going on.

Also, I agree, it's not perfectly clear what timing you are after. So you need to explain in detail. Really, diagrams are better for that.