- Sun Feb 24, 2019 11:08 am
#28625
Hello,
I checked the Maqueen movement with the makecode linked below.
But I'd like to test the same behavior with Python.
Coding the motors with Python is not easy.
I think the motor is supposed to work with I2C, but can you tell me how to code it in Python to do the same thing as the link below?
https://makecode.microbit.org/v0/06010- ... 4565-76380
Especially, I don't know how to specify addr and buff as the argument value of the write function. #i2c.write(addr, buf, repeat=False)
[*]Running the code below will cause the following error:
File "__main__", line 10, in <module>
OSError: I2C write error -1010
I checked the Maqueen movement with the makecode linked below.
But I'd like to test the same behavior with Python.
Coding the motors with Python is not easy.
I think the motor is supposed to work with I2C, but can you tell me how to code it in Python to do the same thing as the link below?
https://makecode.microbit.org/v0/06010- ... 4565-76380
Especially, I don't know how to specify addr and buff as the argument value of the write function. #i2c.write(addr, buf, repeat=False)
[*]Running the code below will cause the following error:
File "__main__", line 10, in <module>
OSError: I2C write error -1010
Code: Select all
#MicroPython + Grove I2C motor driver
from microbit import *
#i2c.init(freq=100000, sda=pin20, scl=pin19)
while True:
# Forwards
print('Forwards')
display.show(Image.ARROW_N)
i2c.write(0x0f, bytearray([0x82,0x46,0x46]))
i2c.write(0x0f, bytearray([0xaa,0x0a,0x01]))
sleep(3000)
# Stop
i2c.write(0x0f, bytearray([0x82,0x00,0x00]))
i2c.write(0x0f, bytearray([0xaa,0x0a,0x01]))
sleep(2000)
# Backwards
display.show(Image.ARROW_S)
print('Backwards')
i2c.write(0x0f, bytearray([0x82,0x46,0x46]))
i2c.write(0x0f, bytearray([0xaa,0x05,0x01]))
sleep(3000)
# Stop
i2c.write(0x0f, bytearray([0x82,0x00,0x00]))
i2c.write(0x0f, bytearray([0xaa,0x0a,0x01]))
sleep(2000)
# Slow Turn Left
display.show(Image.ARROW_W)
print('Left Turn')
i2c.write(0x0f, bytearray([0x82,0x46,0x46]))
i2c.write(0x0f, bytearray([0xaa,0x09,0x01]))
sleep(3000)
# Stop
i2c.write(0x0f, bytearray([0x82,0x00,0x00]))
i2c.write(0x0f, bytearray([0xaa,0x0a,0x01]))
sleep(2000)
# Sharp Right Turn
display.show(Image.ARROW_E)
print('Right Turn')
i2c.write(0x0f, bytearray([0x82,0x46,0x46]))
i2c.write(0x0f, bytearray([0xaa,0x06,0x01]))
sleep(3000)
# Stop
i2c.write(0x0f, bytearray([0x82,0x00,0x00]))
i2c.write(0x0f, bytearray([0xaa,0x0a,0x01]))
sleep(5000)