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.
-
Don't forget, the Sparkle module must be plugged into d1!
-
-
-
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 (LOW), turn the left Sparkle green.
-
If the left sensor is off the line (HI), turn the left sparkle red.
-
For now, let's comment out the line following code so we can test the sparkles on their own.
-
To do this, we can use a block comment to comment out lots of lines at once.
-
Put /* at the start of what you want to comment, and */ at the end.
-
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 A and B are LOW!
-
Add a third IF/ELSE block that checks if both A and B are LO, and sets the middle Sparkle green if they are.
-
There is a hint layout if you need it.
-
Don't forget the and operator, &&
-
-
-
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 - 3 IF statements 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.
-
Uncomment your normal line follower code
-
Move the Sparkle lines into the matching IF statements in the main program, 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 first 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!
-