Delay in Relayshield

I have a requirement where I need to turn a relay on after so many minutes. I got a function that works but while delay in progress to execute that, I cannot call any other function and give me an error that timerdelay has been disturbed. Is there a way to keep the delay active and use other functions or variable calls?

Here is my code in use:

int relayOnDelay(String command) {
    // Ritual incantation to convert String into Int
    char inputStr[64];
    command.toCharArray(inputStr,64);
    char *tempStr;
    int i, delaySecs;
    
    tempStr = strtok(inputStr, "|");
    if (tempStr != NULL) {
        i = atoi(tempStr);
    }
    
    tempStr = strtok(NULL, "|");
    if (tempStr != NULL) {
        delaySecs = atoi(tempStr);
    }
    
    // Turn the desired relay on after specified seconds of delay.
    delay(1000*delaySecs);
    myRelays.on(i);
    
    // Respond
    return 1; 
}

Thanks.

When you want to use delays you should seperate the delayed action out of the Particle.function() callback.
A Particle.function() is expected to return within 3 seconds in order to let the cloud know whether it was successful or not.
So one way to go would be a on-shot Software Timer which to which you set the periode according to your parameter and then start running. The timer callback would then perform the delayed action.

BTW, a useful return value for your function would probably be return delaySecs; to also report back whether the parameter was parsed correctly or not.

2 Likes