MicroPython Tutorial VI
Right, I am still finding it rather difficult, even with sound. Lets add some images. Start by adding a couple more parameters.
from pybricks.parameters import Button, Color, SoundFile, ImageFile, Align
You already got most of these, you just need the last two.
Now let’s add a list of images, again we’re going to restrict ourselves to what’s already there. We got is a set of eyes that move around, let’s use those.
imageList = [ImageFile.MIDDLE_LEFT, ImageFile.MIDDLE_RIGHT, ImageFile.UP, ImageFile.DOWN]
Remember to add spaces between each image definition. Now change the code that shows and reads out the colors.
randomSet()
print(“randomList”,randomList)
blink()
for color in randomList:
brick.display.clear
wait(500)
brick.light(colorList[color])
brick.sound.file(soundList[color])
brick.display.image(imageList[color], Align.CENTER, clear=True)
wait(500)
blink()
Can you spot the difference. Note you could make clear=False, but it makes no sense. You also got a lot options with alignment, although we’re not going to use them with this program, CENTER works perfectly.
Finally there is one more line to add to every if block. Here the complete code.
while len(answerList) != len(randomList):
if Button.LEFT in brick.buttons():
brick.light(colorList[0])
brick.sound.file(soundList[0])
brick.display.image(imageList[0], Align.CENTER, clear=True)
answerList.append(0)
wait(500)
The final touch I would like to add is to change the random routine so that it always returns a distributed set of colors. So that is it never returns the same color twice.
Make it look like this. Within it we make sure we’re referencing the randomList we defined by declaring it global. We then look again and again until we have four distinct members in the random group.
def randomColor(DO):
global randomList
while DO:
seedling = int(round(time.time()))
random.seed(seedling)
colorNo = int(random.randint(0,100) / 25)
print(“seed”,randomList, colorNo)
if colorNo not in randomList:
DO = False
else:
wait(1000)
return(colorNo)
And there you have it. We covered a good deal of ground in the six lessons, we covered buttons, LEDs, sounds and images. We’re going to stay with the buttons for the next few lessons, but focus on motors next time.
Here is the complete code, just in case I forgot something or you did :)
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.parameters import Button, Color, SoundFile, ImageFile, Align
from pybricks.tools import waitimport time
import randomrandomList = []
answerList = []
colorList = [Color.RED, Color.YELLOW, Color.ORANGE, Color.GREEN, Color.BLACK]
soundList = [SoundFile.LEFT, SoundFile.RIGHT, SoundFile.UP, SoundFile.DOWN]
imageList = [ImageFile.MIDDLE_LEFT, ImageFile.MIDDLE_RIGHT, ImageFile.UP, ImageFile.DOWN]def blink():
brick.light(Color.BLACK)
wait(500)def blinkGreen():
for loop in range(4):
brick.light(Color.GREEN)
wait(500)
blink()def blinkRed():
for loop in range(4):
brick.light(Color.RED)
wait(500)
blink()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(True))
wait(1000)def randomColor(DO):
global randomList
while DO:
seedling = int(round(time.time()))
random.seed(seedling)
colorNo = int(random.randint(0,100) / 25)
print("seed",randomList, colorNo)
if colorNo not in randomList:
DO = False
else:
wait(1000)
return(colorNo)randomSet()
print("randomList",randomList)
blink()for color in randomList:
brick.display.clear
wait(500)
brick.light(colorList[color])
brick.sound.file(soundList[color])
brick.display.image(imageList[color], Align.CENTER, clear=True)
wait(500)
blink()while len(answerList) != len(randomList):if Button.LEFT in brick.buttons():
brick.light(colorList[0])
brick.sound.file(soundList[0])
brick.display.image(imageList[0], Align.CENTER, clear=True)
answerList.append(0)
wait(500)if Button.RIGHT in brick.buttons():
brick.light(colorList[1])
brick.sound.file(soundList[1])
brick.display.image(imageList[1], Align.CENTER, clear=True)
answerList.append(1)
wait(500)if Button.UP in brick.buttons():
brick.light(colorList[2])
brick.sound.file(soundList[2])
brick.display.image(imageList[2], Align.CENTER, clear=True)
answerList.append(2)
wait(500)if Button.DOWN in brick.buttons():
brick.light(colorList[3])
brick.sound.file(soundList[3])
brick.display.image(imageList[3], Align.CENTER, clear=True)
answerList.append(3)
wait(500)if answerList == randomList:
brick.display.clear
brick.sound.file(SoundFile.BRAVO)
blinkGreen()
else:
brick.display.clear
brick.sound.file(SoundFile.SORRY)
blinkRed()
The code base has hit that 100 lines, not bad, not bad at all.