I am trying to code my entry for the astro pi competition. My coding brings up a error when i use set_pixels. I don't know if it's a bug, or just my code.
This should rotate a randomly generated coloured stripe around the matrix, wait for a generated time and then display a random coloured dot in the middle of the matrix.
Code: Select all
from astro_pi import AstroPi
import random
from time import time
from time import sleep
ap = AstroPi()
ap.clear()
o = ap.get_orientation()
pitch = o["pitch"]
roll = o["roll"]
O = [0 , 0 , 0]
generation = random.random()
generation = generation * 10
if generation < 2.5:
mode = 1
elif generation < 5 and generation > 2.5:
mode = 2
elif generation < 7.5 and generation > 5:
mode = 3
else:
mode = 4
def colour():
generation = random.random()
generation = generation * 10
if generation < 2.5:
colour = [255 , 0 , 0]
elif generation < 5 and generation > 2.5:
colour = [255 , 255 , 0]
elif generation < 7.5 and generation > 5:
colour = [255 , 255 , 255]
else:
colour = [0 , 0 , 255]
if mode == 1:
R = [255 , 0 , 0]
colour()
up = colour
colour()
down = colour
colour()
right = colour
colour()
left = colour
elif mode == 2:
R = [255 , 255 , 0]
colour()
up = colour
colour()
down = colour
colour()
right = colour
colour()
left = colour
elif mode == 3:
R = [255 , 255 , 255]
colour()
up = colour
colour()
down = colour
colour()
right = colour
colour()
left = colour
elif mode == 4:
R = [0 , 0 , 255]
colour()
up = colour
colour()
down = colour
colour()
right = colour
colour()
left = colour
ap.clear()
line = 0
L = line
side = [
L, L, L, L, L, L, L, L,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O
]
line = up
ap.set_pixels(side)
ap.set_rotation(0)
sleep(3)
ap.clear()
line = down
ap.set_pixels(side)
ap.set_rotation(180)
sleep(3)
ap.clear()
line = right
ap.set_pixels(side)
ap.set_rotation(90)
sleep(3)
ap.clear()
line = left
ap.set_pixels(side)
ap.set_rotation(270)
sleep(3)
ap.clear()
generation = random.random()
generation = generation * 10
sleep(generation)
dot = [
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, R, R, O, O, O,
O, O, O, R, R, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O
]
ap.set_pixels(dot)
starttime = time.time()
run = True
#while run == True:
# if mode == 1:
# elif mode == 2:
# elif mode == 3:
# elif mode == 4:
presstime = time.time()
timetaken = presstime - starttime
