babybigfox
Posts: 15
Joined: Wed Jun 03, 2015 4:13 pm

ap.set_pixels() error

Sun Jun 28, 2015 2:55 pm

Error:
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. ;) Any help?
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
Thanks

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: ap.set_pixels() error

Sun Jun 28, 2015 7:51 pm

From a quick look at your code I can see you're not correctly building up a 64 length pixel array.

Code: Select all

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
]
In the above code, you have line = 0 followed by L = line, so if L is equal to line then L is also equal to 0. You then use L as part of a pixel array called side and because L is an Integer with a value of 0 and not a proper pixel like [0, 0, 0] then set_pixel will throw a ValueError.

Try setting L to [255, 255, 255]

Top tip: It's helpful if you also post the error you get, so then we can see what the error message was and what line of code it fell over on.

Return to “Astro Pi”