Produits

Matrice de points Arduino 8 × 8 - Ocra Electronic
21751
post-template-default,single,single-post,postid-21751,single-format-standard,theme-nouveau,nouveau-core-1.0.4,woocommerce-no-js,ajax_fade,page_not_loaded,,wpb-js-composer js-comp-ver-6.4.1,vc_responsive

Matrice de points Arduino 8 × 8

Matrice de points Arduino 8 × 8

Guide pour 8 × 8 Dot Matrix MAX7219 avec Arduino + Pong Game

La matrice de points que nous allons utiliser dans ce guide est une matrice 8 × 8, ce qui signifie qu’elle comporte 8 colonnes et 8 lignes, elle contient donc un total de 64 LED.

La puce MAX7219 facilite le contrôle de la matrice de points, en utilisant simplement 3 broches numériques de la carte Arduino.

Je pense que la meilleure option est d’acheter la matrice de points avec la puce MAX7219 comme module, cela simplifiera le câblage. Vous pouvez consulter la Ocra electronic et trouver le meilleur prix.

matriciellephoto

Vous pouvez contrôler plusieurs matrices à la fois. Pour cela, il vous suffit de les connecter les uns aux autres, car ils ont des broches des deux côtés pour étendre la matrice de points.

Pièces requises

Pour ce guide, vous aurez besoin de :

Vous pouvez utiliser les liens précédents ou aller directement sur ocra electronic pour trouver toutes les pièces pour vos projets au meilleur prix !

Câblage des broches

Il vous suffit de connecter 5 broches de la matrice de points à votre carte Arduino. Le câblage est assez simple :

Broche matricielleCâblage à Arduino Uno
GNDGND
VCC5V
VACARMEBroche numérique
CSBroche numérique
CLKBroche numérique

Comment contrôler la matrice de points avec Arduino

Pour faciliter le contrôle de la matrice de points, vous devez télécharger et installer dans votre IDE Arduino la bibliothèque LedControl. Pour installer la bibliothèque, procédez comme suit :

  1. Cliquez ici pour télécharger la bibliothèque LedControl . Vous devriez avoir un dossier .zip dans vos téléchargements
  2. Décompressez le dossier .zip et vous devriez obtenir le dossier LedControl-master
  3. Renommez votre dossier de LedControl-maître à LedControl
  4. Déplacez le dossier LedControl  dans votre dossier de bibliothèques d’  installation Arduino IDE 
  5. Enfin, rouvrez votre IDE Arduino

Utilisation des fonctions de la bibliothèque LedControl

Le moyen le plus simple d’afficher quelque chose sur la matrice de points est d’utiliser les fonctions setLed() , setRow() ou setColumn() . Ces fonctions vous permettent de contrôler une seule led, une ligne ou une colonne à la fois.

Voici les paramètres de chaque fonction :

setLed(addr, row, col, state)

  • addr est l’adresse de votre matrice, par exemple, si vous n’avez qu’une seule matrice, l’int addr sera zéro.
  • la rangée est la rangée où se trouve la led
  • col est la colonne où se trouve la led
  • Etat
    • C’est vrai ou 1 si tu veux allumer la led
    • C’est faux ou 0 si tu veux l’éteindre

setRow (adresse, ligne, valeur)

setCol(adr, colonne, valeur)

Indice

Comme indiqué précédemment, cette matrice comporte 8 colonnes et 8 lignes. Chacun est indexé de 0 à 7. Voici un chiffre pour mieux comprendre :

indice

Si vous souhaitez afficher quelque chose dans la matrice, il vous suffit de savoir si dans une ligne ou une colonne déterminée, les LED qui sont allumées ou éteintes.

Par exemple, si vous souhaitez afficher un visage heureux, voici ce que vous devez faire :

visages

Code

Voici un croquis simple qui affiche trois types de visages : un visage triste, un visage neutre et un visage heureux. Téléchargez le code suivant sur votre tableau :

/*
 Created by Rui Santos
 
 All the resources for this project:
 https://randomnerdtutorials.com/
*/

#include "LedControl.h"
#include "binary.h"

/*
 DIN connects to pin 12
 CLK connects to pin 11
 CS connects to pin 10 
*/
LedControl lc=LedControl(12,11,10,1);

// delay time between faces
unsigned long delaytime=1000;

// happy face
byte hf[8]= {B00111100,B01000010,B10100101,B10000001,B10100101,B10011001,B01000010,B00111100};
// neutral face
byte nf[8]={B00111100, B01000010,B10100101,B10000001,B10111101,B10000001,B01000010,B00111100};
// sad face
byte sf[8]= {B00111100,B01000010,B10100101,B10000001,B10011001,B10100101,B01000010,B00111100};

