Use the tutorial to explore Servo motors.
I wanted to make a simple paper or cardboard robot to demonstrate how a student could move its head using a servo. The idea was for them to control the movement of the robot with the buttons on the micro:bit. Once a student understands this principle, they can expand on the idea to add additional motors using the extension board. Eventually, they can further expand to use the radio or Bluetooth signal so that a completely separate micro:bit controls the robot.
Printed to cardstock with a cardstock stand.
Paper robot with servo attached.
Paper robot with servo attached (using adhesive foam)
There were a few challenges when developing this code. The movement of the servo was straightforward enough. I assigned a variable for the position of the servo arm. If-Else logic made it easy to change the position by using the A and B buttons, though I did need to work out the timing for the pause so that it didn't spin too fast or too slowly. I may still speed it up a bit more, but for demonstration purposes this speed works nicely.
I wanted to control the range of motion from 0 to 180 degrees. After experimenting with nested If statements and For loops, I settled on the While loop. The nested If statement worked just as well, but the While loop seemed to create less delay and a smoother movement. The For loop created a complete mess with the servo essentially moving right and left without control. In retrospect, I probably could have used the "set servo range" block in the on start instead. For version two I'll test that.
I wanted to create a kind of reset back to the center for ease of use, so I coded the A+B button push to return to 90 degrees. It took a bit of playing to decide where in the code to add it for the best result. A separate input was unreliable and created unwanted delays. Placing the If statement within the main servo movement loop also caused it to be reliable. Setting that option into its own conditional near the end of the while loop worked well.
For some reason, when the servo reaches 0 degrees, it freezes. It didn't matter which button I used or how I arranged them in the loop. It didn't matter if I adjusted the OR statement to check between 1 and 180 degrees. It froze every time. So I added an If statement that just added a degree whenever the position hit 0. That fixed the problem, though I have no idea why it was an issue at all.
To make a math connection, I added the ability to check the exact degree position of the servo if you touch the micro:bit logo. I wanted to add it to the loop so that the number updated each time the position changed, but it slowed down the movement of the servo far too much.
The only other challenges were more physical. Putting the servo arm (horn) on in the correct position to get the movement I wanted took a bit of trial and error. Designing the paper robot and a paper stand to be strong enough to keep the robot standing once the servo was attached was a bit of an engineering design challenge. I also switch to using the extension board as it seemed to power the servo better, yielding smoother though not smooth) motion.
The last thing I did was adjust the code so that you only had to set the starting position once. This made it easier as I was playing with positioning the servo arm.