I have read several ways to toggle buttons and they all seem more complicated than necessary. (By toggle i mean that if you push a button, it will set your motors to a specific value, and you do not need to hold it) I have two different sections in my code, and cannot seem to figure out why one set of buttons needs to be held down for the code to run, and the other set of buttons you merely need to push it once and it will run. (they both have to do with my launcher and setting the speed)
With this section of code, when i press 7U, 7D, 7L, or 7R it will set the launcher to a certain speed. i only need to press it once and it will be permanently set
if (vexRT[Btn7L])
{
launcher = 70;
} else if (vexRT[Btn7D])
{
launcher = 50;
} else if (vexRT[Btn7U])
{
launcher = 90;
} else if (vexRT[Btn7R])
{
launcher = 0;
}
But with this set of code it will only set the launcher to the given speed if i hold down the button
The second set of code is from the thread about using RobotC’s integrated PID and this is Jpearmans code slightly modified. Can anyone explain to me whey for one it has to be held, for the other it only has to be pushed? does it have to do with the == sign at all?
Nothing to do with the == comparison, there must be somewhere else in the code you send a value to motor port7 ] perhaps. Send me everything in a private conversation if you want.
ok first, you said you couldn’t find a simple toggle code, here it is
bool isOn;
if(Button == 1){
if(isOn == true){
Turn motor off
isOn = false
}
if(isOn == false){
Turn Motor on
isOn = true;
}
}
my best guess as to why the second bit of code only runs when you hold it is that somewhere else in the code you set port 7 to zero, or maybe it has something to do with a weird things about motor powers/ports
my solution would be to use each of the if and else if loops to set a variable, lets call it power
then at the end of the loops (but outside all the else if loops) set port 7 to power, so here is your code re-written this way, check and see if it works
I am confused because most beginners seem to have a hard time programming toggle buttons. Is the first set of code i posted the correct manner of programming a toggle button? i do not understand the need of a bool if my first set of code works as a good toggle
I would not describe you first code as a “toggle code” in my mind, a toggle code, is 1 button, that when you press it once, it turns something on, then when you press it again it turns it off.
what you have is 4 separate buttons doing 4 separate things, ideally, the second set of code I posted should do the same thing as the first set of code you posted
This will fail if you hold the button down for longer than the loop’s cycle time. It’s good practice to make the loop’s cycle time as short as you can get away with so that you minimise latency, so you might have to push the button very quickly. This is better, because you won’t accidentally turn something on and back off by holding the button down for too long:
#define PRESSED = 1;
#define NOT_PRESSED = 0;
bool isOn;
int Button;
int prevButton;
while (1)
{
Button = VexRT[Btn5U];
if(Button == PRESSED && prevButton == NOT_PRESSED) {
if(isOn == true ){
// Turn motor off
isOn = false
}
else { // isOn == false
// Turn Motor on
isOn = true;
}
}
prevButton = Button;
}
This isn’t more complicated than necessary - this is just what you have to do to make a toggle button. If it seems more complicated than you expected, that’s because there isn’t a function already made to do it for you. You could make a function that would make things a lot simpler, for example:
while (1)
{
ToggleControl('Btn5U', 'port3', &isOn, &prevButton);
}
If you do that then from that point on you will be able to set up toggle buttons very easily. It just involves a little work up front.
that’s actually a smart way of doing in oliver, thanks for that. before this what I had always done is just put a wait time at the end of the loop for a quarter second or so
There is actually a problem with the way you programmed it here. Currently if IsOn is true when the button is pressed you will immediately change it to false and then change it to true in the same iteration of the loop. I would suggest
IsOn=! IsOn
If(isOn)
Stop motor
If(! IsOn)
Start motor
Or an else if to ensure only one of the toggle ifs can run per loop.
Basically the different buttons set the int toggleNum to different values, the code checks which value it is set to and sets the motor speed according to that.
Hope that helps.
This isn’t a toggle, because a toggle is something that can change between two states. For example, you could use a button to toggle between having the flywheels on or off. This isn’t a toggle because you are using 4 different buttons to change between 4 different speeds. I just wanted to clear that up; other than that the code looks fine.
Ya, I would agree that it is not a button “toggle” for that you would use a bool, but I believe that ggurta’s original purpose for this thread was to find out how to get 4 different speed out of the buttons.
Although I do know this is not a single button toggle, one thing you could do is use a switch statement to select which piece of code to run based on the value of toggleNum.
task main()
{
int toggleNum = 0;
while(true)
{
// code for assigning the num. of the speed
if(vexRT[Btn7D] == 1)
toggleNum = 0;
else if(vexRT[Btn7U] == 1)
toggleNum = 1;
else if(vexRT[Btn7L] == 1)
toggleNum = 2;
else if(vexRT[Btn7R] == 1)
toggleNum = 3;
// code for setting speed of motors
switch(toggleNum)
{
// If the variable = 0
case 0:
motor[launchL] = 0;
motor[launchR] = 0;
break;
// If the variable = 1
case 1:
motor[launchL] = 60;
motor[launchR] = 60;
break;
// If the variable = 2
case 2:
motor[launchL] = 100;
motor[launchR] = 100;
break;
// If the variable = 3
case 3:
motor[launchL] = 127;
motor[launchR] = 127;
break;
// In all other cases
default:
motor[launchL] = 0;
motor[launchR] = 0;
break;
}
}
#TheMoreYouKnow
Also, the difference between a hold and ‘toggle’ switch, that is when using multiple buttons is that a toggle switch is constructed with a else that tells the motors to stop if the button(s) controlling the speed are not pressed.
while(true)
{
if(vexRT[FlyWheelButton] == 1 )
motor[FlywheelLauncher] = 64;
else if(vexRT[FlywheelButton2] = 1)
motor[FlwyheelLauncher] = 127;
// When neither of the above buttons are pressed set speed to 0
else
motor[FlywheelLauncher] = 0;
}
But a hold switch is missing that else statement. It is usually replaced with a else if statement that checks for a button press and stops the motor.