void setup() {
  lc.shutdown(0,false);
  // Set brightness to a medium value
  lc.setIntensity(0,8);
  // Clear the display
  lc.clearDisplay(0);  
}

void drawFaces(){
  // Display sad face
  lc.setRow(0,0,sf[0]);
  lc.setRow(0,1,sf[1]);
  lc.setRow(0,2,sf[2]);
  lc.setRow(0,3,sf[3]);
  lc.setRow(0,4,sf[4]);
  lc.setRow(0,5,sf[5]);
  lc.setRow(0,6,sf[6]);
  lc.setRow(0,7,sf[7]);
  delay(delaytime);
  
  // Display neutral face
  lc.setRow(0,0,nf[0]);
  lc.setRow(0,1,nf[1]);
  lc.setRow(0,2,nf[2]);
  lc.setRow(0,3,nf[3]);
  lc.setRow(0,4,nf[4]);
  lc.setRow(0,5,nf[5]);
  lc.setRow(0,6,nf[6]);
  lc.setRow(0,7,nf[7]);
  delay(delaytime);
  
  // Display happy face
  lc.setRow(0,0,hf[0]);
  lc.setRow(0,1,hf[1]);
  lc.setRow(0,2,hf[2]);
  lc.setRow(0,3,hf[3]);
  lc.setRow(0,4,hf[4]);
  lc.setRow(0,5,hf[5]);
  lc.setRow(0,6,hf[6]);
  lc.setRow(0,7,hf[7]);
  delay(delaytime);
}

void loop(){
  drawFaces();
}

Au final, vous aurez quelque chose comme ça :

gif_face_final

Jeu de Pong

Le jeu de pong que vous allez essayer a été créé par Alessandro Pasotti .

Pour le jeu de pong, il suffit d’ajouter un potentiomètre de 1k ohm au schéma précédent. Assemblez le nouveau circuit comme indiqué ci-dessous :

pongping_bb

Code

Ensuite, téléchargez le code suivant sur votre carte Arduino :

/*  
 *   Play pong on an 8x8 matrix - project from itopen.it
 */
 
#include "LedControl.h"
#include "Timer.h"
 
#define POTPIN A5 // Potentiometer
#define PADSIZE 3
#define BALL_DELAY 200
#define GAME_DELAY 10
#define BOUNCE_VERTICAL 1
#define BOUNCE_HORIZONTAL -1
#define NEW_GAME_ANIMATION_SPEED 50
#define HIT_NONE 0
#define HIT_CENTER 1
#define HIT_LEFT 2
#define HIT_RIGHT 3
 
//#define DEBUG 1
 
byte sad[] = {
B00000000,
B01000100,
B00010000,
B00010000,
B00000000,
B00111000,
B01000100,
B00000000
};
 
byte smile[] = {
B00000000,
B01000100,
B00010000,
B00010000,
B00010000,
B01000100,
B00111000,
B00000000
};
 
Timer timer;
 
LedControl lc = LedControl(12,11,10,1);
 
byte direction; // Wind rose, 0 is north
int xball;
int yball;
int yball_prev;
byte xpad;
int ball_timer;
 
void setSprite(byte *sprite){
    for(int r = 0; r < 8; r++){
        lc.setRow(0, r, sprite[r]);
    }
}
 
void newGame() {
    lc.clearDisplay(0);
    // initial position
    xball = random(1, 7);
    yball = 1;
    direction = random(3, 6); // Go south
    for(int r = 0; r < 8; r++){
        for(int c = 0; c < 8; c++){
            lc.setLed(0, r, c, HIGH);
            delay(NEW_GAME_ANIMATION_SPEED);
        }
    }
    setSprite(smile);
    delay(1500);
    lc.clearDisplay(0);
}
 
void setPad() {
    xpad = map(analogRead(POTPIN), 0, 1020, 8 - PADSIZE, 0);
}
 
void debug(const char* desc){
#ifdef DEBUG
    Serial.print(desc);
    Serial.print(" XY: ");
    Serial.print(xball);
    Serial.print(", ");
    Serial.print(yball);
    Serial.print(" XPAD: ");
    Serial.print(xpad);
    Serial.print(" DIR: ");
    Serial.println(direction);
#endif
}
 
int checkBounce() {
    if(!xball || !yball || xball == 7 || yball == 6){
        int bounce = (yball == 0 || yball == 6) ? BOUNCE_HORIZONTAL : BOUNCE_VERTICAL;
#ifdef DEBUG
        debug(bounce == BOUNCE_HORIZONTAL ? "HORIZONTAL" : "VERTICAL");
#endif
        return bounce;
    }
    return 0;
}
 
