Pass Statement


Did You Know? Pass Statement

If you don’t rotate the servo with the micro:bit and board, its horn will turn the opposite direction the micro:bit turns.  The statement that makes the servo turn the opposite direction from the micro:bit is angle = 180 - angle in this if...else… statement:

    if angle >= 0:
        angle = 180 - angle
    else:
        continue

To make it turn the same direction, the angle = 180 - angle statement needs to be removed.  That might seem simple enough, but it’s in an if… statement.  So, you can’t just comment it or remove it because MicroPython requires an if… statement to contain at least one statement (below and indented).  Otherwise, the script would stop executing statements and display an exception message when it reached the if… statement.

To make sure theif… statement still contains a statement MicroPython can execute, you can simply add a pass statement.  A pass statement essentially tells MicroPython: “Don’t do anything here, just move on to the next statement.”  Even though it’s a statement that doesn’t actually do anything, it’s still a MicroPython statement.   

    if angle >= 0:
        # angle = 180 - angle
        pass
    else:
        continue

Here’s another way to rewrite theif… statement so it still skips servo positioning in response to angles outside 0...180.  Angles outside the 0...180 range will be negative: -1…-179.  So, another approach would be to rewrite the if...else... statement so it’s just an if statement that only uses continue to skip the servo positioning by jumping back to while True when angle is less than zero.   When angle is greater than zero, this if… statement allows the script to keep going through the last statement in the loop, which updates the servo horn’s position with pin16.write_analog(value).     

    if angle <= 0:
        continue

Your Turn

The Did You Know section above explained how to make the servo horn mirror the board’s rotation if the servo is sitting still and the board is rotated.  

  • Click the Open button and reopen accelerometer_servo_points_up script.
  • Click Send to micro:bit.  
  • Set the servo on a desk or tape it to a wall with the cable to the top, and rotate the board.  The horn should rotate in the opposite direction you rotate the board.
  • Change the project name field to tilt_controlled_servo.
  • Try one of the two script changes explained in the Did You Know section above.
  • Verify that the direction the horn points mirrors how you rotate the board (in the 0° to 180° range.)
  • Click Save to save your work.