Button programming

its probably a really answer but it one i don’t know. Can someone help me with it. I have a code for when the button 6U is pressed it will run some different motors. We program this using a boolean loop. The only problem is that we find that the code is reading the button values to fast (By the time u press the button it has turned off the motors and started them again). Is there a way we can add a time to it to only read the button values every 0.5secs… Any help is great.

This is the code we are using

if(vexRT[Btn6U] == 1)
{
if(pressed == false)
{
motor[L4] = 120;
motor[L5] = 120;
motor[L6] = 120;
motor[L7] = 120;
pressed = true;
}
else
{
motor[L4] = 0;
motor[L5] = 0;
motor[L6] = 0;
motor[L7] = 0;
pressed = false;
}
}

Hi! I have a section of code similar to this. The way we did it was using the waitUntil(condition) command. You could try using it like this

if(vexRT[Btn6U] == 1)
{
if(pressed == false)
{
waitUntil(vexRT[Btn6U] == 0);
motor[L4] = 120;
motor[L5] = 120;
motor[L6] = 120;
motor[L7] = 120;
pressed = true;
}

Basically it does not read the rest of the code until Button 6U is released.
I hope this helps!

Is the waitUntil different from an else-if?

A waitUntil is Different from an if-else statement. Its a Natural Language command like your untilBump or untilButtonPress. Think of it as a wait except instead of waiting for a time value, it waits for its desired condition to be met.

Try something called switch debouncing, I believe that BNS has something on that. We just did this recently for our launcher too.

If you want to do a push to start, push to stop type of system, a solution has already been posted. That solution is not the best (as the rest of your program will not react to commands if you were to hold the button), but it is simpler and should work well enough.

Alternatively, you could do something that when the button is pressed the motor runs, while it’s not pressed it doesn’t:


if(vexRT[Btn6U])
	{
	motor[L4] = 120;
	motor[L5] = 120;
	motor[L6] = 120;
	motor[L7] = 120;
	}
else
	{
	motor[L4] = 0;
	motor[L5] = 0;
	motor[L6] = 0;
	motor[L7] = 0;
	}

Also, to make your posts easier to read, please use the CODE tags. When writing a post (in the advanced one, not quick reply) it is the number symbol (“hashtag”) in the bar of options at the top.

waitUntil is pretty much like doing this:


while(!condition) //Keeps on looping while the condition is not yet met
  {}