J'ai déjà eu l'occasion de présenter mon panneau constituée de 256 LEDs RGB muni du contrôleur WS2812B: nous avions vu comment le brancher à un Arduino Uno et dessiner des formes simples grâces aux bibliothèques FastLED et LEDMatrix.
J'ai toutefois assez d'espace pour afficher des nombres à deux chiffres, et même le nombre "100" (puisque le "1" occupe un peu moins d'espace que les autres chiffres). J'ai donc écrit deux courts programmes qui affichent des nombres entiers s'échelonnant entre 0 et 100.
Bibliothèques
J'ai continué d'utilisé les bibliothèques FastlLED de Daniel Garcia (disponible dans le gestionnaire de bibliothèque) et LEDMatrix de Jorgen - VikingGod.
Sketch #1: Un compte à rebours
Ce premier sketch présente un compte à rebours de 100 à 0.
-
This file contains 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
/* | |
Affichage de nombres sur matrice de LED RGB: | |
Compte à rebours 100 à 0. | |
Pour plus d'infos: | |
https://electroniqueamateur.blogspot.com/2021/03/ecrire-des-nombres-sur-une-matrice-de.html | |
*/ | |
#include <FastLED.h> //https://github.com/FastLED/FastLED | |
#include <LEDMatrix.h> //https://github.com/Jorgen-VikingGod/LEDMatrix | |
#define DATA_PIN 3 // broche 3 de l'Arduino Uno | |
// à modifier selon la matrice utilisée | |
#define COLOR_ORDER GRB | |
#define CHIPSET WS2812B | |
#define MATRIX_WIDTH 16 | |
#define MATRIX_HEIGHT 16 | |
#define MATRIX_TYPE VERTICAL_ZIGZAG_MATRIX | |
#define MATRIX_SIZE (MATRIX_WIDTH*MATRIX_HEIGHT) | |
#define NUMPIXELS MATRIX_SIZE | |
cLEDMatrix<MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_TYPE> leds; | |
int compteur = 100; // valeur initiale maximale: 100 | |
int compteurCouleur = 0; // pour alterner la couleur d'affichage | |
// définition des chiffres ( 0 à 9) | |
const byte police[10][8] = { | |
{ 0x38, 0x44, 0x4C, 0x54, 0x64, 0x44, 0x38 }, // 0 | |
{ 0x10, 0x30, 0x10, 0x10, 0x10, 0x10, 0x38 }, // 1 | |
{ 0x38, 0x44, 0x04, 0x08, 0x10, 0x20, 0x7C }, // 2 | |
{ 0x7C, 0x08, 0x10, 0x08, 0x04, 0x44, 0x38 }, // 3 | |
{ 0x08, 0x18, 0x28, 0x48, 0x7C, 0x08, 0x08 }, // 4 | |
{ 0x7C, 0x40, 0x78, 0x04, 0x04, 0x44, 0x38 }, // 5 | |
{ 0x18, 0x20, 0x40, 0x78, 0x44, 0x44, 0x38 }, // 6 | |
{ 0x7C, 0x04, 0x08, 0x10, 0x20, 0x40, 0x40 }, // 7 | |
{ 0x38, 0x44, 0x44, 0x38, 0x44, 0x44, 0x38 }, // 8 | |
{ 0x38, 0x44, 0x44, 0x3C, 0x04, 0x08, 0x30 } // 9 | |
}; | |
CRGB couleur[] = {CRGB::Blue, CRGB::Yellow, CRGB::Red, CRGB::Green, CRGB:: DarkViolet}; | |
void setup() | |
{ | |
FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds[0], leds.Size()).setCorrection(TypicalSMD5050); | |
FastLED.setCorrection(TypicalLEDStrip); | |
FastLED.setBrightness(50); | |
} | |
void afficheChiffre (int chiffre, int x, int y, CRGB coul) { | |
for (int ligne = 0; ligne < 7; ligne++) | |
{ | |
for (int col = 0; col < 6; col++) | |
{ | |
for (byte masque = 00000001; masque > 0; masque <<= 1) | |
if (B10000000 >> col & police [chiffre][ligne]) { | |
leds.DrawPixel(col + x, ligne + y, coul); | |
} | |
} | |
} | |
} | |
void afficheNombre(int nombre, int x, int y, CRGB coul) { | |
FastLED.clear(true); // on éteint toutes les LEDs | |
int dizaine = nombre / 10; | |
int unite = nombre % 10; | |
// exception: le nombre 100 | |
if (nombre == 100) { | |
afficheChiffre(1, x-2, y, coul); | |
afficheChiffre(0, x + 3, y, coul); | |
afficheChiffre(0, x + 9, y, coul); | |
} | |
else { | |
// affichage de la dizaine | |
if (nombre > 9) { | |
afficheChiffre(dizaine, x + 1, y, coul); | |
} | |
// affichage de l'unité | |
afficheChiffre(unite, x + 7, y, coul); | |
} | |
FastLED.show(); | |
} | |
void loop() | |
{ | |
afficheNombre (compteur, 0, 4, couleur[compteurCouleur]); | |
if (compteur > 0) { | |
compteur--; | |
} | |
compteurCouleur++; | |
if (compteurCouleur > 4){ | |
compteurCouleur = 0; | |
} | |
delay (1000); | |
} |
-
Sketch #2: Affichage d'une valeur analogique
Ce deuxième sketch affiche la valeur mesurée sur l'entrée analogique A0 de l'Arduino Uno. Le résultat est étalonné de 0 à 100%, et est également illustré sur une petite jauge linéaire horizontale.
-
This file contains 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
/* | |
Affichage d'une lecture analogique sur matrice RGB. | |
Pour plus d'infos: | |
https://electroniqueamateur.blogspot.com/2021/03/ecrire-des-nombres-sur-une-matrice-de.html | |
*/ | |
#include <FastLED.h> //https://github.com/FastLED/FastLED | |
#include <LEDMatrix.h> //https://github.com/Jorgen-VikingGod/LEDMatrix | |
#define DATA_PIN 3 // broche 3 de l'Arduino Uno | |
// à modifier selon la matrice utilisée: | |
#define COLOR_ORDER GRB | |
#define CHIPSET WS2812B | |
#define MATRIX_WIDTH 16 | |
#define MATRIX_HEIGHT 16 | |
#define MATRIX_TYPE VERTICAL_ZIGZAG_MATRIX | |
#define MATRIX_SIZE (MATRIX_WIDTH*MATRIX_HEIGHT) | |
#define NUMPIXELS MATRIX_SIZE | |
cLEDMatrix<MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_TYPE> leds; | |
int valeurprecedente; | |
// définition des chiffres ( 0 à 9) | |
const byte police[10][8] = { | |
{ 0x38, 0x44, 0x4C, 0x54, 0x64, 0x44, 0x38 }, // 0 | |
{ 0x10, 0x30, 0x10, 0x10, 0x10, 0x10, 0x38 }, // 1 | |
{ 0x38, 0x44, 0x04, 0x08, 0x10, 0x20, 0x7C }, // 2 | |
{ 0x7C, 0x08, 0x10, 0x08, 0x04, 0x44, 0x38 }, // 3 | |
{ 0x08, 0x18, 0x28, 0x48, 0x7C, 0x08, 0x08 }, // 4 | |
{ 0x7C, 0x40, 0x78, 0x04, 0x04, 0x44, 0x38 }, // 5 | |
{ 0x18, 0x20, 0x40, 0x78, 0x44, 0x44, 0x38 }, // 6 | |
{ 0x7C, 0x04, 0x08, 0x10, 0x20, 0x40, 0x40 }, // 7 | |
{ 0x38, 0x44, 0x44, 0x38, 0x44, 0x44, 0x38 }, // 8 | |
{ 0x38, 0x44, 0x44, 0x3C, 0x04, 0x08, 0x30 } // 9 | |
}; | |
CRGB couleur[] = {CRGB::Blue, CRGB::Yellow, CRGB::Red, CRGB::Green, CRGB:: DarkViolet}; | |
void setup() | |
{ | |
FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds[0], leds.Size()).setCorrection(TypicalSMD5050); | |
FastLED.setCorrection(TypicalLEDStrip); | |
FastLED.setBrightness(50); | |
} | |
void afficheChiffre (int chiffre, int x, int y, CRGB coul) { | |
for (int ligne = 0; ligne < 7; ligne++) | |
{ | |
for (int col = 0; col < 6; col++) | |
{ | |
for (byte masque = 00000001; masque > 0; masque <<= 1) | |
if (B10000000 >> col & police [chiffre][ligne]) { | |
leds.DrawPixel(col + x, ligne + y, coul); | |
} | |
} | |
} | |
} | |
void afficheNombre(int nombre, int x, int y, CRGB coul) { | |
FastLED.clear(true); // on éteint toutes les LEDs | |
int dizaine = nombre / 10; | |
int unite = nombre % 10; | |
// exception: le nombre 100 | |
if (nombre == 100) { | |
afficheChiffre(1, x - 2, y, coul); | |
afficheChiffre(0, x + 3, y, coul); | |
afficheChiffre(0, x + 9, y, coul); | |
} | |
else { | |
// affichage de la dizaine | |
if (nombre > 9) { | |
afficheChiffre(dizaine, x + 1, y, coul); | |
} | |
// affichage de l'unité | |
afficheChiffre(unite, x + 7, y, coul); | |
} | |
FastLED.show(); | |
} | |
void traceJauge (int valeur, int y, CRGB coul) { | |
leds.DrawRectangle(2, y, 13, y + 2, coul); | |
if (valeur >= 95) { | |
leds.DrawPixel(12, y + 1, (CRGB::Red)); | |
} | |
if (valeur >= 85) { | |
leds.DrawPixel(11, y + 1, (CRGB::Red)); | |
} | |
if (valeur >= 75) { | |
leds.DrawPixel(10, y + 1, (CRGB::Red)); | |
} | |
if (valeur >= 65) { | |
leds.DrawPixel(9, y + 1, (CRGB::Red)); | |
} | |
if (valeur >= 55) { | |
leds.DrawPixel(8, y + 1, (CRGB::Red)); | |
} | |
if (valeur >= 45) { | |
leds.DrawPixel(7, y + 1, (CRGB::Red)); | |
} | |
if (valeur >= 35) { | |
leds.DrawPixel(6, y + 1, (CRGB::Red)); | |
} | |
if (valeur >= 25) { | |
leds.DrawPixel(5, y + 1, (CRGB::Red)); | |
} | |
if (valeur >= 15) { | |
leds.DrawPixel(4, y + 1, (CRGB::Red)); | |
} | |
if (valeur >= 5) { | |
leds.DrawPixel(3, y + 1, (CRGB::Red)); | |
} | |
FastLED.show(); | |
} | |
void loop() | |
{ | |
int valeur = map(analogRead(A0), 0, 1023, 0, 100); | |
// on redessine uniquement si la valeur a changé: | |
if (valeur != valeurprecedente) { | |
afficheNombre (valeur, 0, 2, CRGB::Green); | |
traceJauge(valeur, 11, CRGB::Blue); | |
} | |
valeurprecedente = valeur; | |
delay(100); | |
} |
-
Aucun commentaire:
Enregistrer un commentaire