Member-only story

MicroPython Tutorial IV

Mark Lucking
2 min readAug 1, 2019

Here is the template you built so far, with a few tiny changes.

#!/usr/bin/env pybricks-micropython 
from pybricks import ev3brick as brick
from pybricks.parameters import Button, Color
from pybricks.tools
import wait
import time
import random
randomList = [] colorList = [Color.RED, Color.YELLOW, Color.ORANGE, Color.GREEN, Color.BLACK] def blink(): brick.light(Color.BLACK)
wait(500)
answerList = []
def randomSet():
global randomList
# RED = LEFT button
# YELLOW = RIGHT button
# ORANGE = DOWN button
# GREEN = UP button
# BLACK = starting mode
for loop in range(4):
randomList.append(randomColor())
wait(1000)
def randomColor():
seedling = int(round(time.time()))
random.seed(seedling)
colorNo = int(random.randint(0,100) / 25)
return(colorNo)
randomSet()
print(“randomList”,randomList)
blink()
for colorNo in randomList:
brick.light(colorList[colorNo])
wait(1000)
blink()

The colorNo random line changed and we added an answerList variable.

Now I didn’t include the while True: loop cause that has changed quite a bit. What we want to do is associate a sequence with the buttons. The comment explains the plan.

LEFT will return RED,
RIGHT will return YELLOW,
DOWN will return ORANGE and
UP will return GREEN.

--

--

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

No responses yet