Introduction
You may have found that when testing your robot, it was sometimes difficult to know what was going wrong as there was no feedback about the sensors!
-
-
Let's add some more modules to allow our robot to give feedback about the line it is following.
-
Add the Line sensor underneath as before then add Sparkle and buzzer modules to your robot like in the picture.
-
It can be quite hard to see the wiring with so many things plugged in! The motors and line sensor should be the same as before (M1, M2 and P0, P1)
-
Plug the buzzer into P2, and the Sparkles into P8
-
-
-
Load up your two sensor line follower program from the last lesson.
-
It should look something like our example in the picture.
-
-
-
Let's use the outer two Sparkles (0 and 2) to show the state of the two infrared sensors.
-
If the left sensor is on the line (0), turn the left Sparkle green.
-
If the left sensor is off the line (1), turn the left sparkle red.
-
Let's change the program so the line follower code isn't used for the moment - we can do this by making it into a function.
-
Move the line follower code out of the while True: loop and into a function called line_follower that takes no inputs.
-
Write the code in the picture to test the left sparkle - test it by moving it across the line!
-
-
-
Let's use the right Sparkle (Sparkle 2) to do the same thing for the right hand sensor.
-
Add some more code to set the right Sparkle in the same way as the left one, but using the right sensor.
-
There is a hint layout if you need it!
-
Don't forget to test it properly.
-
-
-
Let's add some more blocks so the middle Sparkle (Sparkle 1) is green when both sensors are on the line.
-
This is when both left and right are 0!
-
Add a third IF/ELSE block that checks if both left and right are 0, and sets the middle Sparkle green if they are.
-
There is a hint layout if you need it.
-
Don't forget to use and.
-
-
-
You may have thought the last program didn't look quite right - as the robot moved over the line, two Sparkles changed at once!
-
It would be more sensible for the program to set just the middle Sparkle green when both sensors are on the line, instead of all three.
-
We need to change the code to do this, to set the colours of all three Sparkles for each combination of sensor states.
-
In the picture is the structure you will need - a 3 part if, else if, else statement for checking if the robot is to the left, right or on the centre of the line.
-
Add lines of code to set the sparkles properly!
-
-
-
You may have noticed that our Sparkle code and line following code have some very similar IF blocks.
-
Move the Sparkle lines into the matching IF statements in the line follower function, to merge your line follower and Sparkle programs together.
-
Test it properly on the track!
-
-
-
What about when the robot comes off the line completely?
-
Add some more Sparkle lines to the correct IF statement to turn all the Sparkles red if the robot is off the line.
-
-
-
Our robot should probably make more of a fuss if it gets lost completely and doesn't know where the line is.
-
Add some code to make the buzzer beep SOS if the robot goes off the line.
-
You can look back at the Morse Code lesson if you can't remember how to do this!
-