hello, i wanted to clarify whether or not this code would work before running it and possibly damaging the bot. the attempt here is to make it so i can use the 6U vexRT button as an on/of switch for the headlights. instead of using two buttons, one to turn lights on and one to turn lights off, i want a way to make a single button press turn the lights on and keep them on until i press the same button again, which would turn it off. here is my code
int buttonToggleState = 0;
int buttonPressed = 0;
while(1)
{
// check for a button press only if we are not already pressed.
if(vexRT[Btn6U]==1)
{
if(!buttonPressed)
{
// change the toggle state
buttonToggleState = 1 - buttonToggleState;
// Note the button is pressed
buttonPressed = 1;
}
}
else
{
// the button is not pressed
buttonPressed = 0;
}
// Now do something with our toggle flag
if(buttonToggleState)
motor[port9] = 127;
else
motor[port9] = 0;
}
}
I don’t think you’ll hurt anything on your robot to try it. The only thing I see as a potential problem with your code is you have no wait delay in the loop, so a single button press might switch the state several times if it is held down long enough for the loop to return on itself.
It also is probably more complex then it needs to be. This is just pseudocode, but something like this I think would also work:
toggle = 0
While loop:
if buttonbeingpressed:
if toggle == 0:
turn on light
set toggle to 1
else:
turn off light
set toggle to 0
wait 100 ms
Neither of these solutions is valid. The button press would usually last far longer than a mere 100 ms, and, thus, these suggestions would cycle the light on and off repeatedly until the button is released and not even guarantee that the light’s final state is different from the initial state.
@nnolte The programs posted by @LauraDarkez318 and @majorjoel2 do not rely on timing at all to operate. They employ definitive, time-insensitive methods of tracking the state of the button across iterations of the infinite loop.
all im trying to accomplish is the idea that if i tap the button once, light turns on and stays on. tap it when the lights are on and itll turn them off. like the clicking buttons on things like children’s motorized toys. press it once, it clicks, the toy turns on. click it again and it turns off.
But like @Barin pointed out the button could be pressed for more than 100ms which would keep toggling it while you hold it down
but if you do something like this
it doesn’t matter how long you press the button for
bool lightsOn = false, pressed = false;
while (true){
if (vexRT[Btn6U] == 1){
if (!pressed){//if button has not previously been pressed
lightsOn = !lightsOn;//sets lightsOn to the opposite of itself
pressed = true;//button has been pressed
}
}
if (vexRT[Btn6U] == 0){ pressed = false; }//button has been released
if (lightsOn){//if lightsOn is true
//Turn lights on
}
else{//if lightsOn is false
//Turn lights off
}
}
Let’s first note that @LauraDarkez318 's code is good as is and is better than some of these other suggestions, as @Barin said. I’ll explain why more clearly:
This is not actually true. Sure, there is no delay, so if you’re running more than this you might eat processor time that could be devoted to other tasks. But for just this piece of code, it works properly. What you’ve missed is the two int’s instead of one. Here are the possibilities:
motor stopped, button becomes pressed:
buttonToggleState = 0, buttonPressed = 0
!buttonPressed = true
both become = 1
motor set to 127 (turns on)
motor running, button stays pressed:
buttonToggleState = 1, buttonPressed = 1
!buttonPressed = false
motor set to 127 (stays on)
motor running, button is released:
buttonToggleState = 1, buttonPressed = 1
buttonPressed becomes = 0
motor set to 127 (stays on)
motor running, button becomes pressed:
buttonToggleState = 1, buttonPressed = 0
!buttonPressed = true
buttonToggleState becomes = 0, buttonPressed becomes = 1
motor set to 0 (turns off)
motor stopped, button stays pressed
buttonToggleState = 0, buttonPressed = 1
!buttonPressed = false
motor set to 0 (stays off)
motor stopped, button is released
buttonToggleState = 0, buttonPressed = 1
buttonPressed becomes = 0
motor set to 0 (stays off)
Honestly, use your own code. Unless there is a little typo with a bracket or a semicolon or something which I missed, it will work perfectly. The good thing about this is you know why you wrote it that way, so you understand it well. It’s not bad to understand someone else’s code, but yours is better than half the replies.
which code is better to use in terms of the processor? im going to be running a variety of tasks and i dont want to burn anything out, especially not the processor in the microcontroller cortex