In the previous lesson, you used Zumi’s front IR sensors to avoid obstacles. Now you will use the bottom IR sensors to sense a white or black line on the road and stop.
Pseudocode
Like you did in the obstacle avoidance lesson, write each line of pseudocode as a comment. You will be translating this to code later.
Test!
Once you have your pseudocode, translate each line of code into Python and fill it in the for loop below. Remember that the index for the bottom right IR is 1 and the bottom left IR is 3.
In [ ]:
for x in range(300):
# Write your code here
zumi.stop() # Don't forget to stop at the end!
print("Done!")
Use electrical tape on a light surface or the worksheet included with the lesson guide to test your code. If you don’t want your code to keep running even after Zumi has stopped, replace zumi.stop() in the if statement with break. For example:
if left_ir < 50 and right_ir < 50:
break
Using a break statement will stop your for loop, even if all of the iterations haven’t finished. This will allow you to move on to the rest of your code (if there is any) without waiting for the for loop to finish. Test it in your code above!