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

Help

Mon Jun 29, 2015 3:52 pm

Hi,
I want this snippet of my code to generate a random order of 4 RGB lists:

Code: Select all

colors = [[255 , 0 , 0] , [255 , 255 , 0] , [255 , 0 , 255] , [0 , 255 , 0]]
and display them so that, separately with a 1 second gap, one is on the top strip, one different color on the right, bottom, and left.
I have tried printing the color it comes out with but it just says 0 for all of them.
Here's my code:

Code: Select all

from astro_pi import AstroPi
import random , math
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]

up = 0
down = 0
right = 0
left = 0

def color():
    loop = 1
    while loop <= 4:
        colors = [[255 , 0 , 0] , [255 , 255 , 0] , [255 , 0 , 255] , [0 , 255 , 0]]
        choice = random.choice(colors)
        running = True
        while running:
            if choice == up or choice == down or choice == right or choice == left:
                choice = random.choice(colors)
            else:
                running = False
        if loop == 1:
            choice = up
        elif loop == 2:
            choice = down
        elif loop == 3:
            choice = right
        elif loop == 4:
            choice = left
        print(str(up + down + right + left))
        loop += 1
        
ap.clear()

L = [0 , 0 , 0]

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
]

color()
L = up
ap.set_pixels(side)
ap.set_rotation(0)
sleep(3)
ap.clear()
L = right
ap.set_pixels(side)
ap.set_rotation(90)
sleep(3)
ap.clear()
L = down
ap.set_pixels(side)
ap.set_rotation(180)
sleep(3)
ap.clear()
L = left
ap.set_pixels(side)
ap.set_rotation(270)
sleep(3)
ap.clear()

generation = random.random()
generation = generation * 10

sleep(generation)

R = [0 , 0 , 0]

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()
run = True
#while run == True:
#    if mode == 1:
#    elif mode == 2:
#    elif mode == 3:
#    elif mode == 4:
presstime = time()
timetaken = presstime - starttime
Any help? :)

scotty101
Posts: 3732
Joined: Fri Jun 08, 2012 6:03 pm

Re: Help

Mon Jun 29, 2015 3:59 pm

You are using the variable name 'choice' twice. Once to store the randomly selected color and again to store the value of up,down,left or right.

Change your code to use two separate variables, for example colourChoice, positionChoice.

Also up,down,left and right are all set to 0. There is no way to differentiate between them if they are all set to the same value.

These values for up,down,left and right never change throughout the code, items of this type are called constants. Typical convention is to give constants upper case names, for example

Code: Select all

UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

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

Re: Help

Tue Jun 30, 2015 5:40 am

Thanks :D

Return to “Astro Pi”