Need two seperate codes running constantly

Hello,

I’m trying to make the flashlight sensor work alongside with the claw. Whenever I run the code, the flashlight only works after I activate the claw. Does anyone know how to make both pieces of code run simultaneously?

Thank you,

Here is my code provided:

#pragma config(Sensor, in2,    light,          sensorAnalog)
#pragma config(Sensor, dgtl1,  limitSwitch,    sensorTouch)
#pragma config(Motor,  port1,           flashlight,    tmotorVexFlashlight, openLoop, reversed)
#pragma config(Motor,  port3,           clawMotor,     tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port4,           rotateMotor,     tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()

{
	while (1==1)
		{
		untilTouch(limitSwitch);
			{
				motor[clawMotor] = 50;
				wait(0.5);
				motor[clawMotor] = 0;
			}
			untilRelease(limitSwitch);
			{
				motor[clawMotor] = -50;
				wait(0.5);
				motor[clawMotor] = 0;
			}
		if(SensorValue(light) >=400)
		{
			turnFlashlightOn(flashlight, 127);
		}
		else if(SensorValue(light) <400)
		{
			turnFlashlightOff(flashlight);
		}
	}
}

Your code is getting locked into an internal loop. You can create separate Events or separate Tasks but it might be easiest to switch the until to an if statement.

Example:

task main()

{
	bool prevSwitchState = false;
	
		while (1==1)
		{
		//If the limit switch is pressed and claw hasn't moved forward.	
		if (limitSwitch && !prevSwitchState)
			{
				motor[clawMotor] = 50;
				wait(0.5);
				motor[clawMotor] = 0;
				prevSwitchState = true;
			}
		//If the limit switch is not pressed and the claw hasn't moved back.	
		if(!limitSwitch && prevSwitchState)	
			{
				motor[clawMotor] = -50;
				wait(0.5);
				motor[clawMotor] = 0;
				prevSwitchState = false;
			}
		if(SensorValue(light) >=400)
		{
			turnFlashlightOn(flashlight, 127);
		}
		else if(SensorValue(light) <400)
		{
			turnFlashlightOff(flashlight);
		}
	}
}
1 Like

Note that the wait statements will mean that during claw motion, the flashlight code won’t run. Continuing in this direction would mean saving the time the claw started and if the time was half a second later stop it.