I need help writing some code that uses, 8U, and 8D buttons on the controller to control the 4 motors on my flywheel’s speed. I have attempted to do this many times but have failed all of them. Please Help me with writing this code, for it wi Segway my team into something else that is automated.
Personally I would make the flywheel speed be controlled by a partner joystick. This takes off some of the pressure on the driver and have another person be focused on the shooting. How to make the speed change with buttons, however, I don’t know.
I would set a speed variable and change it using if statements for the buttons. Then, read the variable using an if statement and set the output speed. Remember to add "else"s, too.
At least, this is the only way I can think of. There may be a simpler way out there (cue coding posts)
Some pseudocode would look something like this:
int speed = 0;
while true{
if (buttonA == 1){
speed += 10;
}
if (buttonB == 1){
speed -= 10;
}
if(speed < -127){
speed = -127;
}
if(speed > 127){
speed = 127;
}
flywheel motors = speed;
}
You may want the buttons to raise or lower an increment/cycle through against target RPMs and let the background control task control the motor speed.
Kind of like people do with lift control targets on buttons but setting an RPM target instead. I would think a tweak up/down to the target may be more beneficial in this game.
If you are trying to do a system like this you will need to store the last button values.
if(buttonB&&!lastButtonB) should ensure that each button press only is read once. Remember the robot cycles through loops quickly so it could have read 1 button pressed for a tenth of a second as hundreds of times to increment the speed. Also for any amount of fine control speed being increased by 10 probably would not be accurate enough. You could easily have some buttons that move a large amount and other buttons move a small amount.
Good point. Make sure you don’t increment every time you see this and make sure enough time has passed. Otherwise what you may consider fine control may be course control because it executed the code 5-6 times in the blink of an eye.
I built some code recently with the intention of doing this but found that while the flywheel was spinning the change in speed was… well it didn’t really work. So instead I suggest that you create a button toggle to set different speeds while the flywheel is running, or you have dedicated buttons to start the flywheel at different speeds.
What you asked for well… You could create a variable that holds you motor speed then just reduce or increase it using buttons. Something like this:
(psuedocode)
int FlySpeed =127;
bool ButtonCurrentlyHeldDown
while(true)
{
motor[flywheelMotors] = FlySpeed
if(vexRT[Btn8U] == false && ButtonCurrentlyHeldDown == true) //on release
{ ButtonCurrentlyHeldDown == false }
if(vexRT[Btn8D] == false && ButtonCurrentlyHeldDown == true) //on release
{ ButtonCurrentlyHeldDown == false }
if(vexRT[Btn8U] == true && ButtonCurrentlyHeldDown == false)
{
FlySpeed += 5 //You could add macro and micro adjustment buttons
ButtonCurrentlyHeldDown = true;
}
if(vexRT[Btn8D] == true ** ButtonCurrentlyHeldDown == false)
{
FlySpeed -= 5
ButtonCurentlyHeldDown = true;
}
if( FlySpeed > 127 ) { FlySpeed = 127; }
if( FlySpeed < -127) { FlySpeed = -127; }
}
EDIT: read other post by Giraffes and updated code…
That wouldnt work (for I have tested it) what would happen is the bool would keep switching from true and false so fast that it would skit right past it. So how would I fix that?
Have the program save the button value everytime it iterates. Only count a button press if currently it is held down and last iteration it was not.
If(button&&!last button)
That will only happen when you switch from not pressed to pressed.
could you elaborate a little more on that
I think tabour might mean that each time the loop runs, you are going to save to a variable whether the button is held down. Then on each loop check if the button is currently held down & whether the button was held down previously. If it was held down previously then do nothing, if it was not held down previously then increase the speed. Then the loop will only run once when you press a button.