MicroBit Python code is correct, but causes errors

I’m pretty sure the code I wrote is correct Python-wise, but for some reason it is impossible to get rid of errors in the MicroBit environment. This is the code:

# setup
activated = False
led.enable(False)
OLED.init(128, 64)
tinkercademy.crash_sensor_setup(DigitalPin.P2)
radio.set_group(123)


# helper functions
def distance():
    """Return the current distance measured by the sonarbit in mm."""
    return Environment.sonarbit_distance(Environment.Distance_Unit.DISTANCE_UNIT_MM, DigitalPin.P1)


# basic.forever() called functions
def on_received_number(receivedNumber):
    """On received number, change the value of activated to True."""
    global activated
    # Check if received number is 19
    if receivedNumber == 19:
        activated = True
    else:
        # Stop everything if not 19
        activated = False
        music.stop_all_sounds()


def beep_when_sensor_pressed():
    """Make sound when crash sensor is pressed."""
    if tinkercademy.crash_sensor():
        music.play(music.tone_playable(262, music.beat(BeatFraction.SIXTEENTH)),
            music.PlaybackMode.UNTIL_DONE)


def radio_communiation():
    """Send info via radio."""
    d = distance()
    radio.send_number(d)


def main_process():
    """Main process."""
    if activated:
        smarthome.relay(DigitalPin.P7, smarthome.RelayStateList.ON)  # make sure the pump is turned OFF
        smarthome.motor_fan(AnalogPin.P12, True, 85)  # rise to the top
        basic.pause(3000)
        initial_distance = distance()  # get the initial distance

        while not tinkercademy.crash_sensor():  # as long as the sensor isn't pressed
            if initial_distance - distance() >= 5:  # if distance has decreased by 5 mm or more
                smarthome.motor_fan(AnalogPin.P12, True, 22)  # go down
        else:
            smarthome.motor_fan(AnalogPin.P12, False)  # turn off the motor once the carriage has crashed

        while distance() > 110:
            smarthome.relay(DigitalPin.P7, smarthome.RelayStateList.OFF)  # turn the pump ON
        else:
            smarthome.relay(DigitalPin.P7, smarthome.RelayStateList.ON)  # turn the pump OFF
            music.play(music.tone_playable(262, music.beat(BeatFraction.DOUBLE)), music.PlaybackMode.UNTIL_DONE)  # beep
            smarthome.motor_fan(AnalogPin.P12, True, 85)  # rise up
            basic.pause(10000)  # wait for the mug to be removed
            smarthome.motor_fan(AnalogPin.P12, True, 22)
            basic.pause(3000)
            smarthome.motor_fan(AnalogPin.P12, False)
            
        activated = False  # set activated to False


# register tasks
radio.on_received_number(on_received_number)
basic.forever(radio_communiation)
basic.forever(beep_when_sensor_pressed)
basic.forever(main_process)

It is the very last line that causes the error “Expected 1 arguments, but got 0.” I also had to redefine the radio_communication method by adding the “d” variable in order to make it work, it otherwise wouldn’t. Can you please tell me where the catch could be? Thanks in advance!

1 Like

I have no clue but two things, welcome to the forums and also maybe change your PFP otherwise the mods will most likely have something to say about it.

Looks like this is code from Tinkercademy, which must have different built in functions than Makecode. For example, an obvious one is:

tinkercademy.crash_sensor_setup(DigitalPin.P2

Obviously, it has the word “tinkercademy” in it, which means that it’s a Tinkercademy exclusive function that doesn’t work in Makecode!

Makecode most likely has it’s own versions of all of these features. For example, if you look in the Extensions section, there is one for using sonar sensors, but it doesn’t work the same way as the Tinkercademy implementation does.

You will probably have to do a lot of research and rewriting to use this code in Makecode.

1 Like

You profile picture is probably fine. The moderators just don’t want kids sharing personal information, but you are probably fine.