RobotC Programming Help: Move motor after releasing button

Hey, I was wondering if anyone could help me with my RobotC code. I am trying to program my controller so I am able to hold down the Btn6D (a bumper button) to move a motor and then upon releasing it to move the motor in the opposite direction but only for a second or two. I was previously using armControl(), but I don’t want to use this anymore because I just want to have 1 button. My current code sort of works, it waits for the user to hold down the bumper originally and then upon releasing the button it moves the motor in the opposite direction, but indefinitely instead of just for a couple of seconds.
Here is the code:

task main()
{
    repeat(forever)
     {
           if(vexRT[Btn6D]--1)
            {
                 while(vexRT[Btn6D]==1)
                   {
                       motor[clampmotor] = 50;
 
                    }

                   {
                     motor[clampmotor] = -50;
                   }
             }
      }
}

I am also unsure if it is a good idea to use the repeat(forever) loop in my actual competition code, because I also have a tank control line and a arm control line that need to be constantly running. I am very new to robotC and the forums, so I may be doing something obviously wrong in the code or how I am asking for help.

The repeat(forever) just beneath the task main should be fine. You need that for the teleop.

However, if you have a tank control and arm control that need to be constantly running then I would watch out for the while(vexRT[Btn6D]==1) will cause problems because all of the code in the same task are sequential. That is if you are holding down Btn6D then you won’t be able to control the joystick if the joystick code is also part of the repeat(forever) loop in the task main().

You may want to run a separate task and put all of the Btn6D code there, so you can control your clampmotor, tank control, arm control at the same time.

Thanks for the response, so if I wanted to do two tasks would it look something like this?

task usercontrol()
{
 task drivetrain()
 {
    while (true)
      { 
          tankControl(Ch3, Ch2, 20);
          armControl(linearmotor, Btn5U, Btn5D, 70);
       }
 }

task bumper()
{
    repeat(forever)
     {
         if(vexRT[Btn6D]==1)
         {
               while(vexRT[Btn6D]==1)
               {
                  motor[clampmotor] == -50;
                }

               {
                 motor[clampmotor] = 50;
                 wait(3, seconds);
                 motor[clampmotor] = 0;
                }
          }
      }
 }

So would the task drivetrain and bumper run concurrently? Also, I added a wait line, would that make it so it leaves the motor power at 50 for 3 seconds and then turns it off?

Yup. Under your usercontrol() task, I don’t think you can create the drivetrain() task inside the usercontrol() task. You can call your bumper() task from the usercontrol() task. I could be wrong but I think the tasks you create, like task bumper(), should be created above the usercontrol() task.

task usercontrol()
{
 startTask(bumper);

    while (true)
      { 
          tankControl(Ch3, Ch2, 20);
          armControl(linearmotor, Btn5U, Btn5D, 70);
       }
}
 

task bumper()
{
    repeat(forever)
     {
         if(vexRT[Btn6D]==1)
         {
               while(vexRT[Btn6D]==1)
               {
                  motor[clampmotor] == -50;
                }

               {
                 motor[clampmotor] = 50;
                 wait(3, seconds);
                 motor[clampmotor] = 0;
                }
          }
      }
 }

Yup, setting the wait time for 3 seconds then setting the motor speed to 0 will let it run for 3 seconds then have it turn itself off.