multitasking code - not comp

RobotC ---- Why won’t this work? It starts main task and ends immediately. Doesn’t have time to do anything other than turn on the green LED.

task normal()
{
while(1==1)
{
turnLEDOff(yellow);
turnLEDOff(red);

	turnLEDOn(green);
	wait(2);
	turnLEDOn(yellow);
	wait(1);
	turnLEDOn(red);
	wait(2);
}

}

task interrupt()
{
while(1==1)
{
if (SensorValue[emergency]==1)
{
stopTask(normal);

		if (SensorValue[green]==1)
		{
			turnLEDOff(green);

			turnLEDOn(yellow);
			wait(1);
			turnLEDOn(red);
			untilRelease(emergency);
			wait(2);
			startTask(normal);
		}

		if (SensorValue[yellow]==1)
		{
			wait(1);
			turnLEDOn(red);
			untilRelease(emergency);
			startTask(normal);
		}

		if (SensorValue[yellow]==1)
		{
			wait(1);
			turnLEDOn(red);
			untilRelease(emergency);
			startTask(normal);
		}
	}
}

}

task main()
{
startTask(normal);
startTask(interrupt);
}

You’re letting main() finish. When task main() finishes, the program exits. You need to have some sort of infinite loop to keep main() from ending.

I figured that to be the case but if it is in a loop won’t it just keep “restarting” it over and over?

You don’t need to repeat the whole thing. You might also consider just making task main start up task interrupt followed by the entirety of task normal as part of task main.

You just need a loop somewhere; it doesn’t need to be around the startTask calls. Jusstputtibg a while(true) loop after them containing a delay(20); would work.

Also I’m assuming this code is just an example for what you are trying to do; it is a waste to have task main do absolutely nothing.

@sazrocks what do you recommend? this is a little foreign to me so I appreciate your help.

@jonathans you have to add a wait period in your main task like this.


task main ()
{
startTask(normal);
startTask(interrupt); 
while(true)
{
wait1MSec(1);
}
}

@josh_siegel

That definitely worked. And it makes perfect sense. @sazrocks just makes it sound like I can be more efficient.