I’m learning to program with python right now, and the unicorn hat is my real testing board. However I can’t seem to find any happy mediums. There are no examples on github which are for the intermediate user and which say a secondary school student could follow. I am talking about simple programs writing functions with loops in them etc. They are all super advanced programming (for me!)
Ok no one taking a bite.
Let’s say I want to create a program which lights up a whole row of pixels in a rainbow set of colours. Then I want that row to stay lit whilst the other rows light up one by one in a 0.3 second delay. When the whole array is lit then the rows go dark one by one until the hat is completely dark, and repeat.
Currently I am writing definitions for this, eg
UH.set_pixel(0,1, 255, 127,0)
I’m writing those functions for each row to light them up and then calling them in a new function eg:
There MUST be an easier way to do this but I don’t know python or programming well enough to do it. Any tips?
There are lots of more complicated ways do to this, where you allow your code to generate each colour rather than specifying them manually. But let’s assume we’re happy with your
rainbowLine function and tweak it slightly so it doesn’t need repeated:
def rainbowLine(x): UH.set_pixel(x,0,255, 0, 0) UH.set_pixel(x,1,255, 127,0) UH.set_pixel(x,2,255, 255,0) UH.set_pixel(x,3,0, 255,0) UH.set_pixel(x.4.0, 0, 255) UH.set_pixel(x,5,75, 0, 130) UH.set_pixel(x,6,139, 0, 255) UH.set_pixel(x,7,255, 255,255)
Instead of creating 8 functions with different
x positions, we now have 1 function which takes an
x position and paints any column we like with rainbow goodness!
So now we’d be writing something like:
def flash(): rainbowLine(0) time.sleep(0.3) rainbowLine(1) time.sleep(0.3) rainbowLine(2) time.sleep(0.3) ...
Yikes! That gets old fast. Fortunately Python is really, really, really good at doing things over and over and over, in fact it’ll repeat things so aggressively and so quickly that it’ll lock itself up into an unresponsive loop if you let it.
Fortunately we have a known quantity of columns we want to light up, so we can iterate over them like so:
def flash(): for x in range(8): rainbowLine(x) time.sleep(0.3)
range to count up to 8, but Python and computers are weird- they count from 0. So
range(8) will give us
0, 1, 2, 3, 4, 5, 6, 7 ( count them, that’s 8 numbers! ).
We can see this directly in interactive Python like so:
>>> list(range(8)) [0, 1, 2, 3, 4, 5, 6, 7]
So, that’ll light up your 8 rows, one by one, with your chosen rainbow pattern!
You, sir, are a gent and a scholar. This is exactly the kind of mini tutorial I needed. Passing a variable into the (x,y,r,g,b) of the set_pixel makes total sense, as does using range to count up a list. I have it working right now.
What interests me is your statement “There are lots of more complicated ways do to this, where you allow your code to generate each colour rather than specifying them manually”. If you could show me one example of this too I would be most grateful!
Okay, you took the bait :D
I ended up writing so much about making rainbows that I had to shift it to a learning portal article. And without further ado, here it is: http://learn.pimoroni.com/tutorial/unicorn-hat/making-rainbows-with-unicorn-hat
Ha, that’s amazing. This is exactly it. Nailed it. I salute you.