Need help on conditional statements in a robot program

yhelothar

Lifer
Dec 11, 2002
18,409
40
91
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);
}
}
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
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:

yhelothar

Lifer
Dec 11, 2002
18,409
40
91
When I said fire extinguishing.. I actually meant a candle. :p

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'
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
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:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
When I said fire extinguishing.. I actually meant a candle. :p

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?
 

sm625

Diamond Member
May 6, 2011
8,172
137
106
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?