Quote:
|
Originally Posted by Gamer2028
...I want to have it so when I hit a Trigger on the joystick it sends the motor forward until it hits the Top limit switch or if it is already at the top to reverse the motor until it hits the Bottom Limit Switch.
I have:
Code:
#define T_DRIVE pwm01
#define Up_T_Limit rc_dig_in01 /* Limit Switch on Full Up Set */
#define Down_T_Limit rc_dig_in02 /* Limit Switch on Full Down Set */
#define T_BUTTON_BTN p2_sw_trig /* Button to change T state. */
/* T state variable. */
int intTState = 0;
...
|
I'd try something like this:
Somewhere at top of file:
Code:
// Your stuff
#define T_DRIVE pwm01
#define Up_T_Limit rc_dig_in01 /* Limit Switch on Full Up Set */
#define Down_T_Limit rc_dig_in02 /* Limit Switch on Full Down Set */
#define T_BUTTON_BTN p2_sw_trig /* Button to change T state. */
int intTState = 0; /* T state variable. */
// My stuff
#define RC_SWITCH_ON 0
#define RC_SWITCH_OFF !SWITCH_ON
#define OI_SWITCH_ON 1
#define OI_SWITCH_OFF !OI_SWITCH_ON
#define T_STATE_UP 1
#define T_STATE_DOWN !T_STATE_UP
Somewhere in main loop:
Code:
if(T_BUTTON_BTN == OI_SWITCH_ON)
{
// Switch states. We could do it with intTState = !intTState,
// but just for clarity...
if(intTState == T_STATE_UP)
{
intTState = T_STATE_DOWN;
}
else
{
intTState = T_STATE_UP;
}
}
if(intTState == T_STATE_UP && Up_T_Limit == SWITCH_OFF)
{
// We need to go up more
T_DRIVE = 255;
}
else if(intTState == T_STATE_DOWN && Down_T_limit == SWITCH_OFF)
{
// We need to go down more
T_DRIVE = 0;
}
else
{
// We are at our goal. Stop motor
T_DRIVE = 127;
}
Be advised that I have the code run the motor full speed. If there's no sort of cushion, you could break something. Slow it down if appropriate.