Losing control while running code, please help

Hi,
We keep running into a problem hoping to find some help here.
For vex IQ squared away we have a ball intake with basket. The balls are dropped from the basket using trap door type set up.

The general code to drop the balls is here:

while(true):
{
// our driver control code here such as-- setMotorSpeed(leftMotor, getJoystickValue(ChA)) , ect.

 if(getJoystickValue(BtnEDown) == 1 && getJoystickValue(BtnEUp) == 1)
 {
     setMotorTarget(backPack, 705, 100);
		delay(600);
   setMotorTarget(backPack, 400, 100);
   waitUntilMotorStop(backPack);
	}

}
The back hooks push on a lever at position 705, hold for 600ms while balls drop then the hooks move back to cube release position.

The delay is to ensure that the motor target of 705 was reached to fully push the ball release lever.

Our problem is that when ever the delay is running or the command waitUntilMotor stop is running, we loose all control of the robot. Worst of all, if the bot was moving when the command was started it continues to move- such as if the robot was turning it keeps turning until the delay or waitUntilMotorstop is complete.

Can “Tasks” help with this? can anyone tell me a short answer what the difference is between Tasks and “functions”?

Any help is appreciated!

Here’s something I posted a couple years ago.

The third example shows a task, which is what you need. In your case, you want to start the task, then continue with the rest of your program, including the code for controlling the robot movement

As hassenplug mentioned, “Task” is what you need. The reason is that your main “while(true)” loop is stuck on the delay(600) call. While the computer brain is stuck waiting for the delay(600) to come back, it will not be able to process any other code or command.

What I found is that “Task” is somewhat of an advance concept for the younger kids to grasp. The way I would explain is that think of the main loop as a person responding to the command of the controller. By creating a new “Task”, you clone yourself (or ask a friend) to take care of the new “Task” for you while you are busy doing something else.

In your case, put the delayed trap door code inside a “Task” will do what you want. This way the new friend can take care of the setMotorTarget, delay, setMotorTarget call while the main person can keep responding to new controller command without being “delayed”.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.