not using delay

Hello Everyone.

I have looked at a number of tutorials for millis() and avoiding the cursed delay() function. I just can't figure how to apply it correctly to my code: Any suggestions would be so appreciated!

if (PotDiff > 100 && PotDiff <= 200) {
    digitalWrite(RELAYPIN3, LOW);
    delay(30);
    digitalWrite(RELAYPIN3, HIGH);
    delay(30);
  }

  else if (PotDiff > 50 && PotDiff <= 100) {;
    digitalWrite(RELAYPIN4, LOW);
    delay(30);
    digitalWrite(RELAYPIN4, HIGH);
    delay(30);

thank you Delta...that is a very good and helpful explanation. It its me started in the right direction. I would post my code but its way, way too long. :slight_smile:

OP, see if you can muddle through this. It's not as complicated as it looks. The complex appearance comes from me using arrays to store relay status flags and timers rather than having individual variables.

This compiles (not tested.) It's designed to illustrate independent timing of up to 4 relays using millis() instead of delay.

HTH.

#define NUM_RELAYS  4
//
//#define RLY_1   0b00000001
//#define RLY_2   0b00000100
//#define RLY_3   0b00010000
//#define RLY_4   0b01000000
//
#define RLY_1_IDX       0
#define RLY_2_IDX       1
#define RLY_3_IDX       2
#define RLY_4_IDX       3

//these masks are used to test whether each relay
//is timing or not
//two bits are used for each fan
//relay 1 uses bit 0 (on timing flag) and bit 1 (off timing flag)
//relay 2 uses 2 and 3
//relay 3 uses 4 and 5
//relay 4 uses 6 and 7
//if you have or need to add more relays, you can move
//up to ints which give 8 relays' worth of flag bits... 
byte
    OnMasks[NUM_RELAYS] =
    {
        0b00000001,
        0b00000100,
        0b00010000,
        0b01000000

    };//

byte
    OffMasks[NUM_RELAYS] =
    {
        0b00000010,
        0b00001000,
        0b00100000,
        0b10000000

    };//

byte
    ActiveRelays;
unsigned long
    timeRelays[NUM_RELAYS];
int
    grRlyPins[NUM_RELAYS] = { 6, 7, 8, 9 };

void setup() 
{
  // put your setup code here, to run once:
    ActiveRelays = 0x00;
    for( int i=0; i<NUM_RELAYS; i++ )
    {
        timeRelays[i] = 0;
        pinMode( grRlyPins[i], OUTPUT );
        digitalWrite( grRlyPins[i],HIGH );
        
    }//if
    
}//setup

void loop() 
{
    CheckPots();
    TimeRelays();
    
}//loop

void TimeRelays( void )
{
    int
        i;
        
    //go through all the relays we have
    for( i=0; i<NUM_RELAYS; i++ )
    {
        //is the relay 'on' timer flag set?
        if( ActiveRelays & OnMasks[i] )
        {
            //yes, check to see if it's time to turn it off
            //a difference between the millis() when we turned
            //it on and the millis() now of 30mS or greater
            //will...
            if( (millis() - timeRelays[i]) >= 30 )
            {
                //trigger us to turn the relay off
                digitalWrite( grRlyPins[i], HIGH );
                //we have to start a few timer for the 30mS
                //off period
                timeRelays[i] = millis();
                //we can clear the 'on' timer flag
                ActiveRelays &= ~OnMasks[i];
                //and now set the 'off' timer flag
                ActiveRelays |= OffMasks[i];
                
            }//if  
        
        }//if

        //very similar to above, except here we're timing
        //the relay off time
        if( ActiveRelays & OffMasks[i] )
        {
            //this relay is being timed; time expired?
            if( (millis() - timeRelays[i]) >= 30 )
            {
                //yes, clean the off-timer flag. Relay can now
                //be switched on again
                ActiveRelays &= ~OffMasks[i];
                
            }//if
              
        }//if
        
    }//for
    
}//TimeRelays

void CheckPots()
{
    //need this here because I don't have your whole source
    int
        PotDiff;
        
    if (PotDiff > 100 && PotDiff <= 200) 
    {
        //if we're timing a given relay now, don't muck with it
        if( ActiveRelays & (OnMasks[RLY_3_IDX] | OffMasks[RLY_3_IDX] ) )
        {
            //turn the relay on
            digitalWrite(grRlyPins[RLY_3_IDX], LOW);
            //grab the mS count when we did this
            timeRelays[RLY_3_IDX] = millis();
            //this flag is used to know when a relay's 'on'
            //timer is running
            ActiveRelays |= (1 << RLY_3_IDX);
            
        }//if
    }
    else if (PotDiff > 50 && PotDiff <= 100) 
    {
        //same comments as above
        if( ActiveRelays & (OnMasks[RLY_4_IDX] | OffMasks[RLY_4_IDX] ) )
        {
            digitalWrite(grRlyPins[RLY_4_IDX], LOW);
            timeRelays[RLY_4_IDX] = millis();
            ActiveRelays |= (1 << RLY_4_IDX);
            
        }//if
                
    }//else

}//CheckPots