Counters

For class I must have a robot with 5 sensors on it go around a line track without going off course. I have that part down, but then every time the track changes from a straight away to semi curvy to really curvy ect, there are a certain number of black horizontal lines on the track that I must read in with the sensors and then decide my speed depending on the track type. I do not know how to make a counter that counts these horizontal lines but resets as soon as you pass all the lines for that "road sign" please help me. I do not have my whole code because we have to use the school computers but it is essentially this:

int main()
{
unsigned int sensors[5]; // an array to hold sensor values

// set up the 3pi
initialize();

// This is the "main loop" - it will run forever.
while(1)
{
// Get the position of the line. Note that we *must* provide
// the "sensors" argument to read_line() here, even though we
// are not interested in the individual sensor readings.
unsigned int position = read_line(sensors,IR_EMITTERS_ON);

if(position < 1000)
{
// We are far to the right of the line: turn left.

// Set the right motor to 100 and the left motor to zero,
// to do a sharp turn to the left. Note that the maximum
// value of either motor speed is 255, so we are driving
// it at just about 40% of the max.
set_motors(0,100);

// Just for fun, indicate the direction we are turning on
// the LEDs.
left_led(1);
right_led(0);
}
else if(position < 3000)
{
// We are somewhat close to being centered on the line:
// drive straight.
set_motors(100,100);
left_led(1);
right_led(1);
}
else
{
// We are far to the left of the line: turn right.
set_motors(100,0);
left_led(0);
right_led(1);
}
}

// This part of the code is never reached. A robot should
// never reach the end of its program, or unpredictable behavior
// will result as random code starts getting executed. If you
// really want to stop all actions at some point, set your motors
// to 0,0 and run the following command to loop forever:
//
// while(1);
}

Now I just need to figure out how to change speeds when I hit different terrains. The value of the sensors when it passes a black horizontal line is 1,000
typically you would do something like this in your control loop.

old_sign;
current_sign;

...
current_sign = sensor();
if(current_sign != old_sign)
{
resetcounters();
}
old_sign = current_sign;

in practice, where the terrain is not known or of a specified design, you can time out...

if(time_since_last_sensed > N)
reset_counters();

Topic archived. No new replies allowed.