• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need help on conditional statements in a robot program

yhelothar

Lifer
So I'm building a fire extinguishing robot. I basically have two flame sensors on my robot. I want my robot to move forward when the front sensor sees the flame, and backwards if the rear sensor sees the flame.

It seems to want to run forwards and backwards simultaneously if either sensor sees the flame.

Why does the code run both conditionals if only one was met?

Here's my code:



void findflame()
{
while(SensorValue[rightEncoder] < 171)
{
orientflame();
if(SensorValue[flameF] > 500);
{
motor[motorR] = 40;
motor[motorL] = 40;
}
if(SensorValue[flameR] > 500);
{
motor[motorR] = -40;
motor[motorL] = -40;
}
stopdistance(5);
}
}
 
Well if both SensorValue[flameF] > 500 and SensorValue[flameR] > 500 are ever true at the same time then the robot will always prefer going backwards since the flameR check is second.

You could use an if-elseif construct to give one of your conditionals a preference.

Code:
if(SensorValue[flameF] > 500)
{
motor[motorR] = 40;
motor[motorL] = 40;
}
else if(SensorValue[flameR] > 500)
{
motor[motorR] = -40;
motor[motorL] = -40;
}

That will always go forwards to a flame if it detects both a flame in front and behind.
 
Last edited:
When I said fire extinguishing.. I actually meant a candle. 😛

So there will only be one source of flame at any given time.


I just tried your code. It's not liking the else statement for some reason. I've used that command in the past fine without issues.

Error:Unexpected 'else'. Ignored.
Error:Unexpected scanner token-> 'if'
 
Oh wait a second, somehow I put some ; after the if statements(appears to be a copypasta mistake from your code)... check the code again.

Also, just because the flame is in front of the robot doesn't mean the robots rear sensor isn't seeing the flame as well. If that is the case then you are going to want to compare the sensor values to each other to determine whether or not the flame is in front or behind.

What language is this?
 
Last edited:
When I said fire extinguishing.. I actually meant a candle. 😛

So there will only be one source of flame at any given time.
Then it sounds like you have a sensor problem too, as both sensors are triggering. (Or flameF == flameR) Lights in the room triggering a sensor, perhaps?
 
Code:
void findflame()
{
	while(SensorValue[rightEncoder] < 171)
	{
		orientflame();
		if(SensorValue[flameF] > 500)
		{
			motor[motorR] = 40;
			motor[motorL] = 40;
		}
		else
		{
			if(SensorValue[flameR] > 500)
			{
				motor[motorR] = -40;
				motor[motorL] = -40;
			}
		}
		stopdistance(5);
	}
}


You might want to put the rear flame check first, like this:

Code:
void findflame()
{
	while(SensorValue[rightEncoder] < 171)
	{
		orientflame();
		if(SensorValue[flameR] > 500)
		{
			motor[motorR] = -40;
			motor[motorL] = -40;
		}
		else
		{
			if(SensorValue[flameL] > 500)
			{
				motor[motorR] = 40;
				motor[motorL] = 40;
			}
		}
		stopdistance(5);
	}
}

Also, this code is setting some variables (motor[motorR] etc) but there is no code shown that clears them. Usually when you do something like this, you are incrementing or decrementing the variable rather than setting it to some value. I assume one of those functions is re-initializing all those variable?
 
Back
Top