If statements

I need help once again, Im not sure why my if statement is not working can you guys help me out. This is what my programming looks like.

task Light()
{
while ( 1 == 1 )
{
turnFlashlightOn(port1);
}
}

task dispenseMarbles()
{
while ( 1 == 1 )
{
startMotor (port2,-20);
wait(.3);
stopMotor (port2);
wait(3);
}
}

task main()
{
StartTask( Light );
startMotor(port3,20);
StartTask( dispenseMarbles );
untilEncoderCounts (75, dgtl3);
stopMotor(port3);
if ((sensorValue(dgtl3)< 60))
startmotor (port3, 20);
wait(.5);
startmotor (port4, 20);
wait(.2);
startmotor(port4,-20);
wait(.2);
stopmotor(port4);
else
startmotor(port3,20);

}

Let’s clean up that code so we can see what’s wrong…

task Light()
{
	while(true) {
		turnFlashlightOn(port1);
	}
}

task dispenseMarbles() {

	while(true) {

		startMotor(port2, -20);

		wait(.3);

		stopMotor(port2);

		wait(3);

	}

}

task main() {

	StartTask(Light);

	startMotor(port3,20);

	StartTask(dispenseMarbles);

	untilEncoderCounts(75, dgtl3);

	stopMotor(port3);

	if(sensorValue(dgtl3) < 60)

	startmotor(port3, 20);

	wait(.5);

	startmotor(port4, 20);

	wait(.2);

	startmotor(port4,-20);

	wait(.2);

	stopmotor(port4);

	else

	startmotor(port3,20);

}

So a few things are apparent to me, first off why is lights it’s own task and in an infinite loop? Doesn’t seem necessary at all to me, let’s get rid of that.

task dispenseMarbles() {

	while(true) {

		startMotor(port2, -20);

		wait(.3);

		stopMotor(port2);

		wait(3);

	}

}

task main() {

	turnFlashlightOn(port1);

	startMotor(port3,20);

	StartTask(dispenseMarbles);

	untilEncoderCounts(75, dgtl3);

	stopMotor(port3);

	if(sensorValue(dgtl3) < 60)

	startmotor (port3, 20);

	wait(.5);

	startmotor (port4, 20);

	wait(.2);

	startmotor(port4,-20);

	wait(.2);

	stopmotor(port4);

	else

	startmotor(port3,20);

}

Now I wonder why task main isn’t in an infinite loop…

task dispenseMarbles() {

	while(true) {

		startMotor(port2, -20);

		wait(.3);

		stopMotor(port2);

		wait(3);

	}

}

task main() {

	turnFlashlightOn(port1);

	while(true) {

		startMotor(port3,20);

		StartTask(dispenseMarbles);

		untilEncoderCounts(75, dgtl3);

		stopMotor(port3);

		if(sensorValue(dgtl3) < 60)

		startmotor (port3, 20);

		wait(.5);

		startmotor (port4, 20);

		wait(.2);

		startmotor(port4,-20);

		wait(.2);

		stopmotor(port4);

		else

		startmotor(port3,20);

	}

}

Now the real issue is showing itself quite clearly, your if statement lacks brackets. When an if statement lacks brackets ONLY THE VERY NEXT COMMAND is actually linked to the if statement.

task dispenseMarbles() {

	while (true) {

		startMotor(port2, -20);

		wait(.3);

		stopMotor(port2);

		wait(3);

	}

}

task main() {

	turnFlashlightOn(port1);

	while(true) {

		startMotor(port3,20);

		StartTask(dispenseMarbles);

		untilEncoderCounts(75, dgtl3);

		stopMotor(port3);

		if(sensorValue(dgtl3) < 60) {

			startmotor(port3, 20);

			wait(.5);

			startmotor(port4, 20);

			wait(.2);

			startmotor(port4,-20);

			wait(.2);

			stopmotor(port4);

		} else {

			startmotor(port3,20);

		}

	}

}

This code still seems logically flawed as it now spawns a dispenseMarbles task every time the main loop runs, and since the dispenseMarbles tasks never completes, that won’t end well at all. My best guess is that dispenseMarbles should also not have the loop, and also doesn’t belong in it’s own task so let’s move that code into a function and just call it.



void dispenseMarbles() {

	startMotor(port2, -20);

	wait(.3);

	stopMotor(port2);

	wait(3);

}

task main() {

	turnFlashlightOn(port1);

	while(true) {

		startMotor(port3,20);

		dispenseMarbles();

		untilEncoderCounts(75, dgtl3);

		stopMotor(port3);

		if(sensorValue(dgtl3) < 60) {

			startmotor(port3, 20);

			wait(.5);

			startmotor(port4, 20);

			wait(.2);

			startmotor(port4,-20);

			wait(.2);

			stopmotor(port4);

		} else {

			startmotor(port3,20);

		}

	}

}

But the code is so vague as to what these motor ouputs do that I cannot guess any further, but hopefully from this you can get what you need.

I recommend breaking these little routines into functions and naming them well.

-Cody