Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Button Programing (http://www.chiefdelphi.com/forums/showthread.php?t=26027)

BobcatProgramer 25-02-2004 13:01

Button Programing
 
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

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;


Astronouth7303 25-02-2004 15:53

Re: Button Programing
 
Quote:

Originally Posted by Alan Anderson
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;


Can't you do:
Code:

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

Re: Button Programing
 
Quote:

Originally Posted by Astronouth7303
Can't you do:
Code:

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

Re: Button Programing
 
We coded buttons/triggers to only activate on release, that helps control the longggggggggg button push :)

Phil

JoshJ 25-02-2004 16:43

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... 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

Re: Button Programing
 
Quote:

Originally Posted by Phil_Lutz
We coded buttons/triggers to only activate on release, that helps control the longggggggggg button push :)

Double clever! :cool:

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

Of course, you still have to integrate it.


All times are GMT -5. The time now is 16:47.

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