Kill switch

How would you program a kill switch button on the joystick?

The code I had was like this:

task EmergencyBrake()
{
	while (true)
	{
		if (vexRT[Btn7U] !=0)  
		{
			stopAllTasks();
			startTask(BackFromDead);
		}
		wait1Msec(20);
	}
	return;
}


task BackFromDead()
{
	while (true)
	{
		if (vexRT[Btn7L] !=0)  
		{
			startTask(main);
		}
		wait1Msec(20); // give other tasks a chance...
	}
	return;
}


task main()
{
	startTask(EmergencyBrake);
//etc...
}

However, i was getting compile errors,:

What am I doing wrong?

Declare the task before you start it.

Put your task BackFromDead() definition above your task EmergencyBrake() one. Another option is to use a forward declaration for task BackFromDead (does RobotC support forward declaration for tasks?).
These two options look like this:
Reorganized:


task BackFromDead()
{
	while (true)
	{
		if (vexRT[Btn7L] !=0)  
		{
			startTask(main);
		}
		wait1Msec(20); // give other tasks a chance...
	}
	return;
}


task EmergencyBrake()
{
	while (true)
	{
		if (vexRT[Btn7U] !=0)  
		{
			stopAllTasks();
			startTask(BackFromDead);
		}
		wait1Msec(20);
	}
	return;
}


task main()
{
	startTask(EmergencyBrake);
//etc...
}

Forward declaration (I’m not at a computer equipped with RobotC, or I would check forward declaration support)



task BackFromDead();

task EmergencyBrake()
{
	while (true)
	{
		if (vexRT[Btn7U] !=0)  
		{
			stopAllTasks();
			startTask(BackFromDead);
		}
		wait1Msec(20);
	}
	return;
}


task BackFromDead()
{
	while (true)
	{
		if (vexRT[Btn7L] !=0)  
		{
			startTask(main);
		}
		wait1Msec(20); // give other tasks a chance...
	}
	return;
}


task main()
{
	startTask(EmergencyBrake);
//etc...
}

@lpieroni, Both of these don’t work. Fires error “main not defined”

You would need a forward declaration of task main(). I overlooked that part, my bad. Did RobotC throw an error about the forward declaration for task BackFromDead()? If not, that will work. If it did, you might not be able to restart task main().

@lpieroni, that worked! Thank you!

No problem! I’m glad to know that forward declarations work in RobotC. That was something I learned over the summer, when I started trying to learn C++.