Create an advanced maze solving algorithm that uses the gyroscope for precise 90-degree turns, implements a wall-following strategy, and organises code with functions for a clean, efficient solution.
Activity Demonstration
Setup
Build the most challenging maze you can. Use narrow pathways with only 3-5cm clearance on each side. Include loops, multiple paths, and dead ends. Add coloured sections on the floor for bonus tasks like changing speed or playing different sounds.
Building On Maze II
In Maze II we used all three distance sensors to make better decisions. Now we'll add the gyroscope for precise turning, use functions to organise our code, and implement a proper wall-following algorithm.
Stage 1: Precise Turns With The Gyroscope
Instead of turning for a set time (which can be inaccurate), use the gyroscope to measure exactly how far the rover has turned. Create a function called turnRight that turns the rover exactly 90 degrees by reading the gyroscope and stopping when the target angle is reached. Do the same for turnLeft.
Stage 2: Wall-Following Algorithm
Implement a left-hand or right-hand wall-following strategy. The idea is simple: always keep a wall on one side. If the wall disappears (an opening), turn that direction. If the wall is ahead, turn the other way. This strategy guarantees the rover will eventually find the exit in any simply-connected maze.
Stage 3: Organise With Functions
Create functions for each behaviour: moveForward, turnLeft, turnRight, checkWalls, and checkFinish. Your main loop should read cleanly as a series of function calls. This makes the code easier to debug and modify.
Stage 4: Speed Optimisation
Once your rover can reliably solve the maze, optimise for speed. Move faster in straight sections and slow down when approaching walls or turns. Use the distance sensor readings to adjust speed dynamically.
Challenge
Can you solve the maze as fast as possible? Time your rover and try to beat your personal best. Compare times with classmates to see who has the most efficient algorithm.
