How to open two relays at the same time?

My code is as below. The activity which i want is that when i push power, two relay will open, then after d=3000 & e=2000 period, the relay will shut down individual.

The problem i found is that when i push power, just one relay can open.

Is there someone tell me that how can i modify codes?

Let two relays can open at same time.

Thank you

define PUMP_PIN1 2

define PUMP_PIN2 3

define POWER 4

float d=3000; float e=2000; float f=26.0;

void setup(){ pinMode(PUMP_PIN1, OUTPUT); pinMode(PUMP_PIN2, OUTPUT); pinMode(POWER, INPUT);

//PUMP CONTROL digitalWrite(PUMP_PIN1, LOW); // switch off LED digitalWrite(PUMP_PIN2, LOW); // switch off LED } void loop(){

if(digitalRead(POWER) == HIGH){ digitalWrite(PUMP_PIN1, HIGH); delay(e); digitalWrite(PUMP_PIN1, LOW); } if(digitalRead(POWER) == HIGH){ digitalWrite(PUMP_PIN2, HIGH);

delay(d); digitalWrite(PUMP_PIN2, LOW); } }

I think you mean to do this?

  if(digitalRead(POWER) == HIGH){    
    digitalWrite(PUMP_PIN1, HIGH);  
    digitalWrite(PUMP_PIN2, HIGH); 
    delay(e);
    digitalWrite(PUMP_PIN1, LOW); 
    delay(d-e);
    digitalWrite(PUMP_PIN2, LOW); 
  }

Thanks your response.

If I don't know which one of (d,e) is bigger. (d,e) will be key in by user. Is there other type of codes?

Thank you

Yes, you can set the two pins HIGH and safe a timestamp (millis()), then you just let the loop run and set the respective pins to LOW as soon as timestamp+d/e is smaller than millis(). This has the additional advantage that you processor is free for other tasks in the meantime, instead of being stuck in a delay.

Do you want to try writing that code yourself? Post it afterwards, if you want, so we can check it.

alensriver: Thanks your response.

If I don't know which one of (d,e) is bigger. (d,e) will be key in by user. Is there other type of codes?

Thank you

Use the blink without delay type structure to set the off time of each relay. See the example in the IDE

Time as understood by the Arduino is in whole milliseconds or whole microseconds. Using floats to store time looks clueless.

I have tried to used millis(), but it still can’t open two relays at the same time.
Just one relay open, however, it never close.
Is there someone help me to check the code as below?

Blink example is not suited to this project.
Because this project needs two relays open at same time, close at different time.
thank you

#define PUMP_PIN1 2
#define PUMP_PIN2 3
#define POWER 4

float d=3000;
float e=2000;
float f=26.0;
long startime;
void setup(){
pinMode(PUMP_PIN1, OUTPUT);
pinMode(PUMP_PIN2, OUTPUT);
pinMode(POWER, INPUT);

//PUMP CONTROL
digitalWrite(PUMP_PIN1, LOW); //
digitalWrite(PUMP_PIN2, LOW); //
}
void loop(){

if(digitalRead(POWER) == HIGH)
{
digitalWrite(PUMP_PIN1, HIGH);
digitalWrite(PUMP_PIN2, HIGH);

startime=millis();
while(startime-millis() > d)
{
digitalWrite(PUMP_PIN2, LOW);
}
while(startime-millis() > e)
{
digitalWrite(PUMP_PIN2, LOW);
}
}

}

Use Tools + Auto Format to fix your horrid indentation. Then, your problem will be quite clear.

Aside from the fact that you are simply re-implementing delay()...

The principle of using millis() instead of delay is simple.

Save the value of millis() when the action started in a variable, let's call it startTime Each time through the loop() function check whether startTime minus the value of millis() now is greater than or equal to the required interval. If not, then carry on going round the loop() function until it is When it is, then execute the code you want to happen at the end of the period. If the period timing is to be repeated then save the time the value of millis() in the startTime variable again to restart the wait for the period to end.

Using this method you can have other code, such as reading input, interspersed with checking if the timing period has ended because the loop() function runs continuously and code execution is not blocked unlike when using delay(). As has been pointed out your current code just mimics the delay() function and can do nothing until the period has ended. This is not a good way to use millis() for timing.

Note that you can have more than one variable, or an array, holding a number of start times and periods, so that you can do independant timing and carry out different actions at different frequencies.

Blink example is not suited to this project.

YES IT IS!!!

Try reading this:- http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

A small point: millis() returns unsigned long. If you use long to hold the result, you may get unexpected results once the value overflows.

    while(startime-millis() > e)
    {
      digitalWrite(PUMP_PIN2, LOW); 
    }

Like Paul says you've just created a delay. The sketch is stuck in that loop until it's finished. Anytime the code gets stuck in a block or on a statement it is said to be blocking. You don't want this. delay() has its uses but I've found that if I need a delay of more than 100 millis I rethink my problem. Arduino can do a lot in 100 millis.

We use a conditional statement to see if we need to change a pin's state. Once a pin is written LOW or HIGH it will stay that way until your sketch changes it.

 // this just checks if the duration, e ( a really poor variable name), has passed
// if it has then the pin is written LOW otherwise it just goes on to other things. Like checking on the other relay.
if (millis() - startime >= e) 
    {
      digitalWrite(PUMP_PIN2, LOW); 
    }

You'll find that you can't simply make this change and have it work. It will turn your relay back on right away. Probably need another variable to keep the machine's state so you can skip turning the relays on if the relays are on.

You're getting close. :D

alensriver: If I don't know which one of (d,e) is bigger. (d,e) will be key in by user.

These are the kind of things that really need to be in your problem description or we can give you a correct answer that still won't work for you. My answer meets all the requirements you gave - you just didn't give all the requirements.

The Blink Without Delay example is exactly what you need to be doing here. You need to be able to take examples like that and mash them together, scale them up, provide an interface, etc - you are not going to find coding examples showing how to blink two lights, you are supposed to figure that out yourself. That is one of the skills of programming - being able to look at part of a solution and figure out how it works with the other parts. The other major skill is being able to break big problems into little ones that can be solved. Once you master those two things, you'll be miles ahead of a lot of people who actually make money writing software.

So please, look at the Blink Without Delay example - if there's anything you don't understand, we will help you. Keep in mind that the Arduino is always running a loop that never ends, and I think you'll understand what's going on there - it goes through the loop over and over again. Work it through in your mind, what happens each time through the loop, and what happens when certain time periods are exceeded, and then it goes through the loop. Think about "State changes" that happen - suddenly an IF statement becomes true, etc, that kind of thing.

Think about the loop in terms of a sauce recipe.... Setup routine would be to add spices and liquids to a pot and turn on the heat. Loop routine would be

if (sauceIsNotReadyYet) {
  Stir();
}

And that would repeat forever and ever until sauceIsNotReadyYet becomes false by some other method of the code - then that block will no longer execute, but it will continue to be run over and over again, forever.

Until you understand the Blink Without Delay you can use this:

if (d > e)
 {
   delay (d-e);
 }
else
 {
   delay (e-d);
 }

http://www.gammon.com.au/blink

Also:

Please use code tags.

Read this before posting a programming question

Shpaget: Until you understand the Blink Without Delay you can use this:

if (d > e)
 {
   delay (d-e);
 }
else
 {
   delay (e-d);
 }

I think it's neat that this does the same thing. Also a pretty clear demonstration of why single letter variable names are a bad idea. It's almost unreadable.

delay( d > e ? d - e : e - d );

Or just use "abs()"