Log in

View Full Version : Button Programing


BobcatProgramer
25-02-2004, 13:01
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

Alan Anderson
25-02-2004, 13:14
Put something this in your user_routine, substituting appropriately for switch_value and relay_fwd/rev:


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;

Astronouth7303
25-02-2004, 15:53
Put something this in your user_routine, substituting appropriately for switch_value and relay_fwd/rev:


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;


Can't you do:

//...
//Init Relays
relay1_fwd = 1;
relay1_rev = 0;
//...
if (OI_Switch)
{
relay1_fwd = !relay1_fwd;
relay1_rev = !relay1_rev;
}


Just be sure to only do it again after a certain time. 26.2 ms is still fast, 38 hz fast.

Ryan M.
25-02-2004, 16:01
Can't you do:

//...
//Init Relays
relay1_fwd = 1;
relay1_rev = 0;
//...
if (OI_Switch)
{
relay1_fwd = !relay1_fwd;
relay1_rev = !relay1_rev;
}


Just be sure to only do it again after a certain time. 26.2 ms is still fast, 38 hz fast.Clever. That would work. Just make sure to have the variables static.

Phil_Lutz
25-02-2004, 16:37
We coded buttons/triggers to only activate on release, that helps control the longggggggggg button push :)

Phil

JoshJ
25-02-2004, 16:43
I would do this so your relays dont pulse 40x a second ;) I don't know whose code I just added to tho... sorry

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

Astronouth7303
25-02-2004, 16:43
We coded buttons/triggers to only activate on release, that helps control the longggggggggg button push :)

Double clever! :cool:

[edit]
My code (With suggestion):

//...
//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;
}


Of course, you still have to integrate it.