/*
1- Comment utiliser un joystick pour naviguer sur un afficheur MAX7219
2- Comment utiliser des boutons-poussoirs pour naviguer sur un afficheur MAX7219
3- Comment utiliser les touches du clavier pour naviguer sur un afficheur MAX7219
*/
#include <MD_MAX72xx.h>
uint8_t NombreDeModules = 6;
// Type d'appareil utilisé.
#define maxType MD_MAX72XX::PAROLA_HW
MD_MAX72XX matrice = MD_MAX72XX(maxType, 10, NombreDeModules);
// défini la valeur maximale des axes X Y.
uint8_t axe_X = NombreDeModules * 8 - 1;
uint8_t axe_Y = 7;
// X = horizontal, Y = vertical.
int X = 0, Y = 0; // valeur de départ.
// Joystick connexions.
const int vertical = A0, horizontal = A1, select = 2;
// les 5 pins pour les Bouton-poussoirs
const int btn_haut = 3, btn_gauche = 4, btn_droite = 5, btn_bas = 6, btn_enter = 7,
slideSwitch = 8, // Interrupeur à glissière
LedVerte = 9; // Led témoin
int vert = 0, horz = 0; // Variable des axes vertical et horizontal
int pattern = 0; // Variable
bool Switch_On; // Variable boolenne indiquant la position de l'interrupteur à glissière
void setup() {
matrice.begin(); // Démarre la matrice MAX7219
matrice.clear(); // Vide le contenu l'afficheur matriciel
pinMode(vertical, INPUT); // Joystick position vertical
pinMode(horizontal, INPUT); // Joystick position horizontal
pinMode(select, INPUT_PULLUP); // Joystick bouton select
pinMode(slideSwitch, INPUT_PULLUP); // Interrupeur à glissière
pinMode(LedVerte, OUTPUT); // // Led témoin
// configure le mode INPUT aux broches 3 à 7.
for (int pins = 3; pins < 8; pins++) pinMode(pins, INPUT);
}
void loop() {
Switch_On = digitalRead(slideSwitch); // Lecture de l'interrupeur à glissière
if (Switch_On) { // en posision HIGH = 1 ou true
digitalWrite(LedVerte, HIGH); // Alimente la led verte
dessiner(); // appel la fonction perso "dessiner()" en mode manuel
}
if (!Switch_On) { // en posision LOW = 1 ou false
digitalWrite(LedVerte, LOW); // Éteint la led verte.
DessinAuto(); // appel la fonction perso "dessinAuto()" en mode auto
X = 0; Y = 0; // mise à zéro des axex X et Y
}
}
void DessinAuto() { // fonction de dessin programmé
int speed = 50; // Défini la vitesse à 50ms. Vitesse d'éxécution.
switch (pattern) { // Lance 3 phases du dessin
case 0: matrice.clear(); // Vide l'afficheur
X = 0;
for (Y = 1; Y < 7; Y++) { // dessine 12 lignes verticales
matrice.setPoint(Y, X, true);
matrice.setPoint(Y, 7, true);
matrice.setPoint(Y, 8, true);
matrice.setPoint(Y, 15, true);
matrice.setPoint(Y, 16, true);
matrice.setPoint(Y, 23, true);
matrice.setPoint(Y, 24, true);
matrice.setPoint(Y, 31, true);
matrice.setPoint(Y, 32, true);
matrice.setPoint(Y, 39, true);
matrice.setPoint(Y, 40, true);
matrice.setPoint(Y, 47, true);
delay(speed); // vitesse d'éxécution.
}
pattern = 1;
break;
case 1: // dessine 2 lignes horizontale n sens inverse
Y = 0; for (X = 47; X >= 0 ; X--) {
matrice.setPoint(Y, X, true); delay(speed);
}
Y = 7; for (X = 0; X <= 47 ; X++) {
matrice.setPoint(Y, X, true); delay(speed);
}
pattern = 3;
break;
case 3: // dessine des lignes obliques "/ \" dans chaque case
Y = 7; for (X = 47; X >= 41 ; X--) {
matrice.setPoint(Y, X, true); delay(speed); Y--;
}
Y = 0; for (X = 39; X >= 32 ; X--) {
matrice.setPoint(Y, X, true); delay(speed); Y++;
}
Y = 7; for (X = 31; X >= 24 ; X--) {
matrice.setPoint(Y, X, true); delay(speed); Y--;
}
Y = 0; for (X = 23; X >= 16 ; X--) {
matrice.setPoint(Y, X, true); delay(speed); Y++;
}
Y = 7; for (X = 15; X >= 7 ; X--) {
matrice.setPoint(Y, X, true); delay(speed); Y--;
}
Y = 0; for (X = 7; X >= 1 ; X--) {
matrice.setPoint(Y, X, true); delay(speed); Y++;
}
Y = 0; for (X = 0; X <= 7 ; X++) {
matrice.setPoint(Y, X, true); delay(speed); Y++;
}
Y = 7; for (X = 8; X <= 15 ; X++) {
matrice.setPoint(Y, X, true); delay(speed); Y--;
}
Y = 0; for (X = 16; X <= 23 ; X++) {
matrice.setPoint(Y, X, true); delay(speed); Y++;
}
Y = 7; for (X = 24; X <= 30 ; X++) {
matrice.setPoint(Y, X, true); delay(speed);
Y--;
}
Y = 0; for (X = 32; X <= 39 ; X++) {
matrice.setPoint(Y, X, true); delay(speed); Y++;
}
Y = 7; for (X = 40; X <= 47 ; X++) {
matrice.setPoint(Y, X, true); delay(speed); Y--;
}
pattern = 4;
break;
// case 4 efface tout et recommence le dessin
case 4: delay(2000); matrice.clear(); delay(2000);
pattern = 0;
}
matrice.update(); // met à jour chaque point "setPoint(Y, X, true)" de la matrice
}
void dessiner() {
// contôles vertical et horizontale du joystick.
vert = analogRead(vertical); horz = analogRead(horizontal);
// contôles vertical et horizontale par les boutons poussoirs
// et les touches du clavier
if (digitalRead(btn_haut)) Y = max(Y - 1, 0);
if (digitalRead(btn_bas)) Y = min(Y + 1, axe_Y);
if (digitalRead(btn_gauche)) X = min(X + 1, axe_X);
if (digitalRead(btn_droite)) X = max(X - 1, 0);
if (digitalRead(btn_enter)) {
matrice.clear(); Y = 0; X = 0;
}
if (vert < 300) Y = min(Y + 1, axe_Y);
if (vert > 700) Y = max(Y - 1, 0);
if (horz > 700) X = min(X + 1, axe_X);
if (horz < 300) X = max(X - 1, 0);
if (digitalRead(select) == LOW) {
matrice.clear(); Y = 0; X = 0;
}
// met à jour chaque point "setPoint(Y, X, true)" de la matrice à mesure que les variables changent
matrice.setPoint(Y, X, true); matrice.update();
delay(150); // vitesse d'éxécution.
}
Par défaut, l'interrupteur est en position "arrêt"
Il donne l'accès au Joystick, au boutons poussoir et aux touche de ton clavier
En position "marche" il lance une animation. (dessin préprogrammé