# Program for holding a button to gradually increase motor speed

Hi guys. This should be a relatively simple problem to solve, but it’s been a while since I’ve used coding syntax/logic other than the basic if statements, so I’m rusty.

All I want to do is write a program so that if I hold down a button, the motor speed increases, and if I hold down another, the motor speed decreases.

Here’s my thinking:

An integer variable, v, is initially equal to 0.
When button L1 is pressed, the motor spins at 10 RPM.
Then the variable, v, is increased by 1.
The system waits 20 milliseconds and then checks again for any button presses, for which it’ll increase/decrease v by 1 and do everything again.

I get no errors when compiling (using VCS) but it doesn’t work on the robot.

Any help is very much appreciated. Thank you!

``````
while (true){

int v = 0;
if (Controller1.ButtonL1.pressing()){
Flywheel1.setVelocity((10+v),velocityUnits::rpm);
Flywheel2.setVelocity((10+v),velocityUnits::rpm);
v++;
}

else if (Controller1.ButtonL2.pressing()){
Flywheel1.setVelocity((10-v),velocityUnits::rpm);
Flywheel2.setVelocity((10-v),velocityUnits::rpm);
v--;
}

}

``````

First and importantly, move

int v = 0;

outside of the while loop. Right now set it to 0, then you might or might not change it to -1 or +1. Then you reset it to 0. And this repeats. So you’re never going to really make any progress.

Second, and just as importantly, motor.setVelocity() doesn’t do what you think. Use motor.spin() instead. motor.setVelocity() sets a velocity value for later use, but it does not actually make the motor spin at that velocity. Another command will be needed to get the motor to actually use that value.

Finally, reconsider what you’re doing with that (10-v). My worry is that your buttons get reversed if you hit L2 first. That’s because v will be negative, and adding/subtracting that negative will work the other way around. Of course, that’s a relatively small issue. It just means you don’t know which button will speed up or slow down the flywheel until you’ve made your initial button press; and later on, if you return v to 0, you’ll get to the same point again. For example, I would rather start v at 10 and increase or decrease it with those buttons, always spinning the motor at v, though you would want to make sure v doesn’t drop below 0.

Hopefully this works:

``````
int v = 0;
int valueAdd = 10;//Increase or Decrease this Value Here

while (true){

if (Controller1.ButtonL1.pressing())
else if (Controller1.ButtonL1.pressing())

Flywheel1.setVelocity((v),velocityUnits::rpm);
Flywheel2.setVelocity((v),velocityUnits::rpm);

}

``````

Are what we’re doing a number stacking program? I used one of those to code a CR servo in FTC to act as a motor. It was pretty cool, but still might’ve worked better with just a motor

Oh, also, it’s good to cap v. Otherwise you might hold down much longer than needed to reach full speed and then find it seemingly not responding to slowing down for a while.

Still won’t. You want motor.spin, not motor.setVelocity. Here is the API statement about motor.setVelocity (my emphasis):

You did fix much of the rest of what I said was problematic, though.

Ahh, my apologies. I haven’t used VCS C++ in a while so I forgot a little bit about the velocity, speed, etc.
Is this correct?

``````
int v = 0;
int valueAdd = 10;//Increase or Decrease this Value Here
int rpmCap = 600;//The max RPM the motor can reach
while (true){

//Assign adding and subtracting of speed
if (Controller1.ButtonL1.pressing())
else if (Controller1.ButtonL2.pressing())

//Min and Max RPM Cap
if(v < 0) v = 0;
if(v > rpmCap) v = rpmCap;

//Apply v to the motors
Flywheel1.spin(directionType::fwd, (v), velocityUnits::rpm);
Flywheel2.spin(directionType::fwd, (v), velocityUnits::rpm);

}
``````

Yes, that looks good now.

Wow thanks so much guys! This helped me a lot!

@[TVA]Connor You might want to edit the else if to check L2

Good point, my apologies XD Thank you

So I took the code and improved it a bit to work better with my robot. I added a 200ms sleep after assigning the new v to the buttons, because otherwise it would instantly go to max speed, and moved the motor.spin functions inside of the while(true) loop to it would actually run

I also added some code to report the launch speed to the brain screen, taking into consideration the gear reduction.

It looks like this now:

``````
#include "robot-config.h"

int main() {

int v = 0;
int valueAdd = 10; //Abs value of each speed increment/decrement
int rpmCap = 201; //Abs value of the max speed the motor reaches
int reduction = (84/36)^3; //Gear reduction of flywheels

while (true){

//Assign addition or subtraction of RPM to buttons, which occurs every 200ms
if (Controller1.ButtonL1.pressing()){
}
else if (Controller1.ButtonL2.pressing()){
}

//Apply speed caps
if (v < 0) v = 0;
if (v > rpmCap) v = rpmCap;

//Apply v to motor speed
Flywheel1.spin(directionType::fwd, v, velocityUnits::rpm);
Flywheel2.spin(directionType::fwd, v, velocityUnits::rpm);

//Report actual launching speed to brain screen
Brain.Screen.setPenColor(color::white);
Brain.Screen.setFont(fontType::prop60);
Brain.Screen.setOrigin(1,80);
Brain.Screen.print("Launch speed = %3d RPM",  v*reduction);
Brain.Screen.clearLine();
}

}

``````

Hopefully this can help other people trying to do the same thing. Thanks for the help again.

valueAdd should be the value that you modify to change how quick the flywheel speed goes up. I would suggest not telling it to sleep for 200 miliseconds because you will have latency issues which is no bueno. Instead modify the valueAdd variable from 10 to a value less. Don’t do negative valueAdd or it wouldn’t work, instead call valueAdd and v a double and use decimals:

``````
#include "robot-config.h"

int main() {

double v = 0;
double valueAdd = 0.1; //Double value of each speed increment/decrement
int rpmCap = 201; //Abs value of the max speed the motor reaches
int reduction = (84/36)^3; //Gear reduction of flywheels

while (true){

//Assign addition or subtraction of RPM to buttons, which occurs every 200ms
if (Controller1.ButtonL1.pressing()){
}
else if (Controller1.ButtonL2.pressing()){
}

//Apply speed caps
if (v < 0) v = 0;
if (v > rpmCap) v = rpmCap;

//Apply v to motor speed
Flywheel1.spin(directionType::fwd, v, velocityUnits::rpm);
Flywheel2.spin(directionType::fwd, v, velocityUnits::rpm);

//Report actual launching speed to brain screen
Brain.Screen.setPenColor(color::white);
Brain.Screen.setFont(fontType::prop60);
Brain.Screen.setOrigin(1,80);
Brain.Screen.print("Launch speed = %3d RPM",  v*reduction);
Brain.Screen.clearLine();
}