Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Relay Programming Questions... (http://www.chiefdelphi.com/forums/showthread.php?t=44462)

comwiz7 21-02-2006 00:41

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?

MichaelGoldfarb 21-02-2006 01:13

Re: Relay Programming Questions...
 
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.

comwiz7 21-02-2006 01:46

Re: Relay Programming Questions...
 
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\n", 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???

MichaelGoldfarb 21-02-2006 02:38

Re: Relay Programming Questions...
 
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.

steven114 21-02-2006 03:52

Re: Relay Programming Questions...
 
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.

comwiz7 21-02-2006 08:33

Re: Relay Programming Questions...
 
Could somebody help me code that? I don't understand. What do I need to change?

bush 21-02-2006 09:38

Re: Relay Programming Questions...
 
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.

comwiz7 21-02-2006 09:47

Re: Relay Programming Questions...
 
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?

Matt Krass 21-02-2006 11:22

Re: Relay Programming Questions...
 
Shameless plug:
http://www.chiefdelphi.com/forums/sh...507#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!

comwiz7 21-02-2006 11:35

Re: Relay Programming Questions...
 
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.

bush 21-02-2006 12:44

Re: Relay Programming Questions...
 
Try this...

Declare/initialize these variables at the top of Default_Routine

Code:

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

And add the following anywhere in the body of Default_Routine

Code:

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++;
        }
}


comwiz7 21-02-2006 13:04

Re: Relay Programming Questions...
 
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. :)


All times are GMT -5. The time now is 13:14.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi