One function must complete before others can run

Hi Guys I hope I can explain this clearly.
I am very new to programming and I'll be reading up on it shortly but I had what I hope is a simple question.

I am using 2 SPDT relay circuits and what they do is change the polarity of two wires to move a brushless DC motor forward and reverses.

I'm going to use an IR remote and arduino (with TIP122) to turn relay 1 and 2 on and off.
I can do this type of work with the information out on this website and others.

A very important thing is relay 1 and relay2 absolutely cannot be activated at the same time or I'm going to let the magic smoke out of all my electronics.

How can I write the code in a way that when the button for relay 1 is activated, relay 2 cannot activate until relay 1 is deactivated.

Ideally I'd like the buttons to be a two push for each relay, relay 1 on, relay 1 off.

How can I write the code in a way that when the button for relay 1 is activated, relay 2 cannot activate until relay 1 is deactivated.

Just make sure you digitalWrite() one relay pin LOW (if that means off) before writing the other one HIGH (if that means on).

That is, every time you need to change the state of one relay, you need to change the state of both, so that both are off, before you turn the proper one on.

PaulS:
Just make sure you digitalWrite() one relay pin LOW (if that means off) before writing the other one HIGH (if that means on).

That is, every time you need to change the state of one relay, you need to change the state of both, so that both are off, before you turn the proper one on.

Thanks Paul. I know that I can use the LOW and High as on or off but don't really grasp the way to program it in a way that meets those conditions. I will have to read up on some tutorials.
Thanks for leading me in the right direction.

What you've described is VERY poor design. At some point, perhaps due to a software "crash" or power brown-out, EMI, or whatever, those two outputs WILL get set, and there goes your electronics. Instead, you should wire the relays such that it is virtually impossible to put the electronics at risk. Using two DPT relays will accomplish that easily - use one set of contacts on each relay to do the switching you want, the other other set as an interlock, so that if both relays are energized, the electronics still see a "safe" state.

Regards,
Ray L.

but don't really grasp the way to program it in a way that meets those conditions. I will have to read up on some tutorials.

Lets say that the relays are connected to relayPinOne and relayPinTwo. Lets say that HIGH means off and LOW means on.

I find it easier to write some functions:

void turnBothOff()
{
   digitalWrite(relayPinOne, HIGH);
   digitalWrite(relayPinTwo, HIGH);
}

void turnOneOn()
{
   turnBothOff();
   digitalWrite(relayPinOne, LOW);
}

void turnTwoOn()
{
   turnBothOff();
   digitalWrite(relayPinTwo, LOW);
}

Now, if you call turnOneOn(), it will make certain that the two relays are turned off before turning the first one on. Similarly, if you call turnTwoOn(), it will make certain that the two relays are turned off before turning the second one on.

I'll leave it to you to figure out how to write turnOneOff() and turnTwoOff().

RayLivingston:
What you've described is VERY poor design. At some point, perhaps due to a software "crash" or power brown-out, EMI, or whatever, those two outputs WILL get set, and there goes your electronics. Instead, you should wire the relays such that it is virtually impossible to put the electronics at risk. Using two DPT relays will accomplish that easily - use one set of contacts on each relay to do the switching you want, the other other set as an interlock, so that if both relays are energized, the electronics still see a "safe" state.

Regards,
Ray L.

Good advice. Just be sure that the double throw relay is a break-before-make design. A make-before-break design will fill the air with the magic smoke.

RayLivingston:
What you've described is VERY poor design. At some point, perhaps due to a software "crash" or power brown-out, EMI, or whatever, those two outputs WILL get set, and there goes your electronics. Instead, you should wire the relays such that it is virtually impossible to put the electronics at risk. Using two DPT relays will accomplish that easily - use one set of contacts on each relay to do the switching you want, the other other set as an interlock, so that if both relays are energized, the electronics still see a "safe" state.

Regards,
Ray L.

Ray, thanks for that. I went over my schematic again and there is no relay configuration that will destroy me electronics. I don't know why I thought that...I just assumed I guess.

The way it is set up now, with both relays open, the motor sees no power. With R1 switched it rotates clockwise. With R1 open & R2 switched then it spins counter clockqise. With both R1 and R2 switch there is no load so everything is gravy.

It sounds like you have +V and 0V connected to the common of the relays, and are connecting the motor terminals to the contacts.

Turn the relays around. Connect the common to the motor terminal, +V to one contact and 0V to the other. Then your 4 states are +- (forward), -+ (reverse) -- (off), and ++ (also off). No matter what way you set the relays, nothing gets shorted and it's completely safe.

mattysull:
Ray, thanks for that. I went over my schematic again and there is no relay configuration that will destroy me electronics. I don't know why I thought that...I just assumed I guess.

The way it is set up now, with both relays open, the motor sees no power. With R1 switched it rotates clockwise. With R1 open & R2 switched then it spins counter clockwise. With both R1 and R2 switch there is no load so everything is gravy.

...or maybe I'm wrong. How are your relays wired? That's kind of an important bit of info.

Jiggy-Ninja:
Turn the relays around. Connect the common to the motor terminal, +V to one contact and 0V to the other. Then your 4 states are +- (forward), -+ (reverse) -- (off), and ++ (also off). No matter what way you set the relays, nothing gets shorted and it's completely safe.

This is exactly how it is wired. COM are on the motor terminals. It is just how you say. I still plan to write the program so that R2 is turned off while R1 is one and R1 off while R2 is on.