Need help with delay function

Start by doing some research on state machines. Basically the program can be in one of several states and only the code for that state is executed along with code that code that is executed unconditionally

A convenient way to program this is by using switch/case where each case is a state in the system. This probably sounds like gobbledegook to you ! I could write the code for your project for you, as could many other users here, but you will learn nothing.

Here is a small sketch demonstrating the use of a state machine. It runs continuously and switches between 2 states. You could run any code in either state but as written all it does is to print a message. You can put any non blocking code that you want into loop() such as reading an input and starting the state machine in any state that you want

unsigned long currentTime;
unsigned long startTime;
unsigned long period;

enum states
{
    actionA,
    actionB
};
byte currentState = actionA;

void setup()
{
    Serial.begin(115200);
    period = 5000;
    Serial.println("starting actionA");
    startTime = millis();
}

void loop()
{
    currentTime = millis();
    switch (currentState)
    {
        case actionA:
            if (currentTime - startTime >= period)  //if the period ended ?
            {
                currentState = actionB;   //switch states
                startTime = currentTime;  //action started now
                period = 2000;            //set the period for the action
                Serial.println("switching to actionB");
            }
            break;

        case actionB:
            if (currentTime - startTime >= period)  //if the period ended ?
            {
                currentState = actionA;   //switch states
                startTime = currentTime;  //action started now
                period = 2000;            //set the period for the action
                Serial.println("switching to actionA");
            }
            break;
    }
}

No doubt you will have questions so feel free to ask