Coding Help Needed

My team and I need help with programming, here. We have our double flywheel motors running at a speed lower than 127. The code looks like this:

motor[port2] = vexRT[Ch2]*52/127;

We have it coded to make shots from the starting tiles, but the battery gets depleted fast. Is there a way that I can program a button to up motor 2’s speed by 10 power every time I press the button? That way when we start to undershoot the balls, we can hit the button to get the motors to the right power. Any information about this will help. Thank you!

Yep you could create a variable (say called “additionalSpeed”) and change that line to this:

motor[port2] = vexRT[Ch2]*52/127 + additionalSpeed;

additionalSpeed should initially be set to 0. In the main loop you could have something that checks if a button is pressed, and if so, it increases additionalSpeed by 10. I would recommend doing this in two steps though:

  1. Check if button is pressed
  2. After that happens, check if button gets released
  3. Then increment additionalSpeed
    Because otherwise, if you hold down the button, it would increase additionalSpeed by more than you need.

Thank you! This helps a lot. How would I put that into code? I assume there is a loop somewhere. And we were also thinking of something like this with our flywheel code:

motor[port2] = (vexRT[Ch2]*52/127 + vexRT[Ch3]*47/127);

We just tested this code and it works wonderfully for closer shots. So if we use this code, which we are most likely doing, could we still do what you were suggesting?

You’re welcome! So all your motor code is probably in some sort of while loop like

while (true) {

Basically, this makes whatever’s inside it run over and over again forever. In case you’re not familiar with while loops, they run whatever’s inside the brackets as long as the condition (the thing in between the parentheses) is true. Since the condition here is explicitly set to “true”, everything inside the brackets just runs over and over again. This is helpful since the robot needs to be constantly checking the controller for changes like joysticks being pushed or buttons being pressed.

So outside the loop you need to declare those two variables (outside because if it was in the loop, they would just keep getting reset which won’t help):

boolean buttonPressed = false;
int additionalSpeed = 0;

And inside the loop you should have something like this:

if (vexRT[Btn7U] == 1) { // If button is pressed
   buttonPressed = true;
if (buttonPressed == true && vexRT[Btn7U] == 0) { // If button was pressed down, AND is now released
   buttonPressed = false;
   additionalSpeed += 10;

And yep it should still work with that new code.

Thank you very much for your help. My teammates and I appreciate it. We are doing some practice right now, but then we will try to use this code and see how well it works out. I will keep you updated.

IT WORKED!! THANK YOU SO MUCH!!! We do appreciate this very much. The only thing is once I pressed the buttons, and saw that the speed was going up, I let go of the joystick controlling the flywheels, and they were still going lol. I fixed that by having the same thing happen to Btn7D. Thank you again for all of the help. I wish you good luck for this season.

Okay, My idea didn’t work. It made it faster when I tried hitting the button to make it slower. On top of that, when I pressed it, all of the motors stopped suddenly and returned to full speed (or faster). I used:

if (buttonPressed == true && vexRT[Btn7D] == 0)
buttonPressed = false;
additionalSpeed += -10;

I see why it failed, but i don’t know how to make it slow down when needed. We can’t turn off the cortex while the flywheels are running because that will damage the internal gearing of all of the motors. Ideas? (I swear this will be the last problem we have with this lol)

I think making a separate buttonPressed variable for the decreasing button would fix it (initialize buttonPressed2 or something like that and in the code for the button to decrease, change every buttonPressed to buttonPressed2). Let me know if that works!

Edit: to clarify, you would have for each button (7D and 7U) two if statements. In total you would have 4 if statements about all the buttons.(one checking whether 7U is pressed, one checking if 7D is pressed, one checking if buttonPressed is true and 7U is released, and another checking whether buttonPressed2 is true and 7D is released)

We played around with it, and we tried many different things, but this is what ended up working:

motor[port2] = (vexRT[Ch2]*52/127 + additionalSpeed + vexRT[Ch3]*47/127 + additionalSpeed);
motor[port3] = (vexRT[Ch2]*66/127 + additionalSpeed + vexRT[Ch3]*52/127 + additionalSpeed );
motor[port4] = (vexRT[Ch2]*52/127 + additionalSpeed + vexRT[Ch3]*47/127 + additionalSpeed);
motor[port5] = (vexRT[Ch2]*66/127 + additionalSpeed + vexRT[Ch3]*52/127 + additionalSpeed);
if (vexRT[Btn6U] == 1)
buttonPressed = true;
if (buttonPressed == true && vexRT[Btn6U] == 0)
buttonPressed = false;
additionalSpeed += 2;

	if (vexRT[Btn6D] == 1) 
		buttonPressed2 = true;
	if (buttonPressed2 == true && vexRT[Btn6D] == 0) 
		  buttonPressed2 = false;
		  additionalSpeed += -2;

Thank you for the help :slight_smile:

Glad it worked out, and no problem!