Switching a relay on and off using "millis()" without interfering with the runtime of the other piece of code

OK. I got tired of not being able to compile and test your code here and posting erroneous replies (there are a couple more bugs I missed...) so I made a minimally viable example of the relay timing I'm trying to relate , compiled and debugged it and it works here. You should be able to copy the relay() function into your code but you'll need to change the name of the relay pin from what I used.

In this example I used the built-in LED to mimic the LED so the sense of "on" and "off" are switched (i.e. HIGH on pinRelay turns the LED on but this turns your relay off...) so if you want to adapt it, you'll need to sort that stuff out too...


//"Turning on a relay 5 seconds after the analog voltage is read from the 
//analog pin, and closing it again 5 seconds after opening it
#define K_RELAY_DELAY_TIME  5000ul
#define K_RELAY_HOLD_TIME   5000ul
typedef enum
{
    RLY_IDLE=0,
    RLY_DELAY,
    RLY_HOLD
    
}e_statesRelay_t;

const uint8_t pinSensor = A0;        
const uint8_t pinRelay = LED_BUILTIN;

boolean bRelay = false;

void setup() 
{
    pinMode( pinSensor, INPUT );
    pinMode( pinRelay, OUTPUT );
    Serial.begin(9600);
    
}//setup

void loop() 
{
    chkVolts();   
    relay();    
    
}//loop

void chkVolts()  
{
    static uint32_t
        timeSample = 0ul;
    uint32_t
        timeNow = millis();

    if( (timeNow - timeSample) >= 100ul )
    {
        timeSample = timeNow;
        int sensorValue = analogRead( pinSensor );
        float voltage = (float)sensorValue * 5.0 / 1023.0;
        
        if( voltage > 2.0 ) 
        {
            Serial.print( "Sensor: " ); Serial.println( voltage, 2 );
            bRelay = true;  //...after the analog voltage is read from the analog pin...
            
        }//if
                    
    }//if
    
}//chkVolts

void relay( void ) 
{
    static uint8_t
        stateRelay = RLY_IDLE;
    static uint32_t
        timeRelay;
    uint32_t
        timeNow = millis();
        
    switch( stateRelay )
    {
        case    RLY_IDLE:
            //waiting for flag to indicate relay sequence
            if( bRelay == true )
            {
                Serial.println( "Relay delay" );                
                timeRelay = timeNow;
                stateRelay = RLY_DELAY;
                
            }//if
            
        break;

        case    RLY_DELAY:
            //delay before turning the relay on
            if( timeNow - timeRelay >= K_RELAY_DELAY_TIME )
            {
                Serial.println( "Relay on and hold" );                
                digitalWrite( pinRelay, HIGH );   //relay on 
                timeRelay = timeNow;
                stateRelay = RLY_HOLD;
                    
            }//if
            
        break;

        case    RLY_HOLD:
            //relay on-timing
            if( timeNow - timeRelay >= K_RELAY_HOLD_TIME )
            {
                Serial.println( "Relay off" );                
                digitalWrite( pinRelay, LOW );    
                bRelay = false;
                stateRelay = RLY_IDLE;
                    
            }//if
        
        break;
                
    }//switch

}//relay