Snake 8x16 with two Max7219

I want to make a Snake game with two Max7219 Matrices.

But it dosn't work:

the origin code is:

void Drawing(int Matrix[8][8], byte x, byte y) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if ((Matrix*[j] >= 1) || ((i == x) && (j == y) )) lc.setLed(0, i, j, true);*

  • else lc.setLed(0, i, j, false);*
  • }*
    }
    }
    I want to set the second Matrix:
    void Drawing(int Matrix[8][16], byte x, byte y)
    {
    for (int i = 0; i < 8; i++) {
  • for (int j = 0; j < 16; j++) {*

_ if ((Matrix*[j] >= 1) || ((i == x) && (j == y) )) && if (j <= 8 ) lc.setLed(0, i, j, true);_
_
else lc.setLed(0, i, j, false) if (j >= 8 ) || lc.setLed(1, i, j, true);_
_
else lc.setLed(1, i, j, false);*_

*} *
}
}
What means: (i == x) && (j == y) and Matrix*[j] >= 1)*

You need to read the how to use this forum sticky and post that code using code tags.

Then you need to post all your code, not just bits. This is because your errors are in the bits of code you don't post. Finally you need to show in a schematic how you have wired up the hardware, specifically the two MAX chips to the Arduino.

The code with one Matrix is valid. But my new code for two MAX7219 dosn t work :frowning:

The code is:

#include "LedControl.h" 
#include "Pitches.h" 
 
//Anschluss - 8x8 Matrix
const byte DIN = 12;
const byte CLK = 11;
const byte CS = 10;
LedControl lc = LedControl(DIN, CLK, CS, 2); // 2 = Anzahl Matrizen

 
//Anschluss - Steuerung
const byte btnRight = 7; //Rechts
const byte btnLeft = 6;  //Links
const byte btnUp = 5;    //Auf
const byte btnDown = 4;   //Ab
 
//Anschluss - Lautsprecher
const byte speaker = 9;
 
///////////////////////////////////////////////////////////////////

//Variablen

int Snake[8][16] = {0};
 
byte xSnake = 4;
byte ySnake = 5;
byte xFood = 0;
byte yFood = 0;

byte Direction = 0;
int Step = 1;
long TimeChange = 0;
int Speed = 500;
byte Score = 0;
 
boolean Eat = false;
boolean Sound = true;
boolean Collision = false;

 
///////////////////////////////////////////////////////////////////
//Definition Melodie
//noteDurations = Notendauer
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};
 
//Definition Zahlen
int Numbers[11][7] = {
  {B00000000,B00000000,B00000000,B00000000,B00000001,B01111111,B00100001}, //1
  {B00000000,B00000000,B00110001,B01001001,B01000101,B00100011},           //2
  {B00000000,B00000000,B01000110,B01101001,B01010001,B01000001,B01000010}, //3
  {B00000000,B00001000,B11111111,B01001000,B00101000,B00011000,B00001000}, //4
  {B00000000,B00000000,B00111110,B01000101,B01001001,B01010001,B00111110}, //5
  {B00000000,B00000000,B00000110,B01001001,B01001001,B00101001,B00011110}, //6
  {B00000000,B00000000,B01110000,B01001000,B01000111,B01000000,B01100000}, //7
  {B00000000,B00000000,B00110110,B01001001,B01001001,B01001001,B00110110}, //8
  {B00000000,B00000000,B00111100,B01001010,B01001001,B01001001,B00110000}, //9
  {B00000000,B00000000,B00111110,B01010001,B01001001,B01000101,B00111110}, //0
  {B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000}, //_
  };
 
void setup() {
  Serial.begin(9600);
  Serial.println("Snake Game Intialisierung");
 
  randomSeed(analogRead(A0));
  GenerateFood();
 
  pinMode(btnLeft, INPUT_PULLUP);
  pinMode(btnRight, INPUT_PULLUP);
  pinMode(btnUp, INPUT_PULLUP);
  pinMode(btnDown, INPUT_PULLUP);
 
  pinMode(speaker, OUTPUT);
 
  //Loading Matrix
  lc.shutdown(0, false);
  lc.setIntensity(0, 1);
  lc.clearDisplay(0);
   lc.shutdown(1, false);
  lc.setIntensity(1, 1);
  lc.clearDisplay(1);
}
 
