Skip to main content

1.1

by Tech Camp

Introduction

Learn about analogue inputs to make an even more sophisticated line following robot, that will smoothly follow any path.

  1. We just need the line sensor for now  - make sure your robot is setup like the picture.
    • We just need the line sensor for now - make sure your robot is setup like the picture.

  2. To make a smoother, better line follower, we need to use the line sensor in analogue mode. So far, we have been using it as a digital sensor - it can only be ON or OFF (HIGH or LOW).
    • To make a smoother, better line follower, we need to use the line sensor in analogue mode.

    • So far, we have been using it as a digital sensor - it can only be ON or OFF (HIGH or LOW).

    • Analogue inputs (and outputs) can have any value - think about the difference between a digital and an analogue clock

    • A digital clock must display a whole number of minutes

    • But on an analogue clock, the minute hand can be anywhere - even halfway between two minutes!

  3. Build the simple test program in the picture.
    • Build the simple test program in the picture.

    • Program your robot, and keep it plugged in.

    • Try moving the robot slowly from one side of the line to the other, whilst watching the value of t in the serial monitor.

    • See how it changes gradually as you approach the line?

  4. We can use this gradual change to smoothly change the amount the robot turns as it get further from the line!
    • We can use this gradual change to smoothly change the amount the robot turns as it get further from the line!

    • Add two new variables to the program, above the setup and loop, called l and r (left and right).

    • Let l = the analogue value of the left sensor, and r = the analogue value of the right sensor.

    • Add two Serial.println lines to print the values of l and r to the serial monitor in the loop.

    • Add a delay of 500 milliseconds to the loop so the values aren't printed too fast to read.

  5. The larger the difference between l and r, the further the robot is from the line so the more we need to turn.
    • The larger the difference between l and r, the further the robot is from the line so the more we need to turn.

    • For example, if both sensors are on the line, we don't need to turn at all and l and r will have the same value.

    • Add a new variable called turn.

    • After getting the values of l and r, set turn equal to the difference between l and r.

  6. Change the Serial.println lines to print the value of turn, and remove the delay.
    • Change the Serial.println lines to print the value of turn, and remove the delay.

    • Program your robot and watch what happens to the value of turn as you move it across the line. It should be 0 when the robot is exactly on the line!

    • If it isn't 0, add or subtract a value from turn so that it is - then test it again!

    • For example, if turn=300 when the line sensor is on the middle of the line, add a line of code that says turn-=300;

    • Then, add two lines to set the speeds of the motors using the turn variable.

    • Do you understand how the code works? (hint: turn is positive when we need to turn right, and negative when we need to turn left)

  7. The motor run blocks can only take values between -100 to 100 - the turn variable is currently much larger than that when 1 sensor is off the line and 1 is on the line!
    • The motor run blocks can only take values between -100 to 100 - the turn variable is currently much larger than that when 1 sensor is off the line and 1 is on the line!

    • Using the serial monitor, find the maximum value of turn as you move your robot across the line.

    • Add 1 more line to divide turn by the correct amount, so it never gets higher than 100.

    • For example, if the maximum value of turn is 1000, your line should read turn=turn/10.

  8. You might have noticed that while the new program is smooth, it isn't as fast as the old two sensor digital program - it might also struggle with the tighter turns.
    • You might have noticed that while the new program is smooth, it isn't as fast as the old two sensor digital program - it might also struggle with the tighter turns.

    • To make it faster, we need to make sure 1 wheel is always going 100% forwards, and then change the speed of the other wheel only based on how large turn is to follow the line.

    • So, if turn is positive, we should be turning right, so motor 1 should be at 100%

    • If turn is negative, we should be turning left so motor 2 should be at 100%

    • Add an if/else statement to check if turn is positive or not, and set the correct motor to 100%.

  9. Now its over to you!
    • Now its over to you!

    • Add two more motor lines into the if/else statement to set the other motor's speed using the turn variable, for turning left and right

    • Test the program really well - experiment with multiplying turn by different amounts to get a reliable and smooth line following program.

    • You will need to change how much you adjust turn by - it can now be up to 200!

    • Here's a hint if you're confused - for the first part where motor 1 is at 100%, motor 2 should be set to forwards at a speed of 100 - turn.

  10. If you're feeling really advanced, add the Sparkle module back in and set the colours of the LEDs proportionally based on how far away from the line the robot is!
    • If you're feeling really advanced, add the Sparkle module back in and set the colours of the LEDs proportionally based on how far away from the line the robot is!

    • Your robot can also get lost and now has no way of finding the line again - try and add the code you wrote previously back in so the robot can't get lost, or at least stops if it loses the line completely.

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