changing motor speeds

We know that as we move closer to the goal our robot cannot continue to shoot balls out at the same speed or angle. We were wondering if there was anyway to create a program where a button press would allow the motor to increase or decrease in speed? As of now we are kind of short some motors so we are looking to not add an adjustable shooter to increase or decrease the angle. I use Robot.C 4.0 to program.

Here are a couple of example of how you could do this.

The first example just dedicates a button to a motor speed, Btn8U is 100, Btn8L is 75 etc.

#pragma config(Motor,  port2,           flyWheel,      tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
    int     motorSpeed = 0;
    
    while(1)
        {
        // Press one of four buttons to set motor speed
        if( vexRT Btn8U ] )
            motorSpeed = 100;
        if( vexRT Btn8L ] )
            motorSpeed = 75;
        if( vexRT Btn8R ] )
            motorSpeed = 50;
        if( vexRT Btn8D ] )
            motorSpeed = 0;

        // Send to the motor
        motor flyWheel ] = motorSpeed;
        
        // Don't hog the cpu :)
        wait1Msec( 25 );
        }
}

The second example uses two buttons to increase or decrease the motor speed. When doing this you need to be careful to only detect the “press” and then wait for a release before detecting the next “press”. You could simply wait in a loop for that release but then the rest of your driver control code would not run (unless you used a second task for this code). This example uses a flag (just a variable that can be true or false) to save the fact that a button was pressed and check each time around the while loop to see if it has been released.


#pragma config(Motor,  port2,           flyWheel,      tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
    int     motorSpeed = 0;
    bool    notPressed = true; // button is not pressed

    while(1)
        {
        // Initial speed is 100
        if( vexRT Btn8U ] )
            motorSpeed = 100;
        // Stop
        if( vexRT Btn8D ] )
            motorSpeed = 0;

        // Detect 8L press
        if( vexRT Btn8L ] && notPressed ) {
            // Slower
            motorSpeed = motorSpeed - 10;
            notPressed = false;
            }

        // Detect 8R press
        if( vexRT Btn8R ] && notPressed ) {
            // Faster
            motorSpeed = motorSpeed + 10;
            notPressed = false;
            }
            
        // if both button are "not pressed" then set flag true
        if( !vexRT Btn8L ] && !vexRT Btn8R ] )
            notPressed = true;
            
        // Limit the speed, we must not go negative or too fast
        if(motorSpeed > 100)
            motorSpeed = 100;
        if(motorSpeed < 0)
            motorSpeed = 0;
    
        // Send to the motor
        motor flyWheel ] = motorSpeed;
        
        // Don't hog the cpu :)
        wait1Msec( 25 );
        }
}