Does anyone know how to write code for a toggle button. It sounds like a good idea in my head but I have yet to actually figure out how to code this. This is my idea—— press a button (without holding it down) and the lift (dr4-dr6) goes to the first preset height. If I press the button again it goes to the second preset height… and so on and so on. I also want the exact same thing for going downwards. So that whenever I press the up btn it goes to first cone posiotion, second cone position, third cone position. Etc etc. I’m using robotC btw. If anyone could help that would be a great pleasure to me thx !
While I haven’t actually tested this yet, I used to think you could do it with a switch case. Kind of like this:
int preset_height = 0;
while(true)
{
if(button == pressed)
{
preset_height = preset_height + 1;
switch( preset_height )
{
case 1:
//DR4B goes up to preset height
break;
case 2:
//DR4B goes up to second preset height
preset_height = 0; //reset preset_height to 0 so when you press button again it goes back to first preset height
break;
}
}
}
Again, I’m not sure if it actually works but this is what I would try first.
The toggle code would look something similar to the following. I’m not going to work on the how to get to the position you want to because there are tons of different ways to accomplish that. For choosing which height you go to, I would use an array of values and increment and decrement an index variable, but that is in no way the only way to do it.
//Flag to track whether the button is pressed down
bool upButtonPressed = false;
if(isUpButtonPressed() && !upButtonPressed){
//Set next target position here
upButtonPressed = true;
}
else if(!isUpButtonPressed()){
upButtonPressed = false;
}
The down button would work similarly.
I will try this code
Anyone know if this willnwork ^??
No, you’re going to essentially get random spots. Look at @Gallium 's code again and consider this. @Gallium 's method is good for one button. You need to make sure you only record one button press instead of many button presses when you press the button down, noting that the computer can cycle through reading that button multiple times in the time it takes the person to press and release it once. That’s what the method is doing, only identifying a press if the button was not pressed prior to it being read as pressed now. Your code, in contrast, is going to keep triggering repeatedly in the time it takes to press the one button. Whichever one you happen to end with when you release the button is where it will end up, which is essentially 100% random.
Normally I would also tend to go with @Gallium 's approach of using an array and just increasing or decreasing a index variable. Though if you’re really just toggling between two spots, the array won’t really help; that’s better with several spots.
If you want two buttons, though, I wouldn’t quite use @Gallium 's approach with the if statements, though. In that case, you only want one of two buttons to send a signal at a time so you don’t go nuts with both buttons being depressed by accident. The trick is you really don’t need that else if at the end, instead putting it at the beginning, allowing it not to block an else if for the other button:
One button toggle without else if, essentially identical to above:
//Flag to track whether the buttons is pressed down, prior to while loop
bool toggleButtonPressed = false;
//
// The following sits inside the infinite while loop.
//
if(!isToggleButtonPressed()){
toggleButtonPressed = false;
}
if(isToggleButtonPressed() && !toggleButtonPressed){
//Switch target position here
toggleButtonPressed = true;
}
Two buttons:
//Flags to track whether the buttons are pressed down, prior to while loop
bool upButtonPressed = false;
bool downButtonPressed = false;
//
// The following sits inside the infinite while loop.
//
if(!isUpButtonPressed()){
upButtonPressed = false;
}
if(!isDownButtonPressed()){
downButtonPressed = false;
}
if(isUpButtonPressed() && !upButtonPressed){
//Set next target position here
upButtonPressed = true;
}
else if(isDownButtonPressed() && !downButtonPressed){
//Set next target position here
downButtonPressed = true;
}