MicroPython RC_Joycar-RC_Remote

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 wireless hardware
radio.on()

# Definition of the variables for data transfer
data0 = "0"
data1 = "0"
data = "00"

# Query loop
while True:
    a = microbit.accelerometer.get_values() # read out the values of the acceleration sensor

# Assign the letters to a direction and save in data0
    if a[0] >= 300 and a[1] <= 300: # micro:bit tilted to the left and not forward over the threshold
        data0 = "l"
    elif a[0] <= -300 and a[1] <= 300: # micro:bit tilted to the right and not forward over the threshold
        data0 = "r"
    elif a[1] <= -300 and a[0] >= -300 and a[0] <= 300: # micro:bit tilted forward and not over the threshold to the side
        data0 = "f"
    elif a[1] >= 300: # micro:bit tilted backwards
        data0 = "b"
    else: # else 0
        data0 = "0"

# Assign the letters to a button and save 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"
    data = data0 + data1 # combine data0 and data1 and store them in data
    radio.send(data) # send data
# Import of the required modules
from microbit import *
from neopixel import *
import radio

# Initialization of RGB LEDs
np = NeoPixel(pin0, 8)

# Switching on the wireless hardware
radio.on()

# Predefinitions for the lights
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 dipped beam light
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:
    incoming = radio.receive() # reception via radio hardware is stored in the variable incoming
    if incoming != None: # if incoming is not None (empty) then:
        if incoming[0] == "l": # Query the 1st character for the direction
            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)

        if incoming[0] == "b": # switching on and off the reversing light
            np[4] = (led_white)
            np.show()
        else:
            np[4] = (led_off)
            np.show()

        if incoming[1] == "a": # Query the 2nd character for the functions (light and horn)
#            music.play("c4:1", pin=pin16)
        elif incoming[1] == "b":
            lightsON()
        elif incoming[1] == "c":
            lightsON()
        else:
            lightsOFF()

    else: # 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.
        drive(0, 0, 0, 0)
        for x in range(0, 8):
            np[x] = (50, 0, 0)
        np.show()

Achtung!

Ihr Webbrowser ist veraltet. Wechseln Sie jetzt zu einem aktuellen Browser für mehr Sicherheit, Geschwindigkeit und den besten Komfort auf dieser Seite.