HELP! How do I start a timer when a task starts? OR how do I make a task stop even if the limit switch is still held down

Hello, I’m building something for competition and I’m somewhat inexperienced new to RobotC. (I’m using Vex 2.0 Cortex with Natural Language PLTW. )

One of my actions was a lever that holds down a switch, the problem is that it makes the task go on forever. I tried fixing this by putting a condition in the while loop but it just breaks the code. So now, I tried making a timer that waits about 90 seconds although I think it’s pretty risky, since I only have a tiny amount of time to start it and I’m not sure if it’s supposed to be ready before competition.

Any help? I believe the only options is either figuring out how to end a task if the limit switch hasn’t been released or making a timer AFTER the task was started. Here’s my code:

task slider()
{
startMotor(Motor1, -15);
wait(34);
startMotor(Motor1, 15);
wait(32);
stopMotor(Motor1);
}
task fan()
{
startMotor(Motor2,55);
startMotor(Motor3,55);
wait(5);
stopMotor(Motor2);
stopMotor(Motor3);
}

task checkSensors()
{
repeat(forever)
{
if (SensorValue(Switch1))
{
startTask(slider);
}
if (SensorValue(Switch2))
{
startTask(fan);
}
}
}
task main()
{
startTask(checkSensors);
wait(80);
stopTask(checkSensors);
}

Sorry if it’s not inside a box and that it’s not a image, I just signed up. Again. I would really appreciate any help.

I would suggest making it set a variable to make it not repeat, so it sets the variable to 1 when the lever is pressed, then sets it to 0 when it is not pressed.

Make sure you know that cortex is NOT vex legal in VRC any more, that was changed at the beginning of this competition

ah, okay thank you! The competition I’m going to is different so it’ll be fine (hopefully)

If you do want to do it with a timer, you can set a variable to the time before the function, and update the time during the function, then compare (timeAtStart- currentTime) >= wanted time

i’ll try this, thank you!