8x8 LED Matrix - Just call it art
Just Call It Art!
Before we start to create our snake game, we first need to make a small modification in programming the matrix. With this we will also create some awesome art for the matrix. So let’s get started.
import RPi.GPIO as gpio import max7219.led as led from random import randint import time matrix = led.matrix() while True: for i in range(8): for j in range(8): matrix.pixel(i,j,randint(0,1)) time.sleep(0.0002)
After executing the program you should see some kind of abstract art on your display. This is for your interpretation.
You should be familiar with everything up until the while loop. The goal is quite simple, we want to randomly control every single led in the matrix. We already know that we can generate a random number with
randint() and that we can use the
.pixel() function for turning single leds on and off, but we still need to get coordinates for each pixel. To do this we utilize what is called the for loop. With the for loop we are iterating through all of the columns on the LED Matrix. The for loop can’t do that on its own which requires the need for us to create a list in the prior.
range(8) creates a list with all numbers between 0 and 7. We can use the
print() function on that.
print(range(8)) would give you the following terminal output:
[0, 1, 2, 3, 4, 5, 6, 7]. We can then use the for loop to iterate through all of the items in this so called list. The single item then gets saved in the variable i. So to bring this all together: in line 9 we are generating a list of numbers and then use the for loop to iterate through every item in the list and save it in a variable. The first iteration of the loop i would be 0, then the second time 1 and so on.
We are doing this same thing in line 10 again but are using a different variable this time.
In line 11 we are just calling the pixel function and are using the variables i and j for the x and y coordinates. The third parameter is the
randint(0,1) function which will either turn the LED on (1) or turn the LED off (0).
We are also adding a short delay in line 12 for making the changes more visible. You are welcome to adjust the pause to your liking.
From Art To Images
After creating some fancy art it is time to build something more stable. In the next program we will create our own image. For this we will use a multidimensional list. A normal list is created like this:
liste = [1,5,2]
While a multidimensional list would look like:
mListe = [[1,2], [5,4]]
Both lists have the same structure of numbering items.
print(liste) would be 5. Because we are printing the second element of list (lists start at 0 for counting).
print(mListe) would be [5,4] and
print(mListe) would be 4. This seems at first a bit complicated but is actually quite logical and very useful.
Below we use a multi dimensional list to create an image of an octopus on the matrix.
import max7219.led as led import time device = led.matrix() octopus = [[2,1],[3,1],[4,1],[5,1], [1,2],[3,2],[4,2],[6,2], [1,3],[2,3],[3,3],[4,3],[5,3],[6,3], [1,4],[3,4],[4,4],[5,4],[6,4], [1,5],[6,5], [1,6],[2,6],[3,6],[4,6],[5,6],[6,6], [0,7],[1,7],[3,7],[4,7],[6,7],[7,7]] for i in octopus: device.pixel(i,i,1)
To help make the code more readable we put every row on its own line and saved the whole list into the octopus library. In line 14 and 15 we are printing every element of the list on the LED Matrix. We use the for loop to look at every element of the list. With
i we get every x and y coordinates for the led matrix. In the end we are showing our octopus on the matrix. You can change this code to show any logo or image you want to display.