Multiple Delays Simultaneously.

Hello there.
I am a newbie to Arduino and have a project that i m working on, i need some help :).
Project Definition: I have 5 relays connected to Arduino that should be energized after some delay in minutes. I read data from Serial port which indicates delay. All the relays have different delays depending on the user entered values on a screen.
(Find attached image)

The problem I am facing is that i used delay() function which hold my program to that point. Can anyone please help me out in programming.

Say if the user enters 7 mins for the first relay delay and 5 mins for the second relay, then Arduino should energize relay 1 and 2 after 7 and 5 mins respectively.

I would be very grateful if someone would help me out.
Do let me know if u need any other info from my side.

Thank you in advance. XD

The problem I am facing is that i used delay() function

Put a period after that part of the sentence. And, NEVER use delay again.

Look at the blink without delay example. It shows how to note when an event occurs (such as turning a relay on). It shows how to determine if now minus then exceeds some value (like how long the relay should be on. It shows how to execute some action when that happens.

PaulS,

Thanks man,
I have already gone thro' that code.

Could u please show me a program!! How would u program it??

Thank you.

I have already gone thro' that code.

So you should now be able to apply the BWD principle to one relay. Have you done that ? Once you have one working then extending the idea to n relays will be relatively easy.

Well, it looks like you have "Multiple Posts Simultaneously" figured out.
P.S. (Not a good idea)

UKHeliBob,

BWD Principle?? whats that??
Any ways this is my code now.

int intread(){
  int i = Serial.read();
//  delay(10);
  Serial.flush();
//  int j =Serial.read();
//  delay(10);
//  Serial.flush();/
//  int m = i + j ;
  return i;
//  Serial.flush();
}

Sorry that was part of my code.

Here z full one

void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop() {
  if(Serial.available())
  {
    int n = Serial.read();
    myDelay(10);

    switch(n)
    {
    case 'x':
      {
        int d2;
        d2 = intread();
        Serial.print(d2);
        Delay(d2);
        digitalWrite(2, HIGH);
        Serial.flush();
        break;
      }

    case 2:
      {
        int d3;
        d3 = intread();
        Serial.print(d3);
        Delay(d3);
        digitalWrite(3, HIGH);
        Serial.flush();
        break;
      }

    case 3:
      {
        int d4;
        d4 = intread();
        Serial.print(d4);
        Delay(d4);
        digitalWrite(4, HIGH);
        Serial.flush();
        break;
      }
  //
      case 4:
      {
        int d5;
        d5 = intread();
        Serial.print(d5);
        Delay(d5);
        digitalWrite(5, HIGH);
        Serial.flush();
        break;
      }

    case 5:
      {
        int d6;
        d6 = intread();
        Serial.print(d6);
        Delay(d6);
        digitalWrite(6, HIGH);
        Serial.flush();

        break;
      }

    case 6:
      {
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
        digitalWrite(5, LOW);
        digitalWrite(6, LOW);
        Serial.print("asdfgh");
        //        Serial.flush();

        break;
      }

    case 7:
      {
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, HIGH);
        Serial.print("++++++");

        //        Serial.flush();

        break;
      }


    }
  }
}



void Delay(int n)
{
  unsigned long start = millis();
  unsigned long s = n*1000;
  while (millis() - start <= s)
  {
    ;
  }  
}

void myDelay(unsigned long duration)
{
  unsigned long start = millis();

  while (millis() - start <= duration)
  {
    ; 
  }
}


int intread(){
  int i = Serial.read();
  Serial.flush();
  return i;

}

What should i put in void Delay(int n) function so that Arduino does not paralyze here?? :frowning:

There is a very good page (written by another user of this forum) with a very nice explanation of this topic (and several other interesting things...).
It starts by showing how one can achieve a "delay effect" without using the delay(), and then it shows how it can be used for more than one elements (even "delays" with different duration for each element).
The page is here:
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

I think the important would be that you understand how it works, and then try to write your own code to do exactly what you want.
:wink:

boguz,

thank you.
I am going thro' the page. :slight_smile:

durbz:
thank you.
I am going thro' the page. :slight_smile:

:wink:

Bookmark it and go through the other when you have the time. There are some very cool things there!

BWD Principle?? whats that??

BlinkWithoutDelay

What should i put in void Delay(int n) function so that Arduino does not paralyze here??

You cannot simply replace delay() with some magic code that will not cause your program to stall. Instead you need to refactor your code.

When the start event occurs save the value from millis(). Now, on each pass through the loop() function check if required period of time has passed by comparing millis() now with the saved value. If the difference is greater than the required period then the 'delay' is over so act on it. If not go round loop() again checking for other elapsed periods in the same way and acting accordingly.

Congratulations on being my latest "Most Hated Noob" for cross posting the same question the greatest number of times

Congratulations on being my latest "Most Hated Noob"

Oooh, I can assure you that is NOT a good list to be on.