coeur.py

micro:bit V2 (BBC micro:bit v2) · Fichier : coeur.py · Langage : MicroPython · Usage : Afficher un motif et du texte défilant sur la matrice 5×5 LED

Premier programme micro:bit : un coeur dessiné pixel par pixel sur la matrice 5×5, suivi d'un texte défilant. Montre display.set_pixel avec la luminosité 0-9, display.show et display.scroll, les bases de l'affichage MicroPython.

Matériel requis

Une BBC micro:bit V2 (nRF52833, matrice 5×5 LED, boutons A/B, accéléromètre + boussole, microphone et haut-parleur) alimentée par USB ou pile. Aucun composant : la matrice 5×5 LED est intégrée à la carte.

Câblage

Aucun câblage. Les coordonnées (x, y) vont de 0 à 4, origine en haut à gauche.

Fonctionnement

set_pixel(x, y, n) allume une LED avec n/10 de luminosité ; show() valide l'image courante et scroll() la fait défiler horizontalement.

Exécution

Ouvrez python.microbit.org, collez le programme MicroPython et envoyez-le vers la carte (la micro:bit apparaît comme lecteur MICROBIT). Appuyez sur reset pour relancer.

Variantes

Remplacez le motif par une flèche ou un smiley, ou utilisez Image("90009:09090:00900:09090:90009") pour un X.

coeur.py

# Motif coeur puis texte deroulant sur la matrice 5x5 de la micro:bit.
from microbit import *

# Coeur dessine ligne par ligne (9 = LED allumee, 0 = eteinte).
coeur = Image("09090:99999:99999:09990:00900")

# Variante manuscrite : liste des pixels a allumer.

# Version simple : display.set_pixel allume LED par LED.
motif = [(1,0),(3,0),(0,1),(1,1),(2,1),(3,1),(4,1),
         (0,2),(1,2),(2,2),(3,2),(4,2),
         (1,3),(2,3),(3,3),(2,4)]

while True:
    display.clear()
    for x, y in motif:
        display.set_pixel(x, y, 9)   # luminosite 9 = max
    display.show()
    sleep(1500)

    # Le texte deroulant occupe toute la matrice, 5 colonnes/px.
    display.scroll('microbit V2', delay=80)
    sleep(200)

    # Le motif officiel est aussi disponible en Image integree :
    display.show(coeur)
    sleep(1000)

    # display.stop() interromprait une animation en cours.
    display.clear()

← Retour à la fiche