Ever wanted to create your own light-show? With CodeBug it’s easy to create interesting, and intricate and moving patterns of lights. This activity will show you how to create animated flashing patterns on CodeBug with Python.
To use Python with CodeBug, you will connect CodeBug to your computer and use it in tethered mode. Follow the Tethered Mode activity to set up tethered mode.
You will be using the Python shell and also writing Python programs (look at the Tethered Mode guide if you are unsure how to do this).
|Micro USB cable|
With CodeBug it is really easy to create a checkerboard pattern like on a chessboard. You could turn each LED on individually but this would need a lot of typing. Instead there’s an easier way to set the leds in an entire row at once, using the set_row command.
Start up a Python shell in a command terminal:
Import and initialise the CodeBug tether module:
import codebug_tether c = codebug_tether.CodeBug()
Now set the rows:
c.set_row(0,0b10101) c.set_row(1,0b01010) c.set_row(2,0b10101) c.set_row(3,0b01010) c.set_row(4,0b10101)
Does the pattern looks like how you imagined it would? Try changing one of the rows giving it a different value and see how the pattern changes, e.g.
This is a good way to visualise this pattern
The 0b at the start of the value simply let’s you know that this is a binary number.
Binary is a sequence of ones and zeros as these are the two positions of a switch. Everything on a computer is made up from binary. This binary is represented on the screen in a more useful form, for us simple humans to understand, such as numbers letters and even images. The point is that deep down these are all sequences of ones and zeros, and knowing this can help you manipulate them in different ways. When representing numbers, the first bit (single one or zero) represents 1. The second bit represents 2 and the third is 4, each one is double the previous! So to represent the number 5, you would set the first and third bits to 1, as this is 4 and 1 (101) = 5.
So setting a row to 1, you know will light up the first LED as 1 in binary is 00001. You also know that if you times 1 by 2, you will have just the second LED on that row lit up, as 2 in binary is 00010. The table below shows a couple of examples of the binary representation of some numbers. For each row, if you add together the numbers in the blue boxes for each place that has a one in the columns, you will see that each row adds up to the number after the equals sign. The first row for example, has a 0 for the first column and a 1 for the rest of the columns, this gives us 8 + 4 + 2 + 1 = 15!
Moving patterns can be incredibly hypnotic, and they can achieved with some simple clever coding with CodeBug. Lots of programming relies on manipulating numbers and it can be extremely powerful way of doing things.
Let’s start with a simple moving pattern that moves from one side of the screen to the other. Create a new python program file and give it a sensible name such as:
In your program, first import and initialise the CodeBug tether module:
import codebug_tether c = codebug_tether.CodeBug()
Previously you typed out each command to set a row, including the row number and its value, however they can be controlled programmatically with generated values for the row, as this saves typing a long sequence especially with animated patterns that have lots of commands to send.
You will want your animated pattern to repeat forever, so you can hypnotise your friends. To do this use a while loop that is always true.
while True: for i in range(0,5): val = 0b00001 for j in range(0,5): c.set_row(i,val) val=(val*2)
The first iteration of this for loop sets the first LED on the row, and then timeses the row value by 2 to shift the bit, in the next iteration, the row is set to this new value.
You should then perform a sleep to see the effect.
Save and run this code.
This pattern is pretty cool, but it’s a bit jittery as it only goes from one side to the other, but not back again, so it looks like it resets. Let’s fix this!
To make the pattern reverse, you need to repeat your previous for loop, but with a couple of adjustments. Write this new code inside your while loop, outside of your first for loop . If you reverse the range of this new for loop, it will start with the last row, thus reversing the direction the pattern moves in.
for i in reversed(range(0,5)):
Instead of starting the row value at 1 (0b00001 in binary) and timesing it by 2 each time, you should instead start with 16 (0b10000 in binary) which has the last LED of the row on and all the others off. In Python, you can set a variable with the binary value by preceding the value with a ‘0b’. If you then divide by 2 each time, the LED pattern will move across the row in the other direction.
val=0b10000 for j in range(0,5): c.set_row(i,val) val=int((val/2)) time.sleep(.05)
Again, save and run your code. Pretty cool huh?
With structure like this, you can then make your pattern more complex. Try putting in a set_col after each set_row command, with the same values and run it again.
It’s amazing how as little as two lines of code can change a pattern or program in general!
Come up with your own patterns and try writing the code to create them for CodeBug to display. Share them with us on Twitter, we would love to see what you come up with.