light blink help

im now trying to run a blink sequence when the variable is set to true. heres my code:

task Start_light()
{
	while(true)
	{
		SensorValue[dgtl1] = true;
		wait1Msec(500);
		SensorValue[dgtl1] = false;
		wait1Msec(500);
	}
}

task flash2()
{
			int buttonToggleState = 0;
    int buttonPressed = 0;

	while(1)
	{
		// check for a button press only if we are not already pressed.
		if(vexRT[Btn5U]==1)
		{
			if(!buttonPressed)
			{
				// change the toggle state
				buttonToggleState = 1 - buttonToggleState;

				// Note the button is pressed
				buttonPressed = 1;
			}
		}
		else
		{
			// the button is not pressed
			buttonPressed = 0;
		}
		// Now do something with our toggle flag
		if(buttonToggleState)
		{
			startTask(Start_light);
		}
		else
		{
			stopTask(Start_light);
			SensorValue[dgtl1] = false;
		}
	}
}

the problem is that the blink isnt working that way. it stays solid instead of activating the blink sequence

I’m just skimming with very little time. I don’t see any delay in flash2, so you could be starving the other task.

As a separate thing, 500 ms won’t look like a typical blinking light. You’ll want to lower that amount of time. It’s not a right/wrong thing. You’ll just find it’s slow enough that it looks like someone turning on a light and turning it off as opposed to a traditional “blinking.”

it worked fine with its previous two-button on/off setting. the new one-button wont start the cycle

I don’t have the materials with me to test right now, but I believe that because you are (re)starting the task every loop interval, it never stays active long enough to reach the turn-off portion. I would recommend creating a variable, let’s say


taskRunning

, that is set to


true

when


startTask()

is run, and


false

when


stopTask

is run. Then, before running the


startTask()

command, check if


taskRunning

is


true

, and only start the task if it is


false

.


if (buttonToggleState && !taskRunning) {
    startTask(Start_light);
    taskRunning = true;
}
else if (taskRunning) {
    stopTask(Start_light);
    SensorValue[dgtl1] = false;
    taskRunning = false;
}

I added a check to the


else

as well because it doesn’t make much sense to stop a task that isn’t running, but I don’t think it’s strictly necessary in RobotC.