“On start”

Each project consists of two basic blocks at the beginning. The on start-block and the forever-block. All instructions within these two blocks are executed by the program. However, the on start-block is only executed once as soon as you start your program.

The following program switches the brake light on when the micro:bit is started, then waits 2 seconds and switches the brake light off again.

It is important to ensure that the revision of the Joy-Car's mainboard is specified. You can find out more about this here, for example how to find out which revision you are using.

JoyCar.initJoyCar(RevisionMainboard.OnepThree)
JoyCar.brakelight(ToggleSwitch.On)
basic.pause(2000)
JoyCar.brakelight(ToggleSwitch.Off)

The sample code in MicroPython is there because it is also available in MakeCode. This is super handy because if you're already comfortable with MakeCode, it's much easier to get started with MicroPython. By having sample code in both environments, you can continue learning without much interruption. This will help you understand how programming works and you can see how to do the same things in different programming languages.

# Import necessary libraries
from microbit import *
import neopixel

# Define your Joy Car mainboard revision
joycar_rev = 1.3

# Define object for the lights
np = neopixel.NeoPixel(pin0, 8)

# Define values for the lights
# Values which LEDs are to be activated
headlights = (0, 3)
backlights = (5, 6)
indicator_left = (0, 4)
indicator_right = (3, 7)
indicator_warning = (1, 2, 4, 7)

# Values, which color should be displayed on the LEDs
led_white = (60, 60, 60)
led_red = (60, 0, 0)
led_off = (0, 0, 0)
led_red_br = (255, 0, 0)
led_orange = (100, 35, 0)

# Method for activating/deactivating the brake light
def lightsBreak(on = True):
    if on:
        for x in backlights:
            # Define bright red for the brake lights
            np[x] = led_red_br
    else:
        for x in backlights:
            # Define black for the brake lights
            np[x] = led_off
    np.show()

# Activate brake lights
lightsBreak()

# Wait for 2 seconds
sleep(2000)

# Deactivate brake lights
lightsBreak(on = False)

"Forever"

The forever-block, on the other hand, is executed over and over again. As soon as all commands and instructions in this block have been processed from top to bottom, execution starts again from the beginning. Here, for example, you can permanently check the sensors and react to new events.

The following code is based on the example. The on start-block is executed first. In this block, the revision of the Joy-Car is again specified first, the brake light is switched on, then you wait 2 seconds and then the brake light is switched off. As the code does not end here, in contrast to the first example, we have inserted a further pause of one second. After this pause, the program jumps from the on start-block to the forever-block. Now the light is switched on in an endless loop, it waits 2 seconds, switches off the light again, waits another 2 seconds and then the block starts again from the beginning.

JoyCar.initJoyCar(RevisionMainboard.OnepThree)
JoyCar.brakelight(ToggleSwitch.On)
basic.pause(2000)
JoyCar.brakelight(ToggleSwitch.Off)
basic.pause(1000)
basic.forever(function () {
    JoyCar.light(ToggleSwitch.On)
    basic.pause(2000)
    JoyCar.light(ToggleSwitch.Off)
    basic.pause(2000)
})

The sample code in MicroPython is there because it is also available in MakeCode. This is super handy because if you're already comfortable with MakeCode, it's much easier to get started with MicroPython. By having sample code in both environments, you can continue learning without much interruption. This will help you understand how programming works and you can see how to do the same things in different programming languages.

# Import necessary libraries
from microbit import *
import neopixel

# Define your Joy-Car mainboard revision
joycar_rev = 1.3

# Define object for the lights
np = neopixel.NeoPixel(pin0, 8)

# Define values for the lights
# Values which LEDs are to be activated
headlights = (0, 3)
backlights = (5, 6)
indicator_left = (0, 4)
indicator_right = (3, 7)
indicator_warning = (1, 2, 4, 7)

# Values, which color should be displayed on the LEDs
led_white = (60, 60, 60)
led_red = (60, 0, 0)
led_off = (0, 0, 0)
led_red_br = (255, 0, 0)
led_orange = (100, 35, 0)

# method to activate/deactivate lights
def lights(on = True):
    if on:
        for x, y in zip(headlights, backlights):
            # define white for the headlights
            np[x] = led_white
            # define dark red for the backlights
            np[y] = led_red
    else:
        for x, y in zip(headlights, backlights):
            # define black for the headlights and backlights
            np[x] = led_off
            np[y] = led_off
    np.show()

# method to activate/deactivate the breaking lights
def lightsBreak(on = True):
    if on:
        for x in backlights:
            # define bright red for the backlights
            np[x] = led_red_br
    else:
        for x in backlights:
            # define black for the backlights
            np[x] = led_off
    np.show()

# Activate brake lights
lightsBreak()

# Wait for 2 seconds
sleep(2000)

# Deactivate brake lights
lightsBreak(on = False)

# Wait for 1 second
sleep(1000)

# Loop that remains active
while True:    
    # Activate low beam
    lights()
    
    # Wait for 2 seconds
    sleep(2000)
    
    # Deactivate low beam
    lights(on = False)
    
    # Wait for 2 seconds
    sleep(2000)