I had a similar issue when programming the pneumatics for our upper/lower claws, wrist, extension, and lateral drive. Let me show you how I solved the issue.
Code:
if (CLAW1IN == 1 && clawTimer < 3)
{
clawTimer++;
}
else if (CLAW1IN == 1 && clawTimer >= 3 && clawTest == 0)
{
CLAWFWD = !CLAWFWD;
CLAWREV = !CLAWREV;
clawTest = 1;
}
else if (CLAW1IN == 0 && clawTimer >= 3)
{
clawTimer = 0;
clawTest = 0;
}
In this case we use double action solenoids, so if you just use single ignore the CLAWREV. The clawTimer is used as a measure of necessary depression of the trigger to create a change (eliminating accidental presses) and clawTest is a signal to keep the values from changing again. However, you do need to make sure for doubles that you initialize them as 1, 0, or 0, 1 to begin with, or you'll get 0, 0, and 1, 1 values for the solenoid, I.E. nothing happening.
EDIT:
Oops, forgot to fill in for the definitions.
Code:
#define CLAW1IN p1_sw_aux1
#define CLAWFWD relay2_fwd
#define CLAWREV relay2_rev