How to control 2 LED matrix (MAX7219) using potentiometer

Hello all, I'm new to Arduino and coding. I am trying to light up the LEDs of two 8x8 matrix using a potentiometer. Matrix1 will light up when the pot value ranges from 0 to 500, and another matrix will glow when the pot value ranges from 500 to 1023. I have tried with one LED matrix and need some guidance on connecting the second matrix and making it working.

Here I have given the code for single LED matrix
Code:

#include <LedControl.h>
int DIN = 11;
int CS =  10;
int CLK = 13;
LedControl lc=LedControl(DIN,CLK,CS,2);

void setup(){
 lc.shutdown(0,false); 
 lc.setIntensity(0,50);
 lc.clearDisplay(0);
}
void printByte(byte character []){
  int i = 0;
  for(i=0;i<8;i++){
    lc.setRow(0,i,character[i]);
  }
}

void loop(){ 
  byte cero[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  int value = analogRead(A0); // read of potentiometer value
  int scale = map(value, 500, 1000, 0, 13); // map function to get brightness

  for (int i=0;i<scale;i++){
    cero[i] = 0xff;
  }

  printByte(cero);

  delay(100);
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Why 500? 512 each side would be even... (0 - 511), (512 - 1023)

I think the first value (0) is the panel number... so the second panel will be (1).

This is how to set/reset one LED in a panel...

lc.setLed(0, 0, 0, state); // panel 0, x = 0, y = 0, ON/OFF

Connecting the panels...
VCC - VCC
GND - GND
DOUT - DIN (data moving left to right)
CS - CS
CLK - CLK

I see how moving the potentiometer on A0 "lowers" and "raises" the LEDs... what is the pattern you want reflected on panel 1?

I think this might be what you had in mind... I added comments...

#include <LedControl.h>
int DIN = 11;
int CS =  10;
int CLK = 13;
LedControl lc = LedControl(DIN, CLK, CS, 2);

void setup() {
  Serial.begin(115200);
  lc.shutdown(0, false);
  lc.setIntensity(0, 50);
  lc.clearDisplay(0);
}

void printByte(byte character [], int panel) {
  int i = 0;
  for (i = 0; i < 8; i++) {
    lc.setRow(panel, i, character[i]);
  }
}

void loop() {
  int panel; // which panel to use
  byte cero[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  int value = analogRead(A0); // read of potentiometer value
  int scale = map(value, 0, 1023, 0, 17); // 16 rows, plus one

  if (value > 512) { // if raw potentiometer is above middle, use panel 0, subtract 8 for row 
    panel = 0;
    scale = scale - 8;
  } else { // if raw potentiometer is below middle, use panel 1, subtract row from 8
    panel = 1;
    scale = 8 - scale;
  }
  for (int i = 0; i < scale; i++) {
    cero[i] = 0xff;
  }
  printByte(cero, panel);
  // delay(100);
}
Put this file in WOKWI.COM diagram.json tab
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 4.8, "left": -86.9, "attrs": {} },
    {
      "type": "wokwi-max7219-matrix",
      "id": "matrix1",
      "top": -143.4,
      "left": -110.18,
      "attrs": { "chain": "2" }
    },
    { "type": "wokwi-potentiometer", "id": "pot1", "top": -10.9, "left": 134.2, "attrs": {} }
  ],
  "connections": [
    [ "nano:11", "matrix1:DIN", "green", [ "v-57.6", "h163.2", "v-38.4" ] ],
    [ "nano:10", "matrix1:CS", "green", [ "v-48", "h144", "v-38.4" ] ],
    [ "nano:13", "matrix1:CLK", "green", [ "v-96", "h153.6", "v-38.4" ] ],
    [ "matrix1:GND", "nano:GND.2", "black", [ "h38.4", "v96", "h-76.8" ] ],
    [ "nano:5V", "matrix1:V+", "red", [ "v19.2", "h86.4", "v-211.2" ] ],
    [ "pot1:GND", "nano:GND.1", "black", [ "v19.2", "h-105.6" ] ],
    [ "pot1:VCC", "nano:5V", "red", [ "v28.8", "h-144.8" ] ],
    [ "nano:A0", "pot1:SIG", "green", [ "v28.8", "h201.6" ] ]
  ],
  "dependencies": {}
}

Thank you so much; this is what I am really trying for.

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