Relay Programming Questions...

We need one button to change multiple relays several times, what would be a good way to go about doing this? Does anybody have any code that will do this?

The best way to do this is with a simple state machine. You can do this by using one variable to act as a counter, and then incrementing that counter every program loop. With just simple if/else statements you can change the sate of several outputs and then reset your counter and await activation again.

If you are still stuck I’ll post an example for you.

I figured that out…I actually got it to work, but it works once and then not again. I made sure it was resetting the counters.

This is the code i wrote…
This is in the Default_Routine:

if(p1_sw_trig)
{
counter1++;
if(counter1 == 14)
{
shoot_level = 1;
counter1 = 0;
}
}
else if(!p1_sw_trig)
{
counter1 = 0;
}

if(shoot_level > 0)
{
shoot_level_runner();
shoot_level++;
printf("%d
", shoot_level);

}
if(shoot_level == 61)
{
shoot_level = 0;
counter1 = 0;
}

This is the funtion it calls:

void shoot_level_runner(void)
{
if(shoot_level == 1)
{
relay4_rev = 1; // top ram out
}
if(shoot_level == 20)
{
relay3_fwd = 1; // bottom ram out
}
if(shoot_level == 40)
{
relay3_rev = 1; // bottom ram in
}
if(shoot_level == 60)
{
relay4_fwd = 1; // top ram in
}
}

Why does it only work once???

From what I see you need to reset the state of the relays. Once they receive a value they will stay that way until that output is modified.

You have no logic to turn the relays off. You have to do a relay4_fwd = 0 when you set the reverse on, and so on.

Could somebody help me code that? I don’t understand. What do I need to change?

I briefly read over your code, and I’m not exactly sure what you’re trying to do. Explain what you want in words, and I’ll try to help you out with your code.

Basically we are controlling our feeding mechanism to our flywheels. We have 2 rams, an upper and a lower. By defualt, the lower ram is extended holding up a stack of balls. When we press the joystick trigger, we want to fire the top ram which will then hold up the stack of balls. Then retract the lower ram which will drop only one ball into the flywheels. Then extend the the lower ram again and then retract the top ram, so as the stack drops and we can repeat. Does that explain it?

Shameless plug:
http://www.chiefdelphi.com/forums/showthread.php?p=455507#post455507

Those might make it easier to set up your relays, less confusing logic to deal with. It’s a series of macros to set relays FWD, REV, and OFF, one-liners, saves some headaches.

Good luck!

Telling it what to do isn’t the problem. It works fine except it only does it once, then it won’t do it again. Also, when I do an if statement, like this:
if(counter3 == 1)
{
relay3_fwd = !relay3_fwd;
}
It always executes this statement, whether or not counter3 = 1. Even with a printf I found that counter3 was equal to 0 and it still executed this statement.

Try this…

Declare/initialize these variables at the top of Default_Routine

static unsigned char shooting = 0;
static unsigned char counter1 = 0;

And add the following anywhere in the body of Default_Routine

if (p1_sw_trig) shooting = 1;
if (shooting)
{
	switch (counter1)
	{
		case 0: //TOP RAM OUT
			relay4_fwd = 0;
			relay4_rev = 1;
			break;
		case 20: //BOTTOM RAM IN
			relay3_fwd = 1;
			relay3_rev = 0;
			break;
		case 40: //BOTTOM RAM OUT
			relay3_fwd = 0;
			relay3_rev = 1;
			break;
		case 60: //TOP RAM IN
			relay4_fwd = 1;
			relay4_rev = 0;
			shooting = 0;
			counter1 = 0;
			break;
		default:
			counter1++;
	}
}

I have already tried the exact same thing you are doing with this and surprisingly, it didn’t work. But with lots of trys we finally found something that did:
if(!p4_sw_trig){
if(counter1 > 9 & counter1 < 61)
{
counter1++;
}
else if(counter1 < 10 || counter1 > 60)
{
counter1 = 0;
}
} else if(p4_sw_trig){
if(counter1 < 1000)
counter1++;
}
if(counter1 == 10)
{
relay4_fwd = !relay4_fwd;
}
if(counter1 == 30)
{
relay3_fwd = !relay3_fwd;
}
if(counter1 == 50)
{
relay3_fwd = !relay3_fwd;
}
if(counter1 == 60)
{
relay4_fwd = !relay4_fwd;
}

I don’t know why this worked and that didn’t but it works and I’m happy. Thanks, everybody, for helping out. :slight_smile: