Question

I am working on a NXT lego mindstorm robot and I am building the line follower robot with single reflected light sensor and I am programming in C Language.

the sensor converts the raw Analogue value to digital value with the range from 0 - 1023.

I have to code that of the line is black then motor will move forward and if the there is no line (white base) then stop and if there is grey (in between white and black line) then slightly move right and then left differently to find the lost black line .

So the code is like

While (1)
{
a=ecrobot_get_light_sensor(port_led); //storing the A/D converted value in variable

while (a<300)   // White area
{
ecrobot_status_monitor("White Area");
nxt_motor_set_speed(port_motor_l, 0, 1); // left motor turns off
nxt_motor_set_speed(port_motor_r, 0, 1); // right motor turns off
}
while (a>=600) // Black Line
{
ecrobot_status_monitor("Black Area");
nxt_motor_set_speed(port_motor_l, 100, 1); // left motor turns on
nxt_motor_set_speed(port_motor_r, 100, 1); // right motor turns on
}

while (a>=300 || a<600) // Robot loosing the black line
{
ecrobot_status_monitor("grey Area");
nxt_motor_set_speed(port_motor_l, 50, 1); // left motor move forward
nxt_motor_set_speed(port_motor_r, -50, 1); // right motor move backward

delay_ms(200)
nxt_motor_set_speed(port_motor_l, -50, 1); // left motor move backward
nxt_motor_set_speed(port_motor_r, 50, 1); // right motor move forward
delay_ms(200)

}

Problem is that if the robot loses the line then start moving clockwise without stopping and moving towards the anticlockwise after 200 ms delay .

What is the problem with my code ?

How can i stop the motor after this interval and motor should move in other direction after this interval in search of line and then it will surely find a line

Thanks!

Was it helpful?

Solution 2

I see a couple of things,

  • Some semicolons missing. Im sure thats not the case in your real code, since it wouldn't even compile. Also try and have proper parenthesis in your statements (better practise :-)

  • You also need to re-read the sensor while in the loops to avoid dead lock!!!

  • Also as mentioned, use if/else if statements in a big while loop.

  • You can add some thresholds to make it smoother :-)

Good luck

OTHER TIPS

One solution is to change all the while statements inside your while(1) loop into if statements.

For example, in your last while statement, when the robot looses the black line, your robot will spin clock-wise and then spin counterclockwise for 200ms in each direction regardless of the new values read from your sensor. This is because the last while statement will finish its execution first before your a gets updated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top