Created
July 18, 2023 12:19
-
-
Save ypelletier/54a226f387f2023dfed1e0253fe1a338 to your computer and use it in GitHub Desktop.
Matrice de LEDs RGB 16 X 16 WS2812B et Raspberry Pi Pico. Quelques pixels colorés déambulent au hasard sur la grille.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Matrice de LEDs RGB 16 X 16 WS2812B et Raspberry Pi Pico | |
Promeneurs aléatoires | |
Quelques pixels colorés déambulent au hasard sur la grille | |
Pour lire l'article complet: | |
https://electroniqueamateur.blogspot.com/2023/07/matrice-de-leds-rgb-16-x-16-ws2812b-et.html | |
Bibliothèque pi pico neopixel: | |
https://github.com/blaz-r/pi_pico_neopixel | |
''' | |
from neopixel import Neopixel | |
import time | |
import random | |
nombre_de_LEDs = 256 # matrice 16 X 16 | |
broche = 15 # la matrice est branchée à la broche GP15 du RP Pico | |
pixels = Neopixel(nombre_de_LEDs, 1, broche) | |
# conversion des coordonnées x et y en un numéro de LED | |
def xy_a_nombre(x,y): | |
if (x % 2) == 0: # x est une colonne paire | |
number = x * 16 + y | |
else: # x est une colonne impaire | |
number = x * 16 + 15 - y | |
return number | |
#définissons quelques promeneurs | |
# promeneur [positionx, positiony, couleur] | |
rouge = [random.randint(0,15),random.randint(0,15),(100,0,0)] | |
vert = [random.randint(0,15),random.randint(0,15),(0,100,0)] | |
bleu = [random.randint(0,15),random.randint(0,15),(0,0,100)] | |
magenta = [random.randint(0,15),random.randint(0,15),(100,0,100)] | |
cyan = [random.randint(0,15),random.randint(0,15),(0,100,100)] | |
jaune = [random.randint(0,15),random.randint(0,15),(100,100,0)] | |
promeneurs = [rouge, vert, bleu, magenta, cyan, jaune] | |
while True: | |
for individu in range (0,6): # pour chacun des 6 promeneurs | |
# on efface les positions précédentes: | |
pixels.set_pixel(xy_a_nombre(promeneurs [individu] [0],promeneurs [individu] [1]), (0,0,0)) | |
for xy in range (0,2): # modification de x, et ensuite de y | |
promeneurs [individu] [xy] = promeneurs [individu] [xy] + random.randint(-1,1) | |
# on ne le laisse pas sortir des limites de la matrice: | |
if promeneurs [individu] [xy] < 0: | |
promeneurs [individu] [xy] = 0 | |
if promeneurs [individu] [xy] > 15: | |
promeneurs [individu] [xy] = 15 | |
pixels.set_pixel(xy_a_nombre(promeneurs [individu] [0],promeneurs [individu] [1]), promeneurs [individu] [2]) | |
pixels.show() # nous affichons les promeneurs dans leur nouvelle position | |
time.sleep(.1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment