I need help about jumping from delay to multitasking

hello everyone
i need help to start with advanced step programming level
i search about multitasking i find the millis() function
but is not help me to understand carefully how i can do anything with him for example i have that code with delay function but i don't know how i can do that instruction with millis() function
the several instruction needed its that

                 digitalWrite(led1 ,blink); 
                 digitalWrite(buzz ,HIGH); 
                 delay(500);
                 digitalWrite(buzz , LOW);
                 digitalWrite(motor,HIGH);
                 digitalWrite(fan  ,HIGH); 
                 digitalWrite(vane2,HIGH);
                 delay(12000);
                 digitalWrite(motor,LOW);
                 digitalWrite(fan  ,LOW); 
                 digitalWrite(vane2,LOW);
                 delay(30000);
                 digitalWrite(vane1,HIGH);
                 delay(20000);
                 digitalWrite(vane1,LOW);
                 digitalWrite(led1 ,LOW);
                 digitalWrite(buzz ,HIGH);
                 delay(500);
                 digitalWrite(buzz  ,LOW); 

now i have a problem all instruction run on same time not like my needed
i know the delay block the programme and procs withing for time finish for run another instruction
i need anyone translate my code to millis() function for understand carefully millis() function and how i can do anything with this millis function because I'm very confused about that because I'm not find any tutorial talking about instruction like that

Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

2 Likes

Hello farai
Take some time, study the IPO model and take a piece of paper plus a pencil and design a program structure. A FSM is usefull. Identify modules could to be coded and tested separetly. All modules merged together after sucessfull testing to the needed project.
It is highly recommented to take a view into some powerful C++ instructions like STRUCT, ENUM, ARRAY, CASE/SWITCH,UDT, RANGE-BASED-FOR-LOOP, etc, too.
Have a nice day and enjoy coding in C++.

Sounds like you are aware of making TIMERs using millis( ).

What part of the process are you having trouble understanding ?

first i tank you for quick reply
i read all that and i test to write my sketch with millis function and have a problem about get withing same time for run another instruction
my problem about millis function its all instruction run simultaneously not withing same time for run and I'm i lose my sketch because I'm not save it and i turn off laptop
can you translate my code to millis function for carefully understanding ???
how i can tho withing time without delay ???
all example on internet don't help me all creator tutorial talking about blinking not serial instruction
i can do blinking without delay but for do instruction => withing time => instruction without delay function is not available on all tutorial

i have a trouble understanding about how to do
instruction => withing time => instruction
without delay function !

i will read your link
tank you very match

Do you know what a State Machine is ?

i don't know exactly
i thank something like Z80 parallel output and input with auto multitasking

Show us the rest of the sketch that you now have now ?

that my previous sketch

#include <ezButton.h>
#define LOOP_STATE_STOPPED 0
#define LOOP_STATE_STARTED 1
ezButton button(2);  // create ezButton object that attach to pin 2;
ezButton button1(3); // create ezButton object that attach to pin 3;
ezButton button2(4); // create ezButton object that attach to pin 4;
int loopState = LOOP_STATE_STOPPED;
#define motor 19  // motor attached in relay attached to pin A5 
#define vane1 18  // van 1 attached in relay attached to pin A4
#define vane2 17  // van 2 attached in relay attached to pin A3
#define vane3 16  // van 3 attached in relay attached to pin A2
#define vane4 15  // van 4 attached in relay attached to pin A1   // all output (A0,A1,A2,A3,A4,A5) go to ULN2003 to relays 
#define fan   14  // fan attached in relay attached to pin A0
//#define bm1    2  // push_button 1 attached betwen pin 2 of arduino and earth 
//#define bm2    3  // push_button 2 attached betwen pin 3 of arduino and earth
//#define bm3    4  // push_button 3 attached betwen pin 4 of arduino and earth
#define led1   5  // led 1 attached betwen pin 5 of arduino and earth series with 470 ohm resistor 
#define led2   6  // led 2 attached betwen pin 6 of arduino and earth series with 470 ohm resistor 
#define led3   7  // led 3 attached betwen pin 7 of arduino and earth series with 470 ohm resistor 
                  // the leds is combined in button and i need to use them bliking the time of runing one of my program
