Newbie Code Question

Hi, I am trying to write a program to do this:

  1. Read a button press
  2. change a motor speed value
  3. after X.XX seconds, make the motor speed go to the original value

My first thought is to create the delay with the timer so, on the button press

void powerUp()
{
currentSpeed= turboSpeed;
clearTimer(T2);
}

then in the main task, check back to see if it is time to go back to normal speed
void powerDown()
{
if (time1[T2]>500)
{
currentSpeed = targetSpeed;
}
}

I realize this is sloppy pseudocode. I am just trying to figure out if the timer is an okay way to create delay I am looking for, if there are any drawbacks to using the timer, and if there is a better way of achieving this delay without tying up the program like a wait1Msec() command.

Thanks!

The code is very simple. Adjust the buttons, timers, and speed values to your liking. The code would be the following:


void powerUp()
{
if (vexRT[Btn7U] == 1
{
motor[leftWheel] = 127;
motor[rightWheel] = 127;
wait1Msec(5000);
break;

The break is so you can re-enter the main driver control. If the code is for an autonomous, replace the break with whatever you want to happen after the speed up.

Hope this helps!:slight_smile:

Okay so just to be clear. He said he wanted it to be non blocking. Specifically he said “don’t want to tie up code with wait1ms” and that is exactly what you did.

To the OP. Using a timer is a reasonable way to do it. Or if you felt like it. Another task that’s job is controlling the speed could use a sleep (5000) without messing up the rest of the user control.

Multitasking is a thing in programming that could go wrong if you use it incorrectly. Just stay on the correct side of it and you should be fine.

To multitask, you would make a Task, much like you would a Void (and the same number of letters), although the function is different (ROBOTC is a nice explanation)([

I’m not using a task for this, but you could very well do so (at least, I believe). I just put this in the remote control loop because I figured the driver would only be pressing it for a few hundredths or tenths of a second, which is too short to do anything else anyway.](https://vexforum.com/t/robotc-programming-tips/19718/1&page=3 Post #23 and on show explanations and codes).)


void powerUp()
{
    currentSpeed= turboSpeed;
    clearTimer(T2);
}

then in the main task, check back to see if it is time to go back to normal speed
void powerDown()
{
    if (time1[T2]>500)
    {
        currentSpeed = targetSpeed;
    }
}

This code actually should work fine, I mean if it ain’t broke dont fix it egh :slight_smile:

I recently was working on some robotC code with a team (I was determined to do it all in the main task) and I ended up doing something like this:



int iteration;
int turboSpeed = 127;
int normalSpeed = 80;
int waitTime = 10;
int iterationAmount = 20;

// If You Press a button and the iterations are under iterationAmount 
if( vexRT[HIGH_SPEED_BUTTON] == 1 && iteration < iterationAmount )
{
        // Set the motorSpeed to turbo
	fw_setMotorSpeed( turboSpeed );
	iteration++;
}
// If it has been iterationAmount of iterations then
else if( iteration == iterationAmount )
{
        //  set the motor speed to normal
        fw_setMotorSpeed( normalSpeed );
}

// When you release the high speed button then reset the loop
// This stops the if control statement from repeating if you hold down the button.
if( vexRT[HIGH_SPEED_BUTTON] == 0 )
{
        iteration = 0
}

// Do your other code in your one loop HERE
// Drive Motors and other stuff

wait1Msec(waitTime);

Sorry I just wrote that out quickly, the theory behind it is that you run it for say 20 iterations as 20 Msecs wait time every iteration of the loop by the time its iteration 20 it will be (20 iterations * 20 waitTime =) 400Msecs. You can adjust the wait time and iteration amount using the variables.

Thanks for all your help! got it working just now!