int getHit() {
    if(yball != 6 || xball < xpad || xball > xpad + PADSIZE){
        return HIT_NONE;
    }
    if(xball == xpad + PADSIZE / 2){
        return HIT_CENTER;
    }
    return xball < xpad + PADSIZE / 2 ? HIT_LEFT : HIT_RIGHT;
}
 
bool checkLoose() {
    return yball == 6 && getHit() == HIT_NONE;
}
 
void moveBall() {
    debug("MOVE");
    int bounce = checkBounce();
    if(bounce) {
        switch(direction){
            case 0:
                direction = 4;
            break;
            case 1:
                direction = (bounce == BOUNCE_VERTICAL) ? 7 : 3;
            break;
            case 2:
                direction = 6;
            break;
            case 6:
                direction = 2;
            break;
            case 7:
                direction = (bounce == BOUNCE_VERTICAL) ? 1 : 5;
            break;
            case 5:
                direction = (bounce == BOUNCE_VERTICAL) ? 3 : 7;
            break;
            case 3:
                direction = (bounce == BOUNCE_VERTICAL) ? 5 : 1;
            break;
            case 4:
                direction = 0;
            break;
        }
        debug("->");
    }
 
    // Check hit: modify direction is left or right
    switch(getHit()){
        case HIT_LEFT:
            if(direction == 0){
                direction =  7;
            } else if (direction == 1){
                direction = 0;
            }
        break;
        case HIT_RIGHT:
            if(direction == 0){
                direction = 1;
            } else if(direction == 7){
                direction = 0;
            }
        break;
    }
 
    // Check orthogonal directions and borders ...
    if((direction == 0 && xball == 0) || (direction == 4 && xball == 7)){
        direction++;
    }
    if(direction == 0 && xball == 7){
        direction = 7;
    }
    if(direction == 4 && xball == 0){
        direction = 3;
    }
    if(direction == 2 && yball == 0){
        direction = 3;
    }
    if(direction == 2 && yball == 6){
        direction = 1;
    }
    if(direction == 6 && yball == 0){
        direction = 5;
    }
    if(direction == 6 && yball == 6){
        direction = 7;
    }
    
    // "Corner" case
    if(xball == 0 && yball == 0){
        direction = 3;
    }
    if(xball == 0 && yball == 6){
        direction = 1;
    }
    if(xball == 7 && yball == 6){
        direction = 7;
    }
    if(xball == 7 && yball == 0){
        direction = 5;
    }
 
    yball_prev = yball;
    if(2 < direction && direction < 6) {
        yball++;
    } else if(direction != 6 && direction != 2) {
        yball--;
    }
    if(0 < direction && direction < 4) {
        xball++;
    } else if(direction != 0 && direction != 4) {
        xball--;
    }
    xball = max(0, min(7, xball));
    yball = max(0, min(6, yball));
    debug("AFTER MOVE");
}
 
void gameOver() {
    setSprite(sad);
    delay(1500);
    lc.clearDisplay(0);
}
 
void drawGame() {
    if(yball_prev != yball){
        lc.setRow(0, yball_prev, 0);
    }
    lc.setRow(0, yball, byte(1 << (xball)));
    byte padmap = byte(0xFF >> (8 - PADSIZE) << xpad) ;
#ifdef DEBUG
    //Serial.println(padmap, BIN);
#endif
    lc.setRow(0, 7, padmap);
}
 
void setup() {
  // The MAX72XX is in power-saving mode on startup,
  // we have to do a wakeup call
  pinMode(POTPIN, INPUT);
 
  lc.shutdown(0,false);
  // Set the brightness to a medium values
  lc.setIntensity(0, 8);
  // and clear the display
  lc.clearDisplay(0);
  randomSeed(analogRead(0));
#ifdef DEBUG
  Serial.begin(9600);
  Serial.println("Pong");
#endif
  newGame();
  ball_timer = timer.every(BALL_DELAY, moveBall);
}
 
void loop() {
    timer.update();
    // Move pad
    setPad();
#ifdef DEBUG
    Serial.println(xpad);
#endif
    // Update screen
    drawGame();
    if(checkLoose()) {
        debug("LOOSE");
        gameOver();
        newGame();
    }
    delay(GAME_DELAY);
}

Démonstration

Voici la dernière démonstration de moi jouant au jeu de pong. S’amuser!

gif_game_f

Fin

Avez-vous déjà utilisé la matrice de points dans vos projets Arduino ?

Votre Panier
Il n'y a pas d'articles dans le panier !
Continuer les achats
0