#define buzz   8  // buzzer attached betwen pin 5 of arduino and earth (in real i run with bc547 )
int i;            // variable for state of incriment and decriment for switching betwin program to other 
int a = 1;            // variable for store value of button 1
int b = 1;            // variable for store value of button 2
int c = 1;            // variable for store value of button 3
volatile byte state = LOW; // variable for store value for bliking led the time of runing loop
void blink() // loop bliking led 
           {
             state =! state;
           }
void pression()   // this loop is an program for the first button 1 to do when is pressed for first time 
                { 
                 digitalWrite(led1 ,blink); // i dont know what is the error her because is not bliking when program run and compiling not let me thath wrong
                 digitalWrite(buzz ,HIGH); 
                 delay(500);
                 digitalWrite(buzz , LOW);
                 digitalWrite(motor,HIGH);
                 digitalWrite(fan  ,HIGH); 
                 digitalWrite(vane2,HIGH);
                 delay(12000);
                 digitalWrite(motor,LOW);
                 digitalWrite(fan  ,LOW); 
                 digitalWrite(vane2,LOW);
                 delay(30000);
                 digitalWrite(vane1,HIGH);
                 delay(20000);
                 digitalWrite(vane1,LOW);
                 digitalWrite(led1 ,LOW);
                 digitalWrite(buzz ,HIGH);
                 delay(500);
                 digitalWrite(buzz  ,LOW); 
                 }
void vaccum()     // this loop is an program for the first button 2 to do when is pressed for first time 
             {
              digitalWrite(led2 ,blink);  // i dont know what is the error her because is not bliking when program run and compiling not let me thath wrong 
              digitalWrite(buzz ,HIGH);
              delay(500);
              digitalWrite(buzz,  LOW);
              digitalWrite(motor,HIGH);
              digitalWrite(fan  ,HIGH); 
              digitalWrite(vane1,HIGH);
              digitalWrite(vane3,HIGH);
              delay(35000);
              digitalWrite(vane3,LOW);
              delay(25000);
              digitalWrite(vane3,HIGH);
              delay(15000);
              digitalWrite(vane4,HIGH);
              delay(10000);
              digitalWrite(vane4,LOW);
              digitalWrite(vane2,HIGH);
              delay(10000);
              digitalWrite(motor,LOW);
              digitalWrite(fan  ,LOW); 
              digitalWrite(vane1,LOW);
              digitalWrite(vane3,LOW);
              delay(90000);
              digitalWrite(led2 ,LOW);
              digitalWrite(vane2,LOW);
              digitalWrite(buzz ,HIGH);
              delay(500);
              digitalWrite(buzz  ,LOW);
              }
void pressionp()  // this loop is an program for the first button 3 to do when is pressed for first time 
                {
                 digitalWrite(led3 ,blink);  // i dont know what is the error her because is not bliking when program run and compiling not let me thath wrong
                 digitalWrite(buzz ,HIGH);
                 delay(500);
                 digitalWrite(buzz , LOW);
                 digitalWrite(motor,HIGH);
                 digitalWrite(fan  ,HIGH); 
                 digitalWrite(vane2,HIGH);
                 delay(12000);
                 digitalWrite(motor ,LOW);
                 digitalWrite(fan   ,LOW); 
                 digitalWrite(vane2 ,LOW);
                 delay(24000);
                 digitalWrite(vane1,HIGH);
                 delay(20000);
                 digitalWrite(vane1 ,LOW);
                 digitalWrite(led3  ,LOW);
                 digitalWrite(buzz ,HIGH);
                 delay(500);
                 digitalWrite(buzz  ,LOW); 
               }
   void off()    // this loop is an program for the all button 1,2 and 3 to do when is pressed for second time 
             {                      
               digitalWrite(motor,LOW);
               digitalWrite(vane1,HIGH);
               digitalWrite(vane2,HIGH);
               digitalWrite(vane3,HIGH);
               digitalWrite(vane4,HIGH);
               digitalWrite(fan  ,LOW);
               digitalWrite(led1 ,LOW);
               digitalWrite(led2 ,LOW);
               digitalWrite(led3 ,LOW);
               digitalWrite(buzz ,LOW);
               delay(2000);
               digitalWrite(motor,LOW);
               digitalWrite(vane1,LOW);
               digitalWrite(vane2,LOW);
               digitalWrite(vane3,LOW);
               digitalWrite(vane4,LOW);
               digitalWrite(fan  ,LOW);
               digitalWrite(led1 ,LOW);
               digitalWrite(led2 ,LOW);
               digitalWrite(led3 ,LOW);
               digitalWrite(buzz ,LOW); 
                         
              }  
void setup() 
           {
             button.setDebounceTime(50);  // set debounce time to 50 milliseconds
             button1.setDebounceTime(50); // set debounce time to 50 milliseconds
             button2.setDebounceTime(50); // set debounce time to 50 milliseconds
//             pinMode(bm1  ,INPUT_PULLUP); 
//             pinMode(bm2  ,INPUT_PULLUP);
//             pinMode(bm3  ,INPUT_PULLUP);
             pinMode(motor      ,OUTPUT);
             pinMode(vane1      ,OUTPUT);
             pinMode(vane2      ,OUTPUT);
             pinMode(vane3      ,OUTPUT);
             pinMode(vane4      ,OUTPUT);
             pinMode(fan        ,OUTPUT);
             pinMode(led1       ,OUTPUT);
             pinMode(led2       ,OUTPUT);
             pinMode(led3       ,OUTPUT);
             pinMode(buzz       ,OUTPUT);
          }
void loop() 
           {
             button.loop(); // MUST call the loop() function first

             if (button.isPressed()) 
                                   {
                                     if (loopState == LOOP_STATE_STOPPED)
                                     loopState = LOOP_STATE_STARTED;
                                     else // if(loopState == LOOP_STATE_STARTED)
                                     loopState = LOOP_STATE_STOPPED;
                                     off();
                                   }
                                   if (loopState == LOOP_STATE_STARTED) 
                                                                      {
                                                                        pression();
                                                                       }
             if (button1.isPressed()) 
                                   {
                                     if (loopState == LOOP_STATE_STOPPED)
                                     loopState = LOOP_STATE_STARTED;
                                     else // if(loopState == LOOP_STATE_STARTED)
                                     loopState = LOOP_STATE_STOPPED;
                                     off();
                                   }
                                   if (loopState == LOOP_STATE_STARTED) 
                                                                      {
                                                                        vaccum();
             if (button2.isPressed()) 
                                   {
                                     if (loopState == LOOP_STATE_STOPPED)
                                     loopState = LOOP_STATE_STARTED;
                                     else // if(loopState == LOOP_STATE_STARTED)
                                     loopState = LOOP_STATE_STOPPED;
                                     off();
                                   }
                                   if (loopState == LOOP_STATE_STARTED) 
                                                                      {
                                                                        pressionp();
                                                                       }                                                          }
            }

that my rest now
my sketch with millis function is loses because my laptop is turn off without saving my data

Like this?

The way to think about blink without delay in this context is that there are two states that swap back and forth serially. There is a binary, boolean*** state variable ledState that is updated periodically (every interval milliseconds) to the opposite state.

In your partial code it looks like you have 6 states that transition at variable times:

                 digitalWrite(led1 ,blink); 
                 digitalWrite(buzz ,HIGH); 
                 delay(500);

                 digitalWrite(buzz , LOW);
                 digitalWrite(motor,HIGH);
                 digitalWrite(fan  ,HIGH); 
                 digitalWrite(vane2,HIGH);
                 delay(12000);

                 digitalWrite(motor,LOW);
                 digitalWrite(fan  ,LOW); 
                 digitalWrite(vane2,LOW);
                 delay(30000);

                 digitalWrite(vane1,HIGH);
                 delay(20000);

                 digitalWrite(vane1,LOW);
                 digitalWrite(led1 ,LOW);
                 digitalWrite(buzz ,HIGH);
                 delay(500);

                 digitalWrite(buzz  ,LOW);

The memory for which of the 6 states are active is the state variable, and would correspond to BWOD's int ledState = LOW. You would need a richer interval than BWOD's const long interval = 1000;, and would need to update that along with the other outputs.

in contrast to BWOD's state and output transition logic in

   // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);

...you need a more complicated structure using richer if( faraiState == 0){ or switch(faraiState){ case 0: .... like

    switch(faraiState){ 
        case 0: 
                 digitalWrite(led1 ,blink); 
                 digitalWrite(buzz ,HIGH); 
                 interval = 500;  //delay(500);
                 faraiState = 1;
                 break;
        ...

Maybe start by toggling between two of your tasks squeezed into BlinkWithoutDelay and post some code that compiles.

  • *** Oops -- I thought BWOD's state variable was binary, but it is a perfectly extensible int--genius.
1 Like

can variable like interval in your example work like delay without blocking program ???
its work only on switch case function ???
i find that interesting

yes is like that example

Think of a restaurant, the waiter is the processor and the customers are tasks that need to be done at various time under various conditions. The states are things like:

  • Waiting for a table
  • Being seated
  • Reading menu
  • Placing order
  • Waiting for food
  • Food being placed on table
  • Eating food
  • Empty plates being taken away
  • Getting bill
  • Paying bill
  • Leaving restaurant

The waiter attends to each of these as needed, for each table and each diner as needed, then waits (no pun intended) and watches for other diners needing help.

A waiter does not stay in the kitchen while the food it cooked, they go back to the dining area to attend to other customers. They get the food from the kitchen when it's ready, they don't stand there waiting for it to be cooked.

A restaurant is a state machine, as are many things you do in life. In a computer program the states are much shorter and simpler and happen a lot faster, but the principal is exactly the same.

1 Like

To be tested.

This should be more than enough to get your started:

//  YY/MM/DD  Version    Comments
//  22/03/10  1.00       Running sketch

//output pins, change as needed
const byte led1          = 2;
const byte buzz          = 3;
const byte motor         = 4;
const byte fan           = 5;
const byte vane2         = 6;
const byte vane1         = 7;
const byte heartbeatLED  = 13;

volatile byte state      = LOW;

//our output pins
const byte outputPins[]  = {led1, buzz, motor, fan, vane2, vane1, heartbeatLED};

//the states defined in our machine
//pick better names ;-)
enum States {StartUp, State_1, State_2, State_3, State_4, State_5, State_6};

States mState = StartUp;

//timing stuff
unsigned long commonMillis;
unsigned long interval;
unsigned long heartbeatMillis;
unsigned long stateMachineMillis;

//***********************************************************************************
void setup()
{
  //set tp output
  for (byte x = 0; x < sizeof(outputPins); x++)
  {
    pinMode(outputPins[x], OUTPUT);
    digitalWrite(outputPins[x], LOW);
  }

} //END of   setup()


//***********************************************************************************
void loop()
{
  //************************************
  //is it time to toggle the heartbeat LED ?
  if (millis() - heartbeatMillis > 500)
  {
    //restart this TIMER
    heartbeatMillis = millis();

    //toggle the LED
    digitalWrite(heartbeatLED , !digitalRead(heartbeatLED));

  }

  //************************************
  //is it time to check the State Machine ?
  if (millis() - stateMachineMillis > 10)
  {
    //restart this TIMER
    stateMachineMillis = millis();

    checkStateMachine();

  }

  //************************************
  //other none blocking code goes here
  //************************************

} // END of   loop()


//***********************************************************************************
void checkStateMachine()
{
  switch (mState)
  {
    //*****************
    case StartUp:
      {
        //do nothing

        //next state
        mState = State_1;

      }

      break;

    //*****************
    case State_1:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(led1 , blink());
          digitalWrite(buzz , HIGH);

          //next state
          mState = State_2;

          //adjust the delay interval
          interval = 500;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_2:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(buzz , LOW);
          digitalWrite(motor, HIGH);
          digitalWrite(fan  , HIGH);
          digitalWrite(vane2, HIGH);

          //next state
          mState = State_3;

          //adjust the delay interval
          interval = 12000;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_3:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(motor, LOW);
          digitalWrite(fan  , LOW);
          digitalWrite(vane2, LOW);

          //next state
          mState = State_4;

          //adjust the delay interval
          interval = 30000;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_4:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(vane1, HIGH);

          //next state
          mState = State_5;

          //adjust the delay interval
          interval = 20000;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_5:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(vane1, LOW);
          digitalWrite(led1 , LOW);
          digitalWrite(buzz , HIGH);

          //next state
          mState = State_6;

          //adjust the delay interval
          interval = 500;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_6:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(buzz  , LOW);

          //next state
          //mState = StartUp;

          //adjust the delay interval
          //interval = 500;

          //restart the TIMER
          //commonMillis = millis();
        }
      }

      break;

  } //END of   switch . . . case

} //END of   checkStateMachine()


//***********************************************************************************
bool checkCommonTimer()
{
  //has the common TIMER expired ?
  if (millis() - commonMillis >= interval)
  {
    //TIMER has expired
    return true;
  }

  //TIMER is still running
  return false;

} //END of   checkCommonTimer()


//***********************************************************************************
byte blink() 
{
  state = ! state;

  return state;

}
1 Like

Yes. See this demo of BlinkWithoutDelay using Switch and variable intervals:

Anything you do with switch/case can also be done with a chain of nested if-else statements, but switch/case can be cleaner as the structure gets larger.

1 Like

i tested its good idea to do
i will test more function like stopping loop and run another like cancelling loop program because i need it
and something like block every input action except cancelling button it same button start
that is very interesting and complicated at same time but i don't know is it work on with switch function but i will do
tank you very much and Allah will bless you and give you the glory

Added some comments.


//***********************************************************************************
//  YY/MM/DD  Version      Comments
//  22/03/10  1.00         Running sketch
//  22/03/10  1.01         Added some comments
//

//************************************
#define LEDon              HIGH
#define LEDoff             LOW

#define BUZZon             HIGH
#define BUZZoff            LOW

//etc.

//************************************
//output pins, change as needed
const byte led1          = 2;
const byte buzz          = 3;
const byte motor         = 4;
const byte fan           = 5;
const byte vane2         = 6;
const byte vane1         = 7;
const byte heartbeatLED  = 13;

//our output pins
const byte outputPins[]  = {led1, buzz, motor, fan, vane2, vane1, heartbeatLED};

//************************************
//the states defined in our machine
//pick better names ;-)
enum States {StartUp, State_1, State_2, State_3, State_4, State_5, State_6};

States mState            = StartUp;

//************************************
//timing stuff
unsigned long commonMillis;
unsigned long interval;
unsigned long heartbeatMillis;
unsigned long stateMachineMillis;

//***********************************************************************************
void setup()
{
  //set to output
  for (byte x = 0; x < sizeof(outputPins); x++)
  {
    pinMode(outputPins[x], OUTPUT);
    digitalWrite(outputPins[x], LOW);
  }

} //END of   setup()


//***********************************************************************************
void loop()
{
  //************************************
  //is it time to toggle the heartbeat LED ?
  if (millis() - heartbeatMillis > 500)
  {
    //restart this TIMER
    heartbeatMillis = millis();

    //toggle the LED
    digitalWrite(heartbeatLED , !digitalRead(heartbeatLED));

  }

  //************************************
  //is it time to check the State Machine ?
  if (millis() - stateMachineMillis > 10)
  {
    //restart this TIMER
    stateMachineMillis = millis();

    checkStateMachine();

  }

  //************************************
  //other none blocking code goes here
  //************************************

} // END of   loop()


//***********************************************************************************
void checkStateMachine()
{
  switch (mState)
  {
    //*****************
    case StartUp:
      {
        //do nothing

        //next state
        mState = State_1;

      }

      break;

    //*****************
    case State_1:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(led1 , LEDon);
          digitalWrite(buzz , HIGH);

          //next state
          mState = State_2;

          //adjust the delay interval
          interval = 500;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_2:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(buzz , LOW);
          digitalWrite(motor, HIGH);
          digitalWrite(fan  , HIGH);
          digitalWrite(vane2, HIGH);

          //next state
          mState = State_3;

          //adjust the delay interval
          interval = 12000;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_3:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(motor, LOW);
          digitalWrite(fan  , LOW);
          digitalWrite(vane2, LOW);

          //next state
          mState = State_4;

          //adjust the delay interval
          interval = 30000;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_4:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(vane1, HIGH);

          //next state
          mState = State_5;

          //adjust the delay interval
          interval = 20000;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_5:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(vane1, LOW);
          digitalWrite(led1 , LEDoff);
          digitalWrite(buzz , HIGH);

          //next state
          mState = State_6;

          //adjust the delay interval
          interval = 500;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_6:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(buzz  , LOW);

          //next state
          //mState = StartUp;

          //adjust the delay interval
          //interval = 500;

          //restart the TIMER
          //commonMillis = millis();
        }
      }

      break;

  } //END of   switch . . . case

} //END of   checkStateMachine()


