Line follow: robot not responding

So I have created a robot that the purpose is to follow a line. Simple. The only problem is only one is reacting to the sensor.

So the way I have the code set up so that the line follower sensors take the number and assigns it a variable. Both sensors are reading correctly and only one of them are doing it correctly. I’m new to vex and wondering what I could be doing wrong. I’ve tested the functions of turning and running forward work properly, just the while loop for sensor 1 works, while sensor 2 dues not. I’m most appreciative of any help that you guys can give.
Linefollow.pdf (4.83 KB)

You may want to use If statements instead of while. While statements will stay in that loop until the result is false which could be forever. Using an if statement evaluates the condition one time and makes adjustments. This way all three if statements will each take a turn. Also you can now remove the multiple assignments (color2=…) and PrintToScreen.

while ( 1 )
	{
		if ( GetAnalogInput (8) == 0 )
		{
			Tank4 ( 1 , 3 , 2 , 4 , 1 , 3 , 2 , 0 , 0 , 0 , 0 ) ;
		}
		else
		{
			colour2 = GetAnalogInput ( 2 ) ;
			colour1 = GetAnalogInput ( 1 ) ;
			PrintToScreen ( "colour 2 = %d\n" , colour2 ) ;
			PrintToScreen ( "colour 1 = %d\n" , colour1 ) ;
			
			if ( colour2 >= 300 )
			{
				Leftforward ( ) ;
			}
			else if ( colour1 >= 300 )
			{
				RightForward ( ) ;
			}
			else 
			{
				forward ( ) ;
			}

		}
	}

Alright, so I tried what you said the only problem is the problem still exists. The robot is giving feedback that sensor 1 is working but it is not acting on it. Sensor 2 works fine and reacts as it should but for some reason. It’s not acting to sensor 1. Thanks for the help!

Can you post the rest of your code?

I noticed some inconsistencies with the case of your code -


RightForward ()

has both words capitalized (camel case) where


Leftforward()

and


forward()

do not. Are the functions you created being called and working properly?

			if ( colour2 >= 300 )
			{
                               PrintToScreen("colour2 >= 300");
				Leftforward ( ) ;
			}
			else if ( colour1 >= 300 )
			{
                               PrintToScreen("colour1 >= 300");
				RightForward ( ) ;
			}
			else 
			{
				forward ( ) ;
			}

You might also put a


PrintToScreen("Left Forward being called");

in each of your motor functions to make sure they they are being called.

Alright, so I ended up going back to the while statements because, the if statements made the robot response on the one sensor that was working too long, and the robot moves more fluidly using the while statements. The if statements also did not fix the one sensor. As requested here is the rest of the code. Thanks for all l the help you’ve given this far!
forward.pdf (4.01 KB)
Leftforward.pdf (4.01 KB)
Linefollow.pdf (4.83 KB)
RightForward.pdf (4.01 KB)

Alright, so I was able to troubleshoot to get the robot to work, thanks to you m8s that helped me. What I ended up doing was a mixture of what Pete said and what I had originally. I put the code for the sensor that was not working in an if statement but if they both were in if statements the problem reversed with the other sensor not working. I got it to work using a while statement for sensor 2 and an if statement for sensor 1, then an else statement for going forward. It’s a little bit of a mess but thanks Pete for your suggestions.

Glad to hear you are making progress. One other way to work with line followers is to have the line detection change the speed of the motors a small amount instead of reversing them.

void drive(int left, int right){
	//User function to control all chassis motors
	SetMotor ( 1 , right * -1 ) ;
	SetMotor ( 2 , right * -1 ) ;
	SetMotor ( 3 , left  * -1 ) ;
	SetMotor ( 4 , left  * -1 ) ;
}

//Variables section
int defaultSpeed = 30;
int adjustSpeed = 20;
int colour1, colour2, leftSpeed, rightSpeed;

while(1){
	colour1 = GetAnalogInput ( 1 ) ;	
	colour2 = GetAnalogInput ( 2 ) ;
	if ( colour1 >= 300 ) 
	{
		leftSpeed = adjustSpeed;
	} else 
	{
		leftSpeed = defaultSpeed;
	}
	if ( colour2 >= 300 ) 
	{
		rightSpeed = adjustSpeed;
	} else
	{
		rightSpeed = defaultSpeed;
	}
	drive(leftSpeed, rightSpeed);
}


Then when you really want to get crazy you can take advantage of ternary operators which can replace if statements but you will need to use the UserCode function under Program Flow in EasyC - it doesn’t have a ternary function.

while(1){
	// comparison ? true result : false result
	leftSpeed  = GetAnalogInput ( 1 ) >=300 ? adjustSpeed : defaultSpeed;
	rightSpeed = GetAnalogInput ( 2 ) >=300 ? adjustSpeed : defaultSpeed;
	drive(leftSpeed, rightSpeed);
}