16*16 led matrix help

/* This sketch detects analog signals from the potentiometers ud and lr
  and sends them to the serial monitor.Pixels on led matrix are selected or deselected by buttons a and b */

//initialize variable for x and y values on the potentiometers and the pushbuttons at digital pin 4 and 7
int UD;
int LR ;
int button_a = 4;
int button_b = 7;


bool stored_state [16][16];

int last_x, last_y;

#include "LedControl.h" //  importing the library
// DIN to 10, CLK to 12, CS to 11
LedControl lc = LedControl(11, 13, 10, 4);

void setup() {
  Serial.begin(9600);
  lc.shutdown(0, false); // turn off power saving, enables display
  lc.setIntensity(0, 0); // sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen
  
  lc.shutdown(1, false); // turn off power saving, enables display
  lc.setIntensity(1, 0); // sets brightness (0~15 possible values)
  lc.clearDisplay(1);// clear screen

  lc.shutdown(2, false); // turn off power saving, enables display
  lc.setIntensity(2, 0); // sets brightness (0~15 possible values)
  lc.clearDisplay(2);// clear screen

  lc.shutdown(3, false); // turn off power saving, enables display
  lc.setIntensity(3, 0); // sets brightness (0~15 possible values)
  lc.clearDisplay(3);// clear screen
  
  pinMode(button_a, INPUT); // button is input
  digitalWrite(button_a, HIGH); // initalize button as ON
  pinMode(button_b, INPUT); // button is input
  digitalWrite(button_b, HIGH); // initalize button as ON


}

void loop() {
  UD = analogRead(A1); // read analog value at pin A1 and stores as UD
  LR = analogRead(A0);  // read analog value at pin A0 and stores as LR
  int stateA = digitalRead(button_a); //determines the state of button_a (HIGH or LOW)
  int stateB = digitalRead(button_b); //determines the state of button_b (HIGH or LOW)
  // scales down the value from 0-1023 to 0-15
  char x_translate = map(LR, 0, 1023, 15, 0);
  char y_translate = map(UD, 0, 1023, 0, 15);

  if ( (last_x != x_translate) || (last_y != y_translate) )
  {
    // the position has moved so we need to restore previous state
    lc.setLed(0, last_x, last_y, stored_state [last_x][last_y] );
    lc.setLed(1, last_x, last_y, stored_state [last_x][last_y] );
    lc.setLed(2, last_x, last_y, stored_state [last_x][last_y] );
    lc.setLed(3, last_x, last_y, stored_state [last_x][last_y] );
  }

  last_x = x_translate;
  last_y = y_translate;

  Serial.print ("Button_a=");
  Serial.print(stateA, DEC);
  Serial.print ("Button_b=");
  Serial.print(stateB, DEC);
  Serial.print(", UD = ");
  Serial.print(UD, DEC);
  Serial.print(", LR = ");
  Serial.print(LR, DEC);
  Serial.print(", x = ");
  Serial.print(x_translate, DEC);
  Serial.print(", y = ");
  Serial.println(y_translate, DEC);

  // clears display whenever a new value is updated
  /// --> dont do this!  lc.clearDisplay(0);

  // just set led in new potentiometers position
  lc.setLed(0, x_translate, y_translate, true);
  lc.setLed(1, x_translate, y_translate, true);
  lc.setLed(2, x_translate, y_translate, true);
  lc.setLed(3, x_translate, y_translate, true);

  // if button_a is pressed, store it
  if (stateA == 1) {
    stored_state [last_x][last_y] = true;
  }

  // if button_b is pressed, delete it
  if (stateB == 1) {
    stored_state [last_x][last_y] = false;
  }

  delay(150); //Mess with delay to tweak accuracy
}

Hi
I'm using this code to control 4 8x8 led matrix(Max7219) and the idea is to use two potentiometers to control left,right,up and down of the cursor and two buttons for on and off of the selected led.
This works fine but I have the same display repeated on all 4 matrices rather than one big matrix.

Any help or advice greatly appreciated
Rich

Don't they need each their own CS line?

In the examples I've seen cs and clk are wired in parrallel and the dig in series but I'll give it a try

Ah, I see. Got confused by the typical SPI pin names, but it's not SPI.

Please post schematic of how you have your project wired.

Here's how I have the matrices arranged and wired

I guess that if you are doing something like this:

char x_translate = map(LR, 0, 1023, 15, 0) ;
char y_translate = map(UD, 0, 1023, 0, 15) ;

Which is delivering values in the range 0 to 15, you then have to address the relevant display 0 to 3 based on a range , something like:

if ( x_translate < 8 && y_translate < 8 ) lc.setLed( 0 , x_translate , y_translate , . . . ) ;
if ( x_translate >= 8 && y_translate < 8 ) lc.setLed( 1 , x_translate - 8 , y_translate , . . . ) ;
if ( x_translate < 8 && y_translate >= 8 ) lc.setLed( 2, x_translate , y_translate - 8 , . . . ) ;
if ( x_translate >= 8 && y_translate >= 8 ) lc.setLed( 3, x_translate - 8 , y_translate - 8, . . . ) ;

As it is, you appear to be treating all 4 displays in parallel.

/* This sketch detects analog signals from the potentiometers ud and lr
  and sends them to the serial monitor.Pixels on led matrix are selected or deselected by buttons a and b */

//initialize variable for x and y values on the potentiometers and the pushbuttons at digital pin 4 and 7
int UD;
int LR ;
int button_a = 4;
int button_b = 7;


bool stored_state [8][8];

int last_x, last_y;

#include "LedControl.h" //  importing the library
// DIN to 10, CLK to 13, CS to 11
LedControl lc = LedControl(11, 13, 10, 4);

void setup() {
  Serial.begin(9600);
  lc.shutdown(0, false); // turn off power saving, enables display
  lc.setIntensity(0, 0); // sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen

  lc.shutdown(1, false); // turn off power saving, enables display
  lc.setIntensity(1, 0); // sets brightness (0~15 possible values)
  lc.clearDisplay(1);// clear screen

  lc.shutdown(2, false); // turn off power saving, enables display
  lc.setIntensity(2, 0); // sets brightness (0~15 possible values)
  lc.clearDisplay(2);// clear screen

  lc.shutdown(3, false); // turn off power saving, enables display
  lc.setIntensity(3, 0); // sets brightness (0~15 possible values)
  lc.clearDisplay(3);// clear screen

  pinMode(button_a, INPUT); // button is input
  digitalWrite(button_a, HIGH); // initalize button as ON
  pinMode(button_b, INPUT); // button is input
  digitalWrite(button_b, HIGH); // initalize button as ON


}

void loop() {
  UD = analogRead(A1); // read analog value at pin A1 and stores as UD
  LR = analogRead(A0);  // read analog value at pin A0 and stores as LR
  int stateA = digitalRead(button_a); //determines the state of button_a (HIGH or LOW)
  int stateB = digitalRead(button_b); //determines the state of button_b (HIGH or LOW)
  // scales down the value from 0-1023 to 0-15
  char x_translate = map(LR, 0, 1019, 15, 0);
  char y_translate = map(UD, 0, 1019, 0, 15);

  if ( (last_x != x_translate) || (last_y != y_translate) )
  {
    // the position has moved so we need to restore previous state
    lc.setLed(0, last_x, last_y, stored_state [last_x][last_y] );
    lc.setLed(1, last_x, last_y, stored_state [last_x][last_y] );
    lc.setLed(2, last_x, last_y, stored_state [last_x][last_y] );
    lc.setLed(3, last_x, last_y, stored_state [last_x][last_y] );
  }

  last_x = x_translate;
  last_y = y_translate;

  Serial.print ("Button_a=");
  Serial.print(stateA, DEC);
  Serial.print ("Button_b=");
  Serial.print(stateB, DEC);
  Serial.print(", UD = ");
  Serial.print(UD, DEC);
  Serial.print(", LR = ");
  Serial.print(LR, DEC);
  Serial.print(", x = ");
  Serial.print(x_translate, DEC);
  Serial.print(", y = ");
  Serial.println(y_translate, DEC);

  // clears display whenever a new value is updated
  /// --> dont do this!  lc.clearDisplay(0);

  // just set led in new potentiometers position

  if ( x_translate < 8 && y_translate < 8 )   lc.setLed( 0, x_translate,  y_translate, true);
  if ( x_translate >= 8 && y_translate < 8 )  lc.setLed( 1, x_translate-8,y_translate, true);
  if ( x_translate < 8 && y_translate >= 8 )  lc.setLed( 2, x_translate,  y_translate -8, true);
  if ( x_translate >= 8 && y_translate >= 8 ) lc.setLed( 3, x_translate-8,y_translate -8, true);




// if button_a is pressed, store it
if (stateA == 1) {
  stored_state [last_x][last_y] = true;
}

// if button_b is pressed, delete it
if (stateB == 1) {
  stored_state [last_x][last_y] = false;
}


delay(10); //Mess with delay to tweak accuracy
}

Great! Thanks for that help.I've incorporated that code into the sketch and it is identifying the 4 matrices like this

3...1

2...0

So i have up down and left right :slight_smile:

When I was using a single matrix I could press one of the buttons to select or cancel the led in it's current position.Two things are happening now.

  1. If I store a led on matrix 0 it is also stored on matrix 1,2 and 3
  2. When I move the potentiometers into a position on matrix 1,2 and 3 each led in that position gets automatically stored until it hits the same postition on matrix 0 when it gets cancelled.

For example if i turn the potentiometer for right to left it scrolls the leds on matrix 0 but they all light up on that row on matrix 2

So it seems I have a problem with the button state corresponding to the matrices 1,2 and 3

Am I on the right track here?

You've also got to make another change, something like this:

 if ( (last_x != x_translate) || (last_y != y_translate) )
  {
    // the position has moved so we need to restore previous state
    if ( last_x < 8  && last_y < 8 )  lc.setLed(0, last_x,     last_y,     stored_state [last_x][last_y] );
    if ( last_x >= 8 && last_y < 8 )  lc.setLed(1, last_x - 8, last_y,     stored_state [last_x - 8][last_y] );
    if ( last_x < 8  && last_y >= 8 ) lc.setLed(2, last_x,     last_y - 8, stored_state [last_x][last_y -8] );
    if ( last_x >= 8 && last_y >= 8 ) lc.setLed(3, last_x - 8, last_y - 8, stored_state [last_x -8][last_y -8] );
  }

Once again thanks for that.I can now scroll left right up and down.Great.
I'm still having a problem where it will only select or deselect leds on the first(0) matrix.
I'm finding this hard to understand why it would only store on one matrix as the stored state {last_x} {last_Y} seems to work.
Almost there :slight_smile:

Try this:

bool stored_state [16][16]; // was bool stored_state [8][8];

I've changed the bool stored state to 16 but I still have the same problem.
I've also noticed if I save a pixel on matrix 0 if i select the same row/column on any of the other matrices that pixel appears saved too. :confused:

Post your amended code. In your original code, everything appeared to work correctly if you looked at only the first matrix. Is that correct?

/* This sketch detects analog signals from the potentiometers
  and sends it to the serial monitor */

//initialize variable for x and y values on the potentiometers and the pushbuttons at digital pin 4 and 7
int UD;
int LR ;
int button_a = 7;
int button_b = 4;


bool stored_state [16][16];   // was bool stored_state [8][8];

int last_x, last_y;

#include "LedControl.h" //  importing the library
// DIN to 10, CLK to 12, CS to 11
LedControl lc = LedControl(11, 13, 10, 4);

void setup() {
  Serial.begin(9600);
  lc.shutdown(0, false); // turn off power saving, enables display
  lc.setIntensity(0, 2); // sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen

  lc.shutdown(1, false); // turn off power saving, enables display
  lc.setIntensity(1, 2); // sets brightness (0~15 possible values)
  lc.clearDisplay(1);// clear screen

  lc.shutdown(2, false); // turn off power saving, enables display
  lc.setIntensity(2, 2); // sets brightness (0~15 possible values)
  lc.clearDisplay(2);// clear screen

  lc.shutdown(3, false); // turn off power saving, enables display
  lc.setIntensity(3, 2); // sets brightness (0~15 possible values)
  lc.clearDisplay(3);// clear screen

  pinMode(button_a, INPUT); // button is input
  digitalWrite(button_a, HIGH); // initalize button as ON
  pinMode(button_b, INPUT); // button is input
  digitalWrite(button_b, HIGH); // initalize button as ON


}

void loop() {
  UD = analogRead(A1); // read analog value at pin A1 and stores as UD
  LR = analogRead(A0);  // read analog value at pin A0 and stores as LR
  int stateA = digitalRead(button_a); //determines the state of button_a (HIGH or LOW)
  int stateB = digitalRead(button_b); //determines the state of button_b (HIGH or LOW)
  // scales down the value from 0-1023 to 0-7
  char x_translate = map(LR, 0, 1015, 15, 0);
  char y_translate = map(UD, 0, 1015, 0, 15);



  if ( (last_x != x_translate) || (last_y != y_translate) )
  {
    // the position has moved so we need to restore previous state
    if ( last_x < 8  && last_y < 8 )  lc.setLed(0, last_x,     last_y,     stored_state [last_x][last_y] );
    if ( last_x >= 8 && last_y < 8 )  lc.setLed(1, last_x - 8, last_y,     stored_state [last_x - 8][last_y] );
    if ( last_x < 8  && last_y >= 8 ) lc.setLed(2, last_x,     last_y - 8, stored_state [last_x][last_y - 8] );
    if ( last_x >= 8 && last_y >= 8 ) lc.setLed(3, last_x - 8, last_y - 8, stored_state [last_x - 8][last_y - 8] );
  }



  last_x = x_translate;
  last_y = y_translate;



  // clears display whenever a new value is updated
  /// --> dont do this!  lc.clearDisplay(0);

  // just set led in new potentiometer position
  if ( x_translate < 8 && y_translate < 8 ) lc.setLed( 0 , x_translate ,  y_translate , true ) ;
  if ( x_translate >= 8 && y_translate < 8 ) lc.setLed( 1 , x_translate - 8 ,  y_translate , true) ;
  if ( x_translate < 8 && y_translate >= 8 ) lc.setLed( 2, x_translate ,  y_translate - 8 , true ) ;
  if ( x_translate >= 8 && y_translate >= 8 ) lc.setLed( 3, x_translate - 8 ,  y_translate - 8, true ) ;

  // if button_a is pressed, store it
  if (stateA == 1) {
    stored_state [last_x][last_y] = true;
  }

  // if button_b is pressed, delete it
  if (stateB == 1) {
    stored_state [last_x][last_y] = false
                                    ;
  }

  delay(10); //Mess with delay to tweak accuracy
}

Here's my amended code.Yes the first matrix is working perfectly.Just the others won't save a pixel or if I hit the same pixel on the other matrices they copy the saved pixel from the first matrix.

I'm not sure how relevant this is to the symptoms you have described, but this:

  pinMode(button_a, INPUT); // button is input
  digitalWrite(button_a, HIGH); // initalize button as ON
  pinMode(button_b, INPUT); // button is input
  digitalWrite(button_b, HIGH); // initalize button as ON

has the effect of switching the pull up resistors on. This makes sense only if your buttons are wired between an arduino pin and ground. However, it appears form the comments as if you expect a button push to yield a HIGH

int stateA = digitalRead(button_a); //determines the state of button_a (HIGH or LOW)
. . .
  // if button_a is pressed, store it
  if (stateA == 1) {
    stored_state [last_x][last_y] = true;
  }

Normally with buttons, the buttons are indeed wired between an arduino pin and ground and a pullup resistor (usually internal) is used to stop the pin floating. But then a button press is detected if the pin goes LOW. This is the opposite of what you have here.

I've rewired the switches and changed the code to..

pinMode(button_a, INPUT); // button is input
digitalWrite(button_a, LOW); // initalize button as ON
pinMode(button_b, INPUT); // button is input
digitalWrite(button_b, LOW); // initalize button as ON

Also tried the different variations of the state and true/false

Unfortunately it's still only saving or deleting pixels on the first matrix.Almost as if the data from digital out of the first matrix is only outputting direction data but not data to store.

If you have now wired the switches so that they are now connected between ground and the corresponding arduino pins, you should have this:

. . .
pinMode(button_a, INPUT_PULLUP); // If button pressed, digitalRead() == LOW otherwise it is HIGH.
pinMode(button_b, INPUT_PULLUP); // If button pressed, digitalRead() == LOW otherwise it is HIGH.
// no digitalWrite() statements are needed.
. . .
. . .

  // if button_a is pressed, store it
  if (stateA == LOW) {
    stored_state [last_x][last_y] = true ;
  }

  // if button_b is pressed, delete it
  if (stateB == LOW) {
    stored_state [last_x][last_y] = false ;
  }

. . .
. . .

I guess this code should be as follows because storedState[ ] [ ] uses indexes 0 to 15 so should correct the problem you have described where only the first matrix appears to be used for store/recall :

  if ( (last_x != x_translate) || (last_y != y_translate) )
  {
    // the position has moved so we need to restore previous state
    if ( last_x < 8  && last_y < 8 )  lc.setLed(0, last_x,     last_y,     stored_state [last_x][last_y] );
    if ( last_x >= 8 && last_y < 8 )  lc.setLed(1, last_x - 8, last_y,     stored_state [last_x][last_y] );
    if ( last_x < 8  && last_y >= 8 ) lc.setLed(2, last_x,     last_y - 8, stored_state [last_x][last_y] );
    if ( last_x >= 8 && last_y >= 8 ) lc.setLed(3, last_x - 8, last_y - 8, stored_state [last_x][last_y] );
  }

Fantastic.That works perfectly.I've learned quite a lot looking at how you've altered the original code so thanks again

OK. Good. I'm pleased that about that. Now it works, you can explain what it is. Is it a game or what ?

It's going to be the display of an audio switching matrix where the leds will indicate which switch is closed.I'm using the adg1414 octal switch.It seems pretty easy to control via spi.The hard part for me now is linking the status of a stored led to a closed switch on the adg1414

Something like this

Oh great. I'm glad it has a practical application and the display does look good.