With the help of these two files, you can turn your joy car and another micro:bit into a remote-controlled car. Load the rc_joycar.py on the micro:bit of your joy-car and the rc_remote.py on the micro:bit you use as a remote control. Tilt the micro:bit you use as a remote control in the direction you want to drive and your joy car will drive in that direction. On button A you use the horn. Button B turns on the light.
# Import of the required modules
import microbit
import radio
# Switching on the radio hardware
radio.on()
# Definition of the variables for data transmission
data0 = "0"
data1 = "0"
data = "00"
# Query loop
while True:
# read out the values of the acceleration sensor
a = microbit.accelerometer.get_values()
# Assign the letters to a direction and store in data0
# micro:bit tilted to the left and not forward beyond the threshold value
if a[0] >= 300 and a[1] <= 300:
data0 = "l"
# micro:bit tilted to the right and not forward beyond the threshold value
elif a[0] <= -300 and a[1] <= 300:
data0 = "r"
# micro:bit tilted forward and not over threshold to the side
elif a[1] <= -300 and a[0] >= -300 and a[0] <= 300:
data0 = "f"
# micro:bit tilted backwards
elif a[1] >= 300:
data0 = "b"
# else 0
else:
data0 = "0"
# Assign the letters to a button and store in data1
if microbit.button_a.is_pressed() == 1 and microbit.button_b.is_pressed() == 0:
data1 = "a"
elif microbit.button_a.is_pressed() == 0 and microbit.button_b.is_pressed() == 1:
data1 = "b"
elif microbit.button_a.is_pressed() == 1 and microbit.button_b.is_pressed() == 1:
data1 = "c"
else:
data1 = "0"
# combine data0 and data1 and store them in data
data = data0 + data1
# send data
radio.send(data)
# Import of the required modules
from microbit import *
import neopixel
import radio
import music
# Initalization of the RGB LEDs
np = neopixel.NeoPixel(pin0, 8)
# Turn on the radio hardware
radio.on()
# Predefinitions for lighting
headlights = (0, 3)
backlights = (5, 6)
led_white = (60, 60, 60)
led_red = (60, 0, 0)
led_off = (0, 0, 0)
# Function to control the motors
def drive(PWM0, PWM1, PWM2, PWM3):
i2c.write(0x70, b'\x02' + bytes([PWM0]))
i2c.write(0x70, b'\x03' + bytes([PWM1]))
i2c.write(0x70, b'\x04' + bytes([PWM2]))
i2c.write(0x70, b'\x05' + bytes([PWM3]))
# Switch on low beam
def lightsON():
for x in headlights:
np[x] = led_white
for x in backlights:
np[x] = led_red
np.show()
# Switch off light
def lightsOFF():
for x in headlights:
np[x] = led_off
for x in backlights:
np[x] = led_off
np.show()
# Control loop
while True:
# Reception via radio hardware is stored in the variable incoming
incoming = radio.receive()
# if incoming is not None (empty) then:
if incoming is not None:
# Query the 1st character for the direction
if incoming[0] == "l":
drive(0, 0, 40, 255)
elif incoming[0] == "r":
drive(40, 255, 0, 0)
elif incoming[0] == "f":
drive(40, 255, 40, 255)
elif incoming[0] == "b":
drive(255, 40, 255, 40)
else:
drive(0, 0, 0, 0)
# Switching the reversing light on and off
if incoming[0] == "b":
np[4] = (led_white)
np.show()
else:
np[4] = (led_off)
np.show()
# Query of the 2nd character for the functions (light and horn).
if incoming[1] == "a":
music.play("c4:1", pin=pin16)
elif incoming[1] == "b":
lightsON()
elif incoming[1] == "c":
lightsON()
else:
lightsOFF()
# if incoming = None, then the joy-car is parked.
# This is usually the case when the joy-car is out of range
# of the remote control or when the remote control is off.
else:
drive(0, 0, 0, 0)
for x in range(0, 8):
np[x] = (50, 0, 0)
np.show()