How to reduce the memory use
in the program
This is the program
#include "LedControl.h"
#include <math.h>
#define NBR_MTX 2
LedControl lc=LedControl(12,11,10, NBR_MTX);
int n[11][8][8] = {{
{0,0,0,0,0,0,0,0},
{0,1,1,0,0,0,0,0},
{1,0,0,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,1,1,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,1,1,0,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,0,0,1,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,1,0,0,0,0,0,0},
{1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,1,1,0,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,0,0,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{1,0,0,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{1,1,1,1,0,0,0,0},
{0,0,0,1,0,0,0,0},
{0,0,0,1,0,0,0,0},
{0,0,0,1,0,0,0,0},
{0,0,0,0,0,0,0,0},
},
{
{0,0,0,0,0,0,0,0},
{1,1,1,1,0,0,0,0},
{1,0,0,0,0,0,0,0},
{0,1,1,0,0,0,0,0},
{0,0,0,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,1,1,0,0,0,0,0},
{1,0,0,0,0,0,0,0},
{1,1,1,0,0,0,0,0},
{1,0,0,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{1,1,1,1,0,0,0,0},
{0,0,0,1,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,1,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,1,1,0,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,1,1,0,0,0,0,0},
{1,0,0,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,1,1,0,0,0,0,0},
{1,0,0,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,1,1,1,0,0,0,0},
{0,0,0,1,0,0,0,0},
{0,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
}};
const int LRin = A0;
const int UDin = A1;
bool blinkCandy = true; //switch every loop to blink candy.
int moveDir = 0; //0 = right, 1 = up, 2 = left, 3 = down.
int currMoveDir = 0;//Used to keep track of actual current dir, not just last set during a cycle
int length = 2; //Starting length.
int last[64][2]; //Matrix with enough room for one snake to use all 64 LEDs
int x = 2; //Start pos
int y = 2; //start pos
int lastId = 0; //The position of the snake prevoius step.
bool collision = false; //Set to true if collision.
bool incLength = false;
int candyPos[2]; //Holds the position of the candy.
bool candyOnScreen = false;
int speed = 200;
void setup() {
Serial.begin (9600);
for(int i = 0; i < 64; i++){ //set all positions outside the screen.
last*[0] = -1;*
_ last*[1] = -1;_
_ }_
_ //Set up display*_
* for (int i=0; i< NBR_MTX; i++){
_ lc.shutdown(i,false);_
_ / Set the brightness to a medium values /_
_ lc.setIntensity(i,10);_
_ / and clear the display /_
_ lc.clearDisplay(i);_
_ }_
_}_
int looper = 0;
void loop() {
_ looper++;_
_ looper = looper % speed;_
_ CheckMove();_
_ if(looper==0) DoMove();_
_ if(looper==0) checkCollision();_
_ if(collision) {_
_ GameOver();_
_ }_
_ candyCollision(); //Check if candy has been eaten*_
* while(!candyOnScreen) createCandy(); //radnomize new candy position. Keep going until a valid position is set*
* if(looper % 100 == 0) candyBlink();*
* delay(1);*
}
void GameOver(void){
* while(1){*
* lc.clearDisplay(0);*
* delay(100);*
* PS(length-2);*
* delay(500);*
* }*
}
void PS(int a){
* int nr1;*
* int nr2;*
* if(a > 10){*
* nr1 = a / 10;*
_ nr2 = a - (nr1 * 10);
* }*
* else if(a==10){*
* nr1 = 1;*
* nr2 = 0;*
* }*
* else{*
* digit(a, 2);*
* return;*
* }*
* digit(nr1, 0);*
* digit(nr2, 4);*
}
void digit(int nr1, int offset){
* for(int x1 = 0; x1 < 8; x1++){*
* for(int y1 = 0; y1 < 8; y1++){*
* if(n[nr1][x1][y1] == 1) lc.setLed(0,y1+offset,7-x1,true);*
* }*
* }*
}
void candyBlink(void){
* if(blinkCandy) lc.setLed(0,candyPos[0],candyPos[1],true);*
* else lc.setLed(0,candyPos[0],candyPos[1],false);*
* blinkCandy = !blinkCandy;*
}
void Led(int x, int y){
* if(!incLength){*
* //Turn of the last light at the tail, re arrange array so the new tail is at index 0.*
* lc.setLed(0,last[0][0], last[0][1],false);*
* for(int i = 0; i < length; i++){*
last*[0] = last[i+1][0];
last[1] = last[i+1][1];
}
}
else{
length++;
incLength = false;
}
last[length][0] = x;
last[length][1] = y;
lc.setLed(0,last[length][0],last[length][1],true); // turn on led for the next position of the head.
}*
void CheckMove(void){_
* int LR = analogRead(LRin);*
* int UD = analogRead(UDin);*
* if(LR > 600 && currMoveDir != 0) moveDir = 2; //Go right, if snake is not moving left*
* if(LR < 100 && currMoveDir != 2) moveDir = 0; //Go left, if snake is not moving right*
* if(UD > 600 && currMoveDir != 1) moveDir = 3; //Go up, if snake is not moving down*
* if(UD < 100 && currMoveDir != 3) moveDir = 1; //Go down, if snake is not moving up*
}
void DoMove(void){
* currMoveDir = moveDir;*
* if(moveDir == 0) MoveRight();*
* if(moveDir == 1) MoveUp();*
* if(moveDir == 2) MoveLeft();*
* if(moveDir == 3) MoveDown();*
* Led(x,y);*
}
void IncrementLength(void){
* incLength = true;*
}
void MoveRight(void){
* x++;*
}
void MoveLeft(void){
* x--;*
}
void MoveUp(void){
* y--;*
}
void MoveDown(void){
* y++;*
}
void createCandy(void){
* int xR = rand() % 8;*
* int yR = rand() % 8;*
* for(int i = 0; i <= length; i++){*
if(xR == last_[0] && yR == last*[1]){
candyOnScreen = false;
return;
}
}
candyPos[0] = xR;
candyPos[1] = yR;
candyOnScreen = true;
lc.setLed(0,xR,yR,true);
}
void candyCollision(){
if(last[length][0] == candyPos[0] && last[length][1] == candyPos[1]){
candyOnScreen = false;
IncrementLength();
}
}
void checkCollision(){
int xP = last[length][0];
int yP = last[length][1];
if(xP == -1 || xP == 8 || yP == -1 || yP == 8){
collision = true;
return;
}
else{
for(int i = 0; i < length; i++){
if(last[0] == xP && last[1] == yP){
collision = true;
return;
}
}
}
}*_