Need help Smart Car python programming

Hi,
I am teacher in France and I need some help about programming robot in python.
I want to drive a robot “Smart Car” from Elecfreaks , via micropython (with mu editor).
It works fine with MakeCode blocks (with specific blocks for motorbit) but I can’t do it in python.

Here is my code in python :

from microbit import *

directD = pin8      # right dir (forward = 0, reverse = 1)
moteurD = pin1      # rigth motor (PWM)
directG = pin12     # left dir (forward = 0, reverse = 1)
moteurG = pin2      # left motor (PWM)

display.off         # désactivation des 25 leds

def avancer(vitesse):
    directD.write_digital(0)
    directG.write_digital(0)
    moteurD.write_analog(vitesse)
    moteurG.write_analog(vitesse)

def reculer(vitesse):
    directD.write_digital(1)
    directG.write_digital(1)
    moteurD.write_analog(1023-vitesse)
    moteurG.write_analog(1023-vitesse)

for k in range(2):
    avancer(1000)
    sleep(1000)
    reculer(1000)
    sleep(1000)

So what is wrong in this code ?

Thank you very much for your help

Hi @PatriceRabiller and welcome to the forum!

Does your robot move at all with this code or no? Do you have any errors in Python while running this script or no?

Yes it moves but instead of repeating twice “go forward + wait 1sec + go backward +wait for 1sec”
it does that : “move right + wait + move right + wait”

I verified the connection : the pins are correctly used. I did not weld the wires on the engines: I just pinched them. But this is probably not the problem because I was able to run the program written with the blocks :

Aaaaah I found the solution!
Looking for how the code for MakeCode was written, I found on github detailed export functions in JavaScript.

Here is my new code written in python:

from microbit import *

directD = pin8      # sens moteur D (avant = 0, arrière = 1)
moteurD = pin1      # moteur droit (PWM)
directG = pin12     # sens moteur G (avant = 1, arrière = 0)
moteurG = pin2      # moteur gauche (PWM)


def avancer(vitesse):
    directD.write_digital(0)
    directG.write_digital(1)
    moteurD.write_analog(vitesse)
    moteurG.write_analog(vitesse)

def reculer(vitesse):
    directD.write_digital(1)
    directG.write_digital(0)
    moteurD.write_analog(vitesse)
    moteurG.write_analog(vitesse)

for k in range(1):
    avancer(1000)
    sleep(1000)
    reculer(1000)
    sleep(1000)
    avancer(0)

great :slight_smile: