Skip to main content

1.1

by Tech Camp

Introduction

There's a gap in the track! We need to make our robot even more intelligent so it won't get stuck, and can find the track again on its own.

  1. We only need the line follower module for this lesson - assemble your robot like the picture!
    • We only need the line follower module for this lesson - assemble your robot like the picture!

  2. Dealing with breaks in the track is difficult. We can't just drive forward for 1 second using a delay, as we won't be able to sense the line at the same time.
    • Dealing with breaks in the track is difficult. We can't just drive forward for 1 second using a delay, as we won't be able to sense the line at the same time.

    • Build the test program in the picture.

    • It would be great if this program drove forwards and then stopped on the line - try it, see what happens.

    • It only works if one of the sensors is exactly on the line after 1 second - this is not very likely!

    • This is because the delay stops anything else from happening whilst it is waiting for 1 second - so all the time we are driving forwards, we can't check the sensors - that's no good!

    • Things that stop other things from happening are called 'blocking' - they block everything else until they are finished.

  3. We need to come up with a way to wait whilst still being able to do other things.
    • We need to come up with a way to wait whilst still being able to do other things.

    • Replace the delay(1000); line with a for loop that runs 10 times, with a delay(100); line inside it.

    • This code will wait for 1 second, just like before.

  4. We now need to add a variable that acts as a counter - it will count how many milliseconds of delay have happened.
    • We now need to add a variable that acts as a counter - it will count how many milliseconds of delay have happened.

    • Above the for loop, make a new int variable called t and set it equal to 0.

    • Inside the loop, increase t by 100

    • t+=100 is a shortcut for increasing a variable (it just means t=t+100). You can use t-=100 to reduce things as well!

    • Add another line in the loop to Serial.println the value of t.

    • Run the program and watch the value of t in the Serial Monitor - it will now count the number of milliseconds of delay!

  5. Now let's use the counter to control the number of times the loop runs.
    • Now let's use the counter to control the number of times the loop runs.

    • Instead of the for loop, replace it with a while loop.

    • For the condition, run the loop while t<1000.

    • We can now change this number to decide how long the wait is! For example, changing to t<2000 would run the loop for a total of 2000 milliseconds (2 seconds).

  6. What's the point of making a really complicated delay?
    • What's the point of making a really complicated delay?

    • Anything we put in the loop will be run as the delay is happening - so we can check the sensors whilst we are driving forwards!

    • Move the IF statement checking the sensors inside the loop.

    • Try it out - the robot should now drive forward and stop exactly on the line, every time!

    • Experiment with changing the length of the wait loop, so the robot can start further away from the line and still reach it.

  7. We actually don't need the IF statement in the loop - we can merge the conditions of the loop and the IF statement together!
    • We actually don't need the IF statement in the loop - we can merge the conditions of the loop and the IF statement together!

    • Let's think about this - we want to run the loop (and the the motors) if:

    • t<2000, AND

    • Sensor A is HIGH, AND

    • Sensor B is HIGH

    • Luckily, we can use 2 AND operators together to do this! Change your code to look like the picture, and test it out.

  8. Let's merge our code with the 2 sensor line follower program to deal with simple breaks in the track.
    • Let's merge our code with the 2 sensor line follower program to deal with simple breaks in the track.

    • Load up your code and add the line finder code you just wrote to the IF statement where both sensors are off the track (HIGH).

    • It should look like the picture!

  9. We need to make a few changes to make our code work with the line follower:
    • We need to make a few changes to make our code work with the line follower:

    • Add a delay of 200 milliseconds before the while loop (this makes sure both sensors are not on the line)

    • We only want to stop the motors if both sensors are still off the track after the wait loop.

    • Put the motor stop blocks in an if statement, that checks if both sensors are HIGH

    • After we have stopped the motors, we then want to wait until 1 of the sensors is LOW before we continue

    • Add another while loop to do this!

  10. Cover a small section of straight track (about 5cm) with a piece of paper, or white PVC tape and tape it down to test the program.
    • Cover a small section of straight track (about 5cm) with a piece of paper, or white PVC tape and tape it down to test the program.

    • You will probably need to make adjustments to speeds and timings to make it work reliably!

    • Keep experimenting until it works well.

    • Make sure the gap is on a straight section of track - this code won't work on gaps in curves! Can you work out why?

  11. Once you can cross a gap in straight track, try a gap in curved track! To do this, you will need to make the robot move side to side in the wait loop, instead of just moving forwards.
    • Once you can cross a gap in straight track, try a gap in curved track!

    • To do this, you will need to make the robot move side to side in the wait loop, instead of just moving forwards.

    • This can be done by making the robot turn to start with instead of going forwards, and then changing the direction of turn inside the loop every so often.

    • It works best if the robot goes left and right several times , in a kind of sweeping motion.

    • You can also experiment with other types of break like in the picture - offset lines and breaks that point the robot in the wrong direction like the middle example are particularly difficult to get right!

Finish Line

Tech Camp

Member since: 10/03/2012

119 Guides authored

Team

Tech Camp Staff Member of Tech Camp Staff

6 Members

140 Guides authored