My team would like to be able to change a variable by a set increment (the concept of trim adjustments on “regular” RC vehicles) based on the number of times a button is pushed on the controller. The code we wrote is working properly except for the fact the value of the variable continues to increase (or decrease) as long as the button is held down. Is there a way to eliminate the duration factor and just have the code register one button push as one event, regardless of how long the button is held down?
//Trim Up
if( vexRT Btn6UXmtr2 ] == 1 )
{
trim += 1.0;
}
//Trim Down
if( vexRT Btn6DXmtr2 ] == 1 )
{
trim -= 1.0;
}
Hi, you must use a boolean or any other helper so u can ensure just to count the signal rising ( change from 0 to 1 ) that way u will only increase 1.0 every time you press the btn no matter how long it stay pressed, it will only increase or decrease 1.0 when the btn was released and then be pushed
Bool press=true;
If (press==true)
{
If (vexRt[btn5d]==1)
{
Press =false;
Trim+=1.0;
}
}
Else if (vexRt[btn5d]==0)
{
Press=true;
}
Right now, your code says to increment or decrement ‘trim’ every time it sees the respective button pressed. You wish to get it to only increment or decrement ‘trim’ the first time it sees the respective button pressed. To do this, you need to store the value of the buttons every time through the loop, so that after the button press has been seen, and ‘trim’ has been incremented/decremented, your code can refrain from modifying ‘trim’ again until it first sees the button released. Here is an example of that:
task usercontrol() {
word btnU, btnD, heldU = 0, heldD = 0;
while (true) {
// Collect button values.
btnU = vexRT[Btn6UXmtr2];
btnD = vexRT[Btn6DXmtr2];
if (btnU && !heldU) {
trim += 1.0; // Trim up.
}
if (btnD && !heldD) {
trim -= 1.0; // Trim down.
}
// Save old button values.
heldU = btnU;
heldD = btnD;
}
}