Member-only story

MicroPython Tutorial XVII

Mark Lucking
5 min readAug 27, 2019

Ok, lets back to edges. One of the tasks you’ll surely need your robot to do is square its self up along an edge. A task that with a single colour sensor needs some engineering. What I have done here is mount a colour sensor on cog, that is connected to the medium motor, so that I can move it left and right.

With the accompanying script to drive it shown here.

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import Motor, ColorSensor, GyroSensor
from pybricks.parameters import Port , Color, Stop
from pybricks.tools import print, wait, StopWatch
left_motor = Motor(Port.B)
right_motor = Motor(Port.C)
gyro = GyroSensor(Port.S4)
colorScannedS1 = ColorSensor(Port.S1)
middle_motor = Motor(Port.D)
autoCent = True
autoLeft = False
autoRight = False
middle_motor.reset_angle(0)while True:
if autoCent:
left_motor.dc(17)
right_motor.dc(17)
if colorScannedS1.reflection() < 10 and autoCent:
autoCent = False
left_motor.stop()
right_motor.stop()
# default sensor on the right, now sweep left, 556 degrees moves it left middle_motor.run_angle(90,55,Stop.HOLD,True) while middle_motor.angle() <…

--

--

Mark Lucking
Mark Lucking

Written by Mark Lucking

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Responses (1)