//***********************************************************************************
bool checkCommonTimer()
{
  //has the common TIMER expired ?
  if (millis() - commonMillis >= interval)
  {
    //TIMER has expired
    return true;
  }

  //TIMER is still running
  return false;

} //END of   checkCommonTimer()


2 Likes

i have an problem when i add button the program is not run properly like before is need another click again for go to another step
the edit is that :slightly_frowning_face: :

//  YY/MM/DD  Version    Comments
//  22/03/10  1.00       Running sketch

//output pins, change as needed
#define bm1 2
const byte led1          = 14;
const byte buzz          = 8;
const byte motor         = 19;
const byte fan           = 14;
const byte vane2         = 17;
const byte vane1         = 18;
const byte heartbeatLED  = 5;

volatile byte state      = LOW;

//our output pins
const byte outputPins[]  = {led1, buzz, motor, fan, vane2, vane1, heartbeatLED};

//the states defined in our machine
//pick better names ;-)
enum States {StartUp, State_1, State_2, State_3, State_4, State_5, State_6};

States mState = StartUp;

//timing stuff
unsigned long commonMillis;
unsigned long interval;
unsigned long heartbeatMillis;
unsigned long stateMachineMillis;

//***********************************************************************************
void setup()
{
  //set tp output
  for (byte x = 0; x < sizeof(outputPins); x++)
  {
    pinMode(outputPins[x], OUTPUT);
    digitalWrite(outputPins[x], LOW);
    pinMode(bm1,INPUT_PULLUP);
  }

} //END of   setup()


