Max7219 code help

Hi i am new to arduino i have this code but only one Max7219 i want to switch the logos with buttens can you halp me pleas ?


#include <MD_MAX72xx.h>
#include <SPI.h>

#define delay_t 50 // in milliseconds

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

//Hardware SPI Arduino UNO
// CLK Pin > 13 SCK
// Data Pin > 11 MOSI

#define CS_PIN 8

// Hardware SPI connection
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

byte heart[8] = {0x00, 0x66, 0xFF, 0xFF, 0xFF, 0x7E, 0x3C, 0x18};
byte face[8] = {0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C};
byte face2[8] = {0x00, 0x24, 0x24, 0x24, 0x00, 0x42, 0x3C, 0x00};
byte arrow[8] = {0x18, 0x0C, 0x06, 0xFF, 0xFF, 0x06, 0x0C, 0x18};

void setup() {
mx.begin();
mx.control(MD_MAX72XX::INTENSITY, 0);
mx.clear();
}

void loop() {
drawShape();
}

void drawShape() {
for (int i = 0; i <= 7; i++) {
mx.setRow(0, 0, i, heart[i]);
}
delay(delay_t);
for (int i = 0; i <= 7; i++) {
mx.setRow(1, 1, i, face[i]);
}
delay(delay_t);
for (int i = 0; i <= 7; i++) {
mx.setRow(2, 2, i, face2[i]);
}
delay(delay_t);
for (int i = 0; i <= 7; i++) {
mx.setRow(3, 3, i, arrow[i]);
}
delay(delay_t);
}

Nice!

You need to define a data and clock pin to make it work...

You have:

//Hardware SPI Arduino UNO
// CLK Pin > 13 SCK
// Data Pin > 11 MOSI

#define CS_PIN 8

You need:

//Hardware SPI Arduino UNO
#define DATA_PIN 11
#define CLK_PIN 13
#define CS_PIN 8

if you just have one module, set the MAX_DEVICES to 1

do you see your logos now?

How many buttons do you want?

I want t use two buttens to like switch front and back. Btw thx for the help

I do not understand "front and back." Can you draw a picture and post it here?
Here is a small simulation of your code (press the green play button at the top of the simulation).

Use these files in WOKWI.COM

sketch.ino
// https://forum.arduino.cc/t/max7219-code-help/1106250/

#include <MD_MAX72xx.h>
#include <SPI.h>

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

#define DATA_PIN 11
#define CLK_PIN 13
#define CS_PIN 8

#define BUTTON_PIN 7
int currentBUTTON;
int lastBUTTON;
bool toggleSTATE;

// Hardware SPI connection
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

byte heart[] = {0x00, 0x66, 0xFF, 0xFF, 0xFF, 0x7E, 0x3C, 0x18};
byte face1[] = {0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C};
byte face2[] = {0x00, 0x24, 0x24, 0x24, 0x00, 0x42, 0x3C, 0x00};
byte arrow[] = {0x18, 0x0C, 0x06, 0xFF, 0xFF, 0x06, 0x0C, 0x18};

void setup() {
  Serial.begin(115200);
  mx.begin();
  mx.control(MD_MAX72XX::INTENSITY, 0);
  mx.clear();
  pinMode(BUTTON_PIN, INPUT);
  currentBUTTON = digitalRead(BUTTON_PIN);
}

void loop() {
  lastBUTTON = currentBUTTON;
  currentBUTTON = digitalRead(BUTTON_PIN);
  delay(50);

  // watchButtonState(); // watch the button state in the serial monitor

  if ((lastBUTTON == 1) && (currentBUTTON == 0)) // button is PRESSED and state has CHANGED H to L
  {
    if (toggleSTATE == LOW) // if the toggle state was LOW...
    {
      toggleSTATE = HIGH; // ... set the toggle state HIGH
      drawShape1(); // ... and draw shape 1
    }
    else
    {
      toggleSTATE = LOW; // otherwise set the toggle state LOW
      drawShape2(); // ... and draw shape 2
    }
  }
}

void drawShape1()
{
  for (int i = 0; i <= 7; i++) {
    mx.setRow(0, 0, i, face1[i]);
  }
}

void drawShape2()
{
  for (int i = 0; i <= 7; i++) {
    mx.setRow(0, 0, i, heart[i]);
  }
}

void drawShape3()
{
  for (int i = 0; i <= 7; i++) {
    mx.setRow(2, 2, i, face2[i]);
  }
}

void drawShape4()
{
  for (int i = 0; i <= 7; i++) {
    mx.setRow(3, 3, i, arrow[i]);
  }
}

void watchButtonState() {
  Serial.print(currentBUTTON);
  Serial.print(" ");
  delay(100); // remove this... only here to see button presses
}

diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-uno", "id": "uno", "top": -47.4, "left": -29.4, "attrs": {} },
    {
      "type": "wokwi-max7219-matrix",
      "id": "matrix1",
      "top": -124.2,
      "left": -34.59,
      "attrs": { "chain": "1" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -118.6,
      "left": 182.4,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": 110.4,
      "left": 229.85,
      "rotate": 90,
      "attrs": { "value": "1000" }
    }
  ],
  "connections": [
    [ "matrix1:CS", "uno:8", "green", [ "h38.4", "v0", "h18.8" ] ],
    [ "btn1:2.r", "r1:1", "green", [ "h9.8", "v153.8" ] ],
    [ "r1:2", "uno:GND.3", "black", [ "h-0.16", "v18", "h-119.04" ] ],
    [ "uno:5V", "matrix1:V+", "red", [ "v0" ] ],
    [ "uno:13", "matrix1:CLK", "green", [ "v0" ] ],
    [ "uno:11", "matrix1:DIN", "green", [ "v0" ] ],
    [ "uno:7", "btn1:2.l", "green", [ "v0" ] ],
    [ "uno:5V", "btn1:1.l", "red", [ "v0" ] ],
    [ "uno:GND.1", "matrix1:GND", "black", [ "v0" ] ]
  ],
  "dependencies": {}
}

heart

sorry for the bad drawing i dident have much time

1 Like

Excellent drawing. Start with heart. Click red, see smile (logo 1). Click black, see heart (logo 2)?

yes bc you start with 1 then red bt to 2 then black bt to 1 or from 2 then red bt to 3 and black bt to 2

or start 1 then red 2 again red 3 then black = 2

Okay... here is the simulation with just two logos.

For the third (four, five, et c) logo, re-naming the logos will be needed...

this should switch through all 4 pattern

// show one of several pattern on a MAX7219 display
// https://forum.arduino.cc/t/max7219-code-help/1106250

// 2023-03-31 by noiasca
// code in forum


#include <MD_MAX72xx.h>
#include <SPI.h>

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
const uint8_t MAX_DEVICES = 4;

//Hardware SPI Arduino UNO
// CLK Pin > 13 SCK
// Data Pin > 11 MOSI
//const uint8_t DATA_PIN =11;
const uint8_t CLK_PIN = 13;
const uint8_t  CS_PIN = 8;

// Hardware SPI connection
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

// put your shapes in an array, makes handling easier
byte pattern[][8] {
  {0x00, 0x66, 0xFF, 0xFF, 0xFF, 0x7E, 0x3C, 0x18}, // heart
  {0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C}, // face
  {0x00, 0x24, 0x24, 0x24, 0x00, 0x42, 0x3C, 0x00}, // face2
  {0x18, 0x0C, 0x06, 0xFF, 0xFF, 0x06, 0x0C, 0x18}, // arrow
};

constexpr size_t noOfPattern = sizeof(pattern)/sizeof(pattern[0]); // how many patterns do you have
uint8_t currentPattern = 0;                     // which pattern is active

// a simple button class to debounce the reading
class Button {                                   // class to debounce switches
    const byte buttonPin;                        // the pin for the button
    static constexpr byte debounceDelay = 20;    // the debounce time - one value for all buttons (hence "static")
    const bool active;                           // is the button HIGH or LOW active
    bool lastButtonState = HIGH;                 // previous state of pin
    byte lastDebounceTime = 0;                   // previous debounce time (we just need short debounce delay, so the rollover of the byte variable works fine and spares 3 bytes compared to an unsigned long)

  public:
    Button(byte attachTo, bool active = LOW) : buttonPin(attachTo), active(active) {}

    void begin() {                               // sets the pinMode and optionally activates the internal pullups
      if (active == LOW)
        pinMode(buttonPin, INPUT_PULLUP);
      else
        pinMode(buttonPin, INPUT);
    }

    bool wasPressed() {
      bool buttonState = LOW;
      byte reading = LOW;
      if (digitalRead(buttonPin) == active) reading = HIGH;
      if (((millis() & 0xFF ) - lastDebounceTime) > debounceDelay) {
        if (reading != lastButtonState && lastButtonState == LOW) {
          buttonState = HIGH;
        }
        lastDebounceTime = millis() & 0xFF;   // we store just the last byte from millis()
        lastButtonState = reading;
      }
      return buttonState;
    }
};

Button buttonA(7, HIGH);  // create a button object on this pin, in this case, a HIGH active button

void drawShape(uint8_t currentPattern) {
  for (int i = 0; i <= 7; i++) {
    mx.setRow(0, 0, i, pattern[currentPattern][i]);
  }
}

void setup() {
  Serial.begin(115200);
  mx.begin();
  mx.control(MD_MAX72XX::INTENSITY, 0);
  mx.clear();
  buttonA.begin();
  drawShape(currentPattern);
}

void loop() {
  if (buttonA.wasPressed()) {
    currentPattern++;                                      // next pattern
    if (currentPattern >= noOfPattern) currentPattern = 0; // wrap around
    drawShape(currentPattern);
  }
}

MAX7219 Copy - Wokwi Arduino and ESP32 Simulator

Currently it has just one forward button.
It should be easy to introduce a "back" button. But first recap the code and ask if you have questions.

the problem is u cant use animated Patterns

That's not a technical problem.
Animated patterns can be shown also. In the end an animated pattern is just a pattern which will be triggered by a timer/millis().

Do you know how to loop somethink in a if funktion ?
then i can program animated stuff.

I do know how to make a loop in an if(). Is there something I should do?

The original request was to switch two patterns with a pushbutton.

Solution is the code linked in post #10.

how?

Can you describe your "loop in an if()"? How many logos? Will the all be animated with one button?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.