Multi Tasking with out delay

I am trying to turn on one relays for x amount of time and turn it off for z amount of time (and the second relay) and wait for y amount of time and do it again, but not use any delay. since I have other inputs that need be checked while this is happening.

I know you can use Millis() and use current vs previous time but I couldn't figure out how to put everything together.

I already found this but the problem i will have push buttons which increase or decrease the delays

The blink without delay example. Did you studied?
It is simple, just replace the LED with relay.

Here is a very simple BWDexample (using millis) , you should be able to get some ideas from it:

//Simple BWD BlinkWithoutDelay examples

//Timer variables used
unsigned long currentMillis;
unsigned long pin13Millis;
unsigned long pin12Millis;
unsigned long SwitchMillis;

//if these are not changed in the sketch, they can be const
unsigned long debounceMillis = 50UL;  //50ms
unsigned long ledOnTime      = 500UL; //500ms seconds

byte lastSwitchState = HIGH;
byte buttonState     = HIGH;

//enable/disable flags
boolean flag13 = true;
boolean flag12 = false;

const byte Switch = 2; //pushed = LOW

//**********************************************************************

void setup()
{
  Serial.begin(9600);
  
  digitalWrite(13,LOW);
  pinMode(13, OUTPUT);
   
  digitalWrite(12,LOW);
  pinMode(12, OUTPUT);
  
  pinMode(Switch, INPUT_PULLUP); //pushed = LOW

} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<

void loop()
{
  //save the current time
  currentMillis = millis();

  //************************************* 
  //Heartbeat LED
  //Toggle LED on and off, helps show if there is blocking code
  if (flag13 == true && currentMillis - pin13Millis >= ledOnTime)
  {
    pin13Millis = millis();            //re-initialize Timer
    digitalWrite(13,!digitalRead(13)); //toggle LED condition
  }
  
  //************************************* 
  if (flag12 == true && currentMillis - pin12Millis >= 5*1000UL)
  {
    //Turn off pin 12
    digitalWrite(12,LOW); //Turn off LED
    flag12 = false;       //disable timing
  }

  //************************************* 
  //is it time to check the switches?
  if (currentMillis - SwitchMillis >= debounceMillis)
  {
    //code here runs every debounceMillis ms
    SwitchMillis = millis(); //re-initilize Timer
    //go and check the switches
    checkSwitches();    
  } 

  //*********************************
  //put other non-blocking stuff here
  //*********************************

} //  >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<


//======================================================================
//                      F U N C T I O N S
//======================================================================


//****************** c h e c k S w i t c h e s ( ) *********************
//switches are checked every debounceValue milli seconds 
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void checkSwitches()  
{
  //re-usable for all the switches  
  boolean thisState;    

  //check if this switch has changed state
  thisState = digitalRead(Switch);
  if (thisState != lastSwitchState)
  {  
    //update the switch state
    lastSwitchState = thisState;  

    //this switch position has changed so do some stuff
    //"LOW condition code"
    //has switch gone from HIGH to LOW?
    if(thisState == LOW)                          
    {
      //Do some LOW switch stuff here  
      flag12 = true;          //allow timing
      digitalWrite(12, HIGH); //turn on LED
      pin12Millis = millis(); //initialize Timer
    }

  } //END of Switch code

  //*****************************************  
  //similar code for other switches goes here 
  //*****************************************  

} //END of checkSwitches()

//**********************************************************************

//======================================================================
//                      E N D  O F  C O D E
//======================================================================

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing. It may help with understanding the technique.

...R

thank you for help

I already found this but the problem i will have push buttons which increase or decrease the delays

So then you need to look at button debouncing examples.

Also look for state machine examples as identifying the states (button pushed, button not pushed) will help you work out when to apply the increase or decrease.

A variable delay has to be stored in a variable, and is used like this:

if (Button1pressed) Delay = Delay+5; //increase delay by 5ms
if (millis()-startTime >= Delay) ...

Below is my code but I can not make it to stay in wait time for x time

#define Relay_LED1 6
#define Relay_LED2 7
#define Wait 10
int Relay_ON_Interval;
int Relay_OFF_Interval;
int Wait_Interval;
int Relay_State = 0;
long unsigned previous_Millis = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(Relay_LED1, OUTPUT);
pinMode(Relay_LED2, OUTPUT);
pinMode(Wait, OUTPUT);

}

void loop() {

Relay_ON_Interval = 1000;
Relay_OFF_Interval = 1000;
Wait_Interval = 2000;

updateRelay_State(Wait_Interval, Relay_ON_Interval, Relay_OFF_Interval);
}

void updateRelay_State(int wait, int on, int off)
{
Wait_Interval = wait;
Relay_ON_Interval = on;
Relay_OFF_Interval = off;

long unsigned currentMillis = millis();

Serial.print(Relay_State);
Serial.print(",");
Serial.println(currentMillis - previous_Millis);

switch ( Relay_State )
{

case 0:

if (currentMillis - previous_Millis >= Wait_Interval)
{

Serial.print(Relay_State);
Serial.print(",");
Serial.print("wait");
Serial.print(",");
Relay_State = 1;
digitalWrite(Relay_LED1, HIGH);
digitalWrite(Relay_LED2, HIGH);
digitalWrite(Wait, LOW);
Serial.print(currentMillis);
Serial.print(",");
Serial.print(previous_Millis);
Serial.print(",");
Serial.println(Relay_State);
previous_Millis = currentMillis;

}

case 1:
if (currentMillis - previous_Millis >= Relay_OFF_Interval)
{
Serial.print(Relay_State);
Serial.print(",");
Serial.print("Relay 1");
Serial.print(",");
Relay_State = 2;
digitalWrite(Relay_LED1, HIGH);
digitalWrite(Relay_LED2, LOW);
digitalWrite(Wait, HIGH);
Serial.print(currentMillis);
Serial.print(",");
Serial.print(previous_Millis);
Serial.print(",");
Serial.println(Relay_State);
previous_Millis = currentMillis;
}
case 2:
if (currentMillis - previous_Millis >= Relay_ON_Interval)
{
Serial.print(Relay_State);
Serial.print(",");
Serial.print("Relay 2");
Serial.print(",");
Relay_State = 0;
digitalWrite(Relay_LED2, HIGH);
digitalWrite(Relay_LED1, LOW);
digitalWrite(Wait, HIGH);
Serial.print(currentMillis);
Serial.print(",");
Serial.print(previous_Millis);
Serial.print(",");
Serial.println(Relay_State);
previous_Millis = currentMillis;
}

}

}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

I wonder if you have your logic upside-down. For example the word "wait" will only appear when your Wait_interval is finished.

If I have got this wrong please explain what you want to happen in more detail. Write it down in English with each step of the process on a separate line.

...R

Each case needs a 'break' :wink: