user11916: I am doing a project which needs to meet the following specifications.

On receiving an SMS arduino will start switching 1st relay on after some
  time duration(say 30 Seconds) relay 1 will be turned off and it will now turn on 2nd relay for (say 20seconds) so on 1>2>3>4.

The problem im facing is managing the time intervals between the switching process,i did think of using millis() i.e (currentMillis-previousMillisx>Intervalx) as in blinkWithoutDelay ,how ever this will work only once as i dont not know when will the next SMS come to the system ,if the user decides to send the message after 1hour currentMillis-previousMillisx will always return true so i cannot keep the motor on for (Say 30 seconds) and same will happen with all 4 cases.
If there is any alternative logic for this problem please suggest me or can i just add RTC for my project to manage time easily
Thank You

user7508: I do not really understand your problem. More specifically,
“currentMillis-previousMillisx will always return true” does not make
sense, as it is not a boolean expression (it's unsigned long).
Anyway, the technique shown in blinkWithoutDelay is the way to go. Here
is my (untested) version:
const int relay_count = 4;  // we have 4 relays
const int relay_pins[relay_count] = {2, 3, 4, 5};
const unsigned long durations[relay_count] = {30000, 20000, 15000, 10000};
bool sms_received();  // you implement this one

void loop()
{
    static int current_relay = -1;  // meaning: no active relay
    static unsigned long last_switch;
    unsigned long now = millis();

    // If no relay is active, wait for an SMS.
    if (current_relay == -1) {
        if (sms_received()) {
            // Switch on first relay.
            current_relay = 0;
            digitalWrite(relay_pins[current_relay], HIGH);
            last_switch = now;
        }
    }

    // Otherwise, switch relays when it is time to do so.
    else if (now - last_switch >= durations[current_relay]) {
        digitalWrite(relay_pins[current_relay], LOW);
        if (++current_relay == relay_count) {
            current_relay = -1;  // no active relay
        } else {
            digitalWrite(relay_pins[current_relay], HIGH);
            last_switch = now;
        }
    }
}

BTW, you did not specify what should happen if you receive an SMS while
one of the relays is active. I assume the SMS are only acknowledged when
no relay is active. Otherwise you would have to change the code.

user10214: Well, you need a sort of tracking for your active relay, don't you? So you just have to reset that tracking variable and the previousMillis variables and you are done.
Some code (note: untested and moreover I don't know how you are receiving the SMS)
// The intervals for the different relays
const byte Intervals[] = {30, 20, 10, 5};

// The current state. 0 means relay 1 on, 
// 1 means relay 2 on, 2->3 and 3->4, while
// 4 means no relay activated
byte currentState = 4;

unsigned long previousMillis;


void loop()
{
    if (received_sms)
    {
        currentState = 0;
        previousMillis = millis();
        digitalWrite(RELAY1, HIGH);
        // Maybe turn off all the other relays?
    }

    if (currentState < 4)
    { // Active state: check if we should advance
        unsigned long currentInterval = ((unsigned long)Intervals[currentState])*1000;
        while ((millis() - previousMillis) >= currentInterval)
        { // Advance to the next step
            switch(currentState)
            {
                case 0: // Advance from state 0 to 1
                    digitalWrite(RELAY1, LOW);
                    digitalWrite(RELAY2, HIGH);
                    // Maybe turn off all the other relays?
                    previousMillis += currentInterval;
                    currentState = 1;
                    break;
                ...
                // cases 1, 2, 3
                ...
                default:
                    // It should never reach this
                    previousMillis = millis();
                    currentState = 4;
                    break;
            }
        }

    }
}


11916: <p>I am doing a project which needs to meet the following specifications.</p>

<blockquote>
  <p>On receiving an SMS arduino will start switching 1st relay on after some
  time duration(say 30 Seconds) relay 1 will be turned off and it will now turn on 2nd relay for (say 20seconds) so on 1>2>3>4.</p>
</blockquote>

<p>The problem im facing is managing the time intervals between the switching process,i did think of using millis() i.e <strong>(currentMillis-previousMillisx>Intervalx)</strong> as in blinkWithoutDelay ,how ever this will work only once as i dont not know when will the next SMS come to the system ,if the user decides to send the message after 1hour currentMillis-previousMillisx will always return true so i cannot keep the motor on for (Say 30 seconds) and same will happen with all 4 cases.</p>

<p>If there is any alternative logic for this problem please suggest me or can i just add RTC for my project to manage time easily</p>

<p>Thank You</p>
 7508: <p>I do not really understand your problem. More specifically,
“currentMillis-previousMillisx will always return true” does not make
sense, as it is not a boolean expression (it's <code>unsigned long</code>).</p>

<p>Anyway, the technique shown in blinkWithoutDelay is the way to go. Here
is my (untested) version:</p>

<pre class="lang-c++ prettyprint-override"><code>const int relay_count = 4;  // we have 4 relays
const int relay_pins[relay_count] = {2, 3, 4, 5};
const unsigned long durations[relay_count] = {30000, 20000, 15000, 10000};
bool sms_received();  // you implement this one

void loop()
{
    static int current_relay = -1;  // meaning: no active relay
    static unsigned long last_switch;
    unsigned long now = millis();

    // If no relay is active, wait for an SMS.
    if (current_relay == -1) {
        if (sms_received()) {
            // Switch on first relay.
            current_relay = 0;
            digitalWrite(relay_pins[current_relay], HIGH);
            last_switch = now;
        }
    }

    // Otherwise, switch relays when it is time to do so.
    else if (now - last_switch &gt;= durations[current_relay]) {
        digitalWrite(relay_pins[current_relay], LOW);
        if (++current_relay == relay_count) {
            current_relay = -1;  // no active relay
        } else {
            digitalWrite(relay_pins[current_relay], HIGH);
            last_switch = now;
        }
    }
}
</code></pre>

<p>BTW, you did not specify what should happen if you receive an SMS while
one of the relays is active. I assume the SMS are only acknowledged when
no relay is active. Otherwise you would have to change the code.</p>
 10214: <p>Well, you need a sort of tracking for your active relay, don't you? So you just have to reset that tracking variable and the previousMillis variables and you are done.</p>

<p>Some code (note: untested and moreover I don't know how you are receiving the SMS)</p>

<pre><code>// The intervals for the different relays
const byte Intervals[] = {30, 20, 10, 5};

// The current state. 0 means relay 1 on, 
// 1 means relay 2 on, 2-&gt;3 and 3-&gt;4, while
// 4 means no relay activated
byte currentState = 4;

unsigned long previousMillis;


void loop()
{
    if (received_sms)
    {
        currentState = 0;
        previousMillis = millis();
        digitalWrite(RELAY1, HIGH);
        // Maybe turn off all the other relays?
    }

    if (currentState &lt; 4)
    { // Active state: check if we should advance
        unsigned long currentInterval = ((unsigned long)Intervals[currentState])*1000;
        while ((millis() - previousMillis) &gt;= currentInterval)
        { // Advance to the next step
            switch(currentState)
            {
                case 0: // Advance from state 0 to 1
                    digitalWrite(RELAY1, LOW);
                    digitalWrite(RELAY2, HIGH);
                    // Maybe turn off all the other relays?
                    previousMillis += currentInterval;
                    currentState = 1;
                    break;
                ...
                // cases 1, 2, 3
                ...
                default:
                    // It should never reach this
                    previousMillis = millis();
                    currentState = 4;
                    break;
            }
        }

    }
}
</code></pre>
