Science Oxford Logo

CCC - Self-Driving Cars

Resources for the 'Self-Driving Cars' Creative Computing Club.

This page recaps what we discussed as a group.

To skip to the advanced extensions, click here.


Creating your first program

Open the Python editor by visiting python.microbit.org/. Your screen will look something like this:

screenshot of the Python editor screen

Edit the code on the screen, then flash it to your micro:bit to see the changes.

Try:


Testing the cars

Download the project hex file, and open it in the Python editor.

This file includes a library, car.py, and the main file to write your own code in.

Send this code to the micro:bit, plug it into the car's edge connector, and test it out - how far does the car drive before stopping?

What features do you need to add to the code to make the car work properly?

Currently, the car is set to stop as soon as one of the sensors goes over the black line.


Making them smart

The next step is to program the car to turn off the black line so that it can keep following the road.

IN-PERSON DEMO HERE - INFO TO BE ADDED

The Python way of writing this is:

elif left_sensor > 100:
    car.left_turn(500)

Where the number inside the brackets is how long the car should turn for - you will need to test out your own car to find the best amount of time.

Below is the full code (replacing your current while loop), including turning left if the left sensors goes onto the line. Try it out, then add the right sensor yourself.

while True:
    left_sensor = pin2.read_analog()
    right_sensor = pin1.read_analog()
  
    if left_sensor <= 100 and right_sensor <= 100:
        car.forward(500)
    elif left_sensor > 100:
        car.left_turn(500)
      
    else:
        car.stop()

Test out your car on the roads - how well can it cope with the corners?