//***********************************************************************************
void loop()
{
  if (digitalRead(bm1) == LOW)      // when the button is pressed lunch void pression()
                           {
                            pression();
                           }


  //************************************
  //other none blocking code goes here
  //************************************

} // END of   loop()

void pression() // i add your togle led and state machine in loop 
              {
                //************************************
  //is it time to toggle the heartbeat LED ?
  if (millis() - heartbeatMillis > 500)
                                      {
                                        //restart this TIMER
                                        heartbeatMillis = millis();
                                    
                                        //toggle the LED
                                        digitalWrite(heartbeatLED , !digitalRead(heartbeatLED));
                                  }
//************************************
//is it time to check the State Machine ?
  if (millis() - stateMachineMillis > 10)
                                        {//restart this TIMER
                                          stateMachineMillis = millis();
                                          checkStateMachine();
                                          }
              }
//***********************************************************************************
void checkStateMachine()
{
  switch (mState)
  {
    //*****************
    case StartUp:
      {
        //do nothing

        //next state
        mState = State_1;

      }

      break;

    //*****************
    case State_1:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(led1 , blink());
          digitalWrite(buzz , HIGH);

          //next state
          mState = State_2;

          //adjust the delay interval
          interval = 500;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_2:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(buzz , LOW);
          digitalWrite(motor, HIGH);
          digitalWrite(fan  , HIGH);
          digitalWrite(vane2, HIGH);

          //next state
          mState = State_3;

          //adjust the delay interval
          interval = 12000;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_3:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(motor, LOW);
          digitalWrite(fan  , LOW);
          digitalWrite(vane2, LOW);

          //next state
          mState = State_4;

          //adjust the delay interval
          interval = 30000;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_4:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(vane1, HIGH);

          //next state
          mState = State_5;

          //adjust the delay interval
          interval = 20000;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_5:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(vane1, LOW);
          digitalWrite(led1 , LOW);
          digitalWrite(buzz , HIGH);

          //next state
          mState = State_6;

          //adjust the delay interval
          interval = 500;

          //restart the TIMER
          commonMillis = millis();
        }
      }

      break;

    //*****************
    case State_6:
      {
        //TIMER expired ?
        if (checkCommonTimer())
        {
          digitalWrite(buzz  , LOW);
          digitalWrite(led1  , LOW);

          //next state
          //mState = StartUp;

          //adjust the delay interval
          //interval = 500;

          //restart the TIMER
          //commonMillis = millis();
        }
      }

      break;

  } //END of   switch . . . case

} //END of   checkStateMachine()


//***********************************************************************************
bool checkCommonTimer()
{
  //has the common TIMER expired ?
  if (millis() - commonMillis >= interval)
  {
    //TIMER has expired
    return true;
  }

  //TIMER is still running
  return false;

} //END of   checkCommonTimer()


//***********************************************************************************
byte blink() 
{
  state = ! state;

  return state;

}

and the led not blinking during execution
i don't know the problem how to solve it because its interrupting by something in the program but I'm not add something weird

What do you want to do when bm1 is LOW ?