Flip Flop Fun
I've been trying to code some functions with a gamepad in c for some time. When a button is held down on the gamepad, calling vexRT[Btn4D]
(the '4D' just means the fourth set of buttons in the down direction) will return either true or false.
If you have a boolean, say x, and you wanted to turn 'x' on with a single press and release of the button and have it stay on, there's no simple method/function predefined.
So I spent some time and came up with a simple flip-flop that can be seen here:
if (vexRT[Btn7D]) {
if (y)
x = false;
else
x = true;
}
else if (counter == 0) {
counter = 25;
if (x)
y = true;
else
y = false;
}
else
counter--;
Then simply, you could just say if (x) or if (!x) and do whatever you want to do.
The problem is that I have 8 buttons on the gamepad and I want them each to have their own ability to 'flip'. I could simply copy out the code eight times, making a new variable such as x2 and counter2, however this seems redundant. I tried making a method that would shorten the total code but I know it doesn't work. Here it is anyway:
bool tFlip(bool x) {
bool y = !x;
if (y)
return true;
else
return false;
}
Long story short, if anyone knows how to properly code these things I'd love to know how. I've tried googling however I can't find anything on it. Thanks.
TLDR: Anyone know how to code a Flip-Flop?
EDIT: This is all inside a loop so I can't simply do
x = !x
as it will continually be called when the button is held down; the value of x would essentially be 'random' when you release. The y is there to act as a 'wait until key release' as there is no native key pressed/key pressed detection method.