|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
I have a relay programed to a button but now I want it to be a press to be forward and then press again to reverse. For example, I am using a selanoid and i want to press the button to have the cylinder go out then the next time i press it the cylinder go in. I can do this easilly in p-basic but I have tried to do it in C and I can't get it to work right. Could anyone give me a hand with this by posting code. It would be appreciated. Thanks
|
|
#2
|
|||||
|
|||||
|
Re: Button Programing
Put something this in your user_routine, substituting appropriately for switch_value and relay_fwd/rev:
Code:
static char relay_state = 0; /* 0 means forward, 1 means reverse */
static char last_switch_value = 1; /* will remember the last state of the switch */
if ((last_switch_value == 0) && (switch_value == 1))
{
// here is where you toggle the relay state
relay_state = 1-relay_state;
if (relay_state == 0)
{
relay_fwd = 1;
relay_rev = 0;
}
else
{
relay_fwd = 0;
relay_rev = 1;
}
}
last_switch_value = switch_value;
|
|
#3
|
|||||
|
|||||
|
Re: Button Programing
Quote:
Code:
//...
//Init Relays
relay1_fwd = 1;
relay1_rev = 0;
//...
if (OI_Switch)
{
relay1_fwd = !relay1_fwd;
relay1_rev = !relay1_rev;
}
|
|
#4
|
||||
|
||||
|
Re: Button Programing
Quote:
|
|
#5
|
||||
|
||||
|
Re: Button Programing
We coded buttons/triggers to only activate on release, that helps control the longggggggggg button push
![]() Phil |
|
#6
|
|||
|
|||
|
Re: Button Programing
I would do this so your relays dont pulse 40x a second
I don't know whose code I just added to tho... sorryint flag=0; //... //Init Relays relay1_fwd = 1; relay1_rev = 0; //... if (OI_Switch && !flag) { relay1_fwd = !relay1_fwd; relay1_rev = !relay1_rev; flag=1; } if (!OI_Switch) { flag=0; } |
|
#7
|
|||||
|
|||||
|
Re: Button Programing
Quote:
![]() [edit] My code (With suggestion): Code:
//...
//Init Relays
relay1_fwd = 1;
relay1_rev = 0;
//...
static char SwitchVals = 0;
if (OI_Switch)
{
SwitchVals = 1;
}
if (!OI_Switch & SwitchVals)
{
relay1_fwd = !relay1_fwd;
relay1_rev = !relay1_rev;
SwitchVals = 0;
}
Last edited by Astronouth7303 : 25-02-2004 at 16:52. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| pnumatic programing | jacob_dilles | Programming | 8 | 24-02-2004 21:49 |
| which button machine do you use and do you like it? | KenWittlief | General Forum | 10 | 16-10-2003 00:01 |
| Who is doing the programing for team 61 | Walter_Jr | Programming | 5 | 25-01-2003 00:31 |
| Select button and user mode | n[ate]vw | Programming | 2 | 20-01-2003 16:33 |
| Programing of controller | DLyons | Robotics Education and Curriculum | 2 | 02-12-2002 00:16 |