Overlapping Outputs

Hello this is my code at the moment ( i will expand it to more buttons but at the moment its 2 different inputs and 2 different outputs.)

At the moment its press button 1 ---> light LED 1 ---> LED fades out.

and the same for button 2 but LED 2 lights.

I am trying to figure out a way so i dont have to wait for operation 1 to finish so i can press the next button.

So say button1 has been pushed and the light is fading out, i would like to press button 2 to jump on top of button1's remaining function

So when LED 1 is fading out LED2 can start on top of LED1 while letting LED 1 fade out.

This is my code

  const int buttonPin1 = 2;  
  const int buttonPin2 = 4;// the number of the pushbutton pin
  const int ledPin1 =  11;
  const int ledPin2 = 9;
  int buttonState1 = 0;
  int buttonState2 = 0;
  
  void setup() {
    // initialize the LED pin as an output:
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT); //nitialize the pushbutton pin as an input:
    pinMode(buttonPin1, INPUT);
    pinMode(buttonPin2, INPUT);
  }
  void loop()
  {
    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);
  
  
    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState1 == HIGH) {
  
      digitalWrite(ledPin1, HIGH);
  
      myDelay1();
      lightAndFade();
    }
    else{
      digitalWrite(ledPin1, LOW); 
    }
  
    if (buttonState2 == HIGH) {
  
      digitalWrite(ledPin2, HIGH);
      myDelay2();
      lightAndFade2();
    }
    else{
      digitalWrite(ledPin2, LOW); 
    }
  }
  
  void myDelay1()
  {
    delay(100);
  }
  void myDelay2()
  {
    delay(100);
  }
  void lightAndFade()
  {
    // fade in from min to max in increments of 5 points:
    for(int fadeValue = 255 ; fadeValue <= 255; fadeValue +=5) {
      // sets the value (range from 0 to 255):
      analogWrite(ledPin1, fadeValue);
     
      // wait for 30 milliseconds to see the dimming effect
      delay(0);
    }
    // fade out from max to min in increments of 5 points:
    for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
      // sets the value (range from 0 to 255):
      analogWrite(ledPin1, fadeValue);
     
      // wait for 30 milliseconds to see the dimming effect
      delay(100);  // Your working LED fading code
    }
  }
  void lightAndFade2()
  {
    // fade in from min to max in increments of 5 points:
    for(int fadeValue1 = 255 ; fadeValue1 <= 255; fadeValue1 +=5) {
      // sets the value (range from 0 to 255):
    
      analogWrite(ledPin2, fadeValue1); 
      // wait for 30 milliseconds to see the dimming effect
      delay(30);
    }
    // fade out from max to min in increments of 5 points:
    for(int fadeValue1 = 255 ; fadeValue1 >= 0; fadeValue1 -=5) {
      // sets the value (range from 0 to 255):
      analogWrite(ledPin2, fadeValue1);
      // wait for 30 milliseconds to see the dimming effect
      delay(100);  // Your working LED fading code
    }
  }

Would much appreciate it !!
Thanks,
Victor

With the way your trying to use delay you only need one subroutine to cover the delay on both buttons. Really you don't need to be using a subroutine for the delay. Just put the delay directly into the loop. Otherwise your making the code overly complicated. You also got too much going on with the fading. All you want is for it to fade out so there is no need to have it fade in first since your turning the LED full on before going to the fade routine. You also don't need to use the if() else.

So here is how i see it. I have changed the way its looking at the button. Currently your using a pull up resistor on the buttons like the examples show. The arduino has this built in. So connect the button without the resistor. Have it connected to the ground rather then the +5. Then in the setup, set the pin mode as input then after that do a digitalWrite to the button pins. Now the buttons will need to be LOW rather then HIGH when activated. Here is the modified code. It is tested and works. Just haven't figured out a way to get the code to allow the other button to be pushed and other fade to active in the middle of the first.

 const int buttonPin1 = 2;  
  const int buttonPin2 = 4;// the number of the pushbutton pin
  const int ledPin1 =  9;
  const int ledPin2 = 11;
  int buttonState1 = 0;
  int buttonState2 = 0;
  
  void setup() {
    // initialize the LED pin as an output:
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT); //nitialize the pushbutton pin as an input:
    pinMode(buttonPin1, INPUT);
    pinMode(buttonPin2, INPUT);
    digitalWrite(buttonPin1, HIGH);  // enable internal pullup; buttons start in high position; logic reversed 
    digitalWrite(buttonPin2, HIGH);  // no pullup resister needed on buttons. buttons tied to ground not +5v
    
  }
    void loop()
  {
    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);
  
  
    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState1 == LOW) {
  
      digitalWrite(ledPin1, HIGH);
  
     delay(100);
      lightAndFade();
    }
    
  
    if (buttonState2 == LOW) {
  
      digitalWrite(ledPin2, HIGH);
      delay(100);
      lightAndFade2();
    }
  }
  
  void lightAndFade()
  {
    // fade out from max to min in increments of 5 points:
    for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
      // sets the value (range from 0 to 255):
      analogWrite(ledPin1, fadeValue);
    
      // wait for 30 milliseconds to see the dimming effect
      delay(100);  // Your working LED fading code
    }
  }
  void lightAndFade2()
  {
    // fade out from max to min in increments of 5 points:
    for(int fadeValue1 = 255 ; fadeValue1 >= 0; fadeValue1 -=5) {
      // sets the value (range from 0 to 255):
      analogWrite(ledPin2, fadeValue1);
      // wait for 30 milliseconds to see the dimming effect
      delay(100);  // Your working LED fading code
    }
  }

Problem with getting this to do what your want is the method used to fade the LED. Once you start fading one nothing else will happen till the for() statement is complete. Unfortunately i don't think there is a good way for that to work.

You're going to have to learn how to write code without using delay. I don't use delay at all now, because my arduinos have to do several things and I can't afford it to be sat around waiting for a delay to time out, it has to be servicing other stuff.

The way I do it is to keep a time variable of type unsigned long and numerous loop variables (one for each action that needs to be done) of the same type. Time is set to millis(); each loop. Then I have 'if then' checking to see if the time is greater than the loop variable. If it is, it enters a subroutine that adds x to the loop variable (x is effectively the delay in milli seconds) and then does whatever is to be done at that time. It then returns to the main loop and carries on running. Each subroutine has its own loop variable to control when it is next done. You could have it polling for button presses in the main loop and then have the dimming done one step at a time in a subroutine (if it takes 10 steps to fade, its called 10 times, there needs to be a variable to keep the light level / track how far through the process it is.).

The only downside is that fades and blink gaps etc can have a bit of variance due to stuff being done in between times. Not much you can do about that...........

Digi Mike ! Your code works fine.

Pluggy: I cant seem to visialize how that code would be written. I am new to coding and arduino!

could you show me an example?

Heres pluggy's version of blink without delay.

    time=millis();

    if (time >= loop2){ 
    loop2 = time + 500;
    if (led2){ 
      digitalWrite(relay2, HIGH); 
      led2 = false; 
    }
    else
    { 
      digitalWrite(relay2, LOW); 
      led2 = true;
    };
  };