Science Oxford Logo

Session 5: programming and challenges


Test driving

Before making any changes, check that your robot still works as expected, by running the test code again (press A on your micro:bit).


Wiring the distance sensors

Component list

From a workshop leader, collect two ultrasonic distance sensors with wires attached.

From your previous set of equipment, you need some wires.


Wiring

Go to the end of the assembly instructions from earlier to see the wiring instructions, or follow the steps below.


Testing the sensors

To test the sensors, use the same code as before, but the micro:bit must stay plugged in to the computer.

  1. Send the code to the micro:bit again.
  2. Open the serial terminal at the bottom of the editor.
  3. Click the button on the back of the micro:bit to restart your code.
  4. Press B on the micro:bit to run the sensor test.

What numbers do you get?

If the sensors are working correctly, they should change as objects are moved closer to or further away from the sensors.

If they do not change, or you get e.g. -0.03, speak to a workshop leader for some troubleshooting help.


Distance sensor code

You can now program your robot to avoid obstactles, or you follow you around.

Download the example code, or use the code you worked on in Session 1 (ask a workshop leader to help you find this).

The logic section of the code is this:

screenshot of code and comments, as in the example code linked above

Or in flow chart form:

logic flowchart of distance sensor code

Test your robot with this code, then decide what changes you want to make:


Use the code print outs and flowchart to help you make your changes.


What next?

Once your robot works, you can try some more advanced coding to give your robot more functionality, or make the code more efficient. For example:


COMING SOON: guidance for adding the above functionality into your robot's code