|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
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?
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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??? |
|
#4
|
|||
|
|||
|
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.
|
|
#5
|
|||
|
|||
|
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.
|
|
#6
|
||||
|
||||
|
Re: Relay Programming Questions...
Could somebody help me code that? I don't understand. What do I need to change?
|
|
#7
|
|||||
|
|||||
|
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.
|
|
#8
|
||||
|
||||
|
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?
|
|
#9
|
||||
|
||||
|
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! |
|
#10
|
||||
|
||||
|
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. |
|
#11
|
|||||
|
|||||
|
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; 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++;
}
}
Last edited by bush : 21-02-2006 at 12:47. |
|
#12
|
||||
|
||||
|
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. ![]() |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A few simple programming questions; | Oumonkey | Programming | 18 | 17-02-2006 14:24 |
| #1 The Journey of a FIRST Graduate: Questions | Ken Leung | General Forum | 12 | 27-07-2005 18:24 |
| Problems and questions about programming | hedgehogger | Programming | 4 | 15-01-2005 18:18 |
| Default mapping relay questions | Jared Stofflett | Programming | 1 | 25-02-2004 19:06 |
| A few questions on programming the Control System | Avarik | Control System | 21 | 08-02-2004 11:42 |