diff --git a/micropython_firmware/main.py b/micropython_firmware/main.py index 42e51eb..6a72408 100644 --- a/micropython_firmware/main.py +++ b/micropython_firmware/main.py @@ -1,4 +1,5 @@ import machine +import sys from machine import I2C, Pin import d1motor @@ -13,14 +14,19 @@ class Robot: d1 = Pin(5) d2 = Pin(4) i2c = I2C(scl=d1, sda=d2) - i2c_addr = i2c.scan()[0] - print("i2c scan = {}".format(i2c_addr)) - print("setting up motors ...") - self.m1 = d1motor.Motor(0, i2c, address=i2c_addr) - self.m2 = d1motor.Motor(1, i2c, address=i2c_addr) - self.m1.speed(0) - self.m2.speed(0) - print("motor setup complete!") + i2c_scan = i2c.scan() + if len(i2c_scan) > 0: + i2c_addr = i2c_scan[0] + print("i2c scan = {}".format(i2c_addr)) + print("setting up motors ...") + self.m1 = d1motor.Motor(0, i2c, address=i2c_addr) + self.m2 = d1motor.Motor(1, i2c, address=i2c_addr) + self.m1.speed(0) + self.m2.speed(0) + print("motor setup complete!") + else: + print("error: no i2c interfaces found!") + sys.exit(1) ip = my_ip[0] # setup socket for remote control