void loop() {
  if ((digitalRead(btnLeft) == false) && (Direction != 0)) Direction = 1;
  if ((digitalRead(btnRight) == false) && (Direction != 1)) Direction = 0;
  if ((digitalRead(btnUp) == false) && (Direction != 3)) Direction = 2;
  if ((digitalRead(btnDown) == false) && (Direction != 2)) Direction = 3;
 
  if ((millis() - TimeChange) > Speed) {
    TimeChange = millis();
 
    ClearSnake(Snake);
    Scores();
    Move();
 
    if (xSnake == xFood && ySnake == yFood) Eat = true;
    if (Snake[xSnake][ySnake] != 0) GameOver();
 
    Step++;
    Snake[xSnake][ySnake] = Step;
  }
  Drawing(Snake, xFood, yFood);
}

///////////////////////////////////////////////////////////////////
//Funktionen

void Scores() {
  if (Eat == true) {
    Eat = false;
    GenerateFood();
 
    Score++;
    tone(speaker, NOTE_C5, 200);
    Speed -= 20;
 
    Serial.print ("Score is: ");
    Serial.println (Score);
  }
}



void Drawing(int Matrix[8][16], byte x, byte y) 

{for (int i = 0; i < 8; i++) 
  {for (int j = 0; j < 16; j++)
    {if ((Matrix[i][j] >= 1) || ((i == x) && (j == y) )) lc.setLed(m, i, j, true); else lc.setLed(m, i, j, false);

    if (j<8) byte m=0;
    if (j>8) byte m=1;
    }
   }   
}




 
void ClearSnake(int Matrix[8][16]) {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 16; j++) {
      if (Matrix[i][j] == (Step - Score)) Matrix[i][j] = 0;
    }
  }
}
 
void Move() {
  switch (Direction) {
    case 0:
      xSnake++;
      break;
    case 1:
      xSnake--;
      break;
    case 2:
      ySnake++;
      break;
    case 3:
      ySnake--;
      break;
  }
  if (xSnake == 8) xSnake = 0;
  if (ySnake == 16) ySnake = 0;
  if (xSnake == 255) xSnake = 7;
  if (ySnake == 255) ySnake = 15;
}


 
void GenerateFood() {
  lc.setLed(0, xFood, yFood, false);
  do {
    xFood = random(0, 8);
    yFood = random(0, 16);
  } while (Snake[xFood][yFood] != 0 );
}
 
void DrawingNumbers(byte Number){
  if(Number ==0) Number = 10;
    for (int col=0; col<7; col++) {
      lc.setRow(0, col, Numbers[Number-1][col]);    
  }
}
 
void GameOver() {
  Serial.println ("Game over");
  for (int row = 0; row < 8; row++) {
    for (int col = 0; col < 8; col++) {
      if (Sound == true) {
        int noteDuration = 1000 / noteDurations[col];
        tone(speaker, melody[col], noteDuration);
        int pauseBetweenNotes = noteDuration * 1.30;
        delay(pauseBetweenNotes);
        noTone(speaker);
      }
      lc.setLed(0,col,row, true);
      delay(25);
    }
    Sound = false;
  }
  for (int row = 0; row < 8; row++) {
    for (int col = 0; col < 8; col++) {
      lc.setLed(0, col, row, false); 
      delay(25);
    }
  }
  do {
       DrawingNumbers((Score/10)%10);
  delay(200);
  DrawingNumbers(Score%10);
  delay(200);
  DrawingNumbers(11);
  delay(1000);
  } while (true);
}

Das Problem liegt hier:

void Drawing(int Matrix[8][16], byte x, byte y) 

{for (int i = 0; i < 8; i++) 
  {for (int j = 0; j < 16; j++)
    {if ((Matrix[i][j] >= 1) || ((i == x) && (j == y) )) lc.setLed(m, i, j, true); else lc.setLed(m, i, j, false);

    if (j<8) byte m=0; // Set the first matrix
    if (j>8) byte m=1; // Set the second matrix
    }
   }   
}

m=0 for the first Matrix

m=1 for the second Matrix

Thanks for your cooperation so far but:-

Have you read How to use this forum
It tells you that the comment:-

But my new code for two MAX7219 dosn t work

Is a bit useless. All code works, it is just that it might be doing something that you did not intend it to do. So what is it doing and what did you expect?