Work with a two-dimensional array

Hello,
i’m working on a little Project for the university.
It’s a LED-labyrinth connected to a arduino and a gaming controller.
I simply took a 8 meter LED-Strip. Cut it in eight pieces and make with them a matrix.

I use a one dimensional array and it works well. But what I’m trying to do is to make more than one Labyrinth. So I tried it with a two-dimensional array, but it doesn’t work and I don’t know why!

What I’m trying to fix:

  • When the mover (moveX) reach the goal (ziel). Not only get the lights off, also that the mover don’t get obstacles at all.
  • Than the second labyrinth should read.

Is there a way to reset the programm? So I can set a random labyrinth and it should work, or not?

This is my first version, which works:

// Joystick movement
// Joystick on A0=X / A1= Y

#include "LPD8806.h"
#include "SPI.h"
int line= 32; // LED in one line
int column=8; // LED in a column

int nLEDs = 256; //


int dataPin  = 2;
int clockPin = 3;
LPD8806 strip = LPD8806(nLEDs, dataPin, clockPin);

int moveX=4*line;  // Startpoint green omver
int ziel=5*line+25;
int testLed=0;

int test1;

boolean left,right,up,down;

int obstacle[]={ 
  0,1,1,1,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,1,0,1,0,0,0,1,0,0,1,0,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,0,1,0,
  1,1,0,1,0,1,1,1,0,1,0,1,1,0,0,0,0,1,0,1,0,0,1,1,1,1,1,0,1,0,1,0,
  0,1,0,0,0,1,0,1,0,0,0,0,1,0,1,1,0,1,0,1,0,0,1,0,0,0,1,0,1,0,1,0,
  0,0,0,1,0,1,0,1,1,1,1,0,1,0,1,0,0,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,
  1,1,0,1,1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,
  0,0,0,1,0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,
  0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
};

void setup() {
  // Start up the LED strip
  strip.begin();
  // Update the strip, to start they are all 'off'
  strip.show();

  for (int i=0 ; i < nLEDs; i++) {
    if (obstacle[i]==1)   {
      strip.setPixelColor(i,10,60,40); // obstacle color
    }
    strip.show();
  }




  Serial.begin(9600);// Debugging only

  strip.setPixelColor(ziel,127,0,10);

}


void loop() {
  Serial.print(analogRead(A0));
  Serial.print("    ");
  Serial.println(analogRead(A1));
  if (analogRead(A1) > 1000){
    down=true;
  } 
  else {
    down=false;
  }
  if ((analogRead(A1) <800)&& (analogRead(A1)>100)){
    up=true;
  } 
  else {
    up=false;
  }
  if ((analogRead(A0) <650)&& (analogRead(A0)>100)){
    right=true;
  } 
  else {
    right=false;
  }
  if (analogRead(A0) >900)    {
    left=true;
  } 
  else {
    left=false;
  }

  go();




}
//
// set movement according reading
int go(){  

  strip.setPixelColor(moveX,80,80,0); // mover on startposition
  strip.show();
  delay(10); //


  if ((left)&& (moveX%line !=0)) 
  { 
    int test=moveX -1;
    if (checkO(test)) {
      strip.setPixelColor(moveX,0); // clear green mover 
      moveX--;
    }
  }//walk right
  if ((right)&&(moveX%line !=(line-1))){
    int test=moveX+1;
    if (checkO(test)) {
      strip.setPixelColor(moveX,0); // clear green mover
      moveX++;
    }
  }

  if ((down)&& (moveX <(line*(column-1))))
  { 
    int test=moveX+line;
    if (checkO((test))) {
      strip.setPixelColor(moveX,0); // clear green mover
      moveX+=line; //walk down
    }
  }
  if ((up)&&(moveX > (line-1))){
    int test=moveX-line;
    if (checkO((test))) {
      strip.setPixelColor(moveX,0); // clear green mover
      moveX-=line;// walk up
    }


  }
}


// Check Obstacles

boolean checkO(int pos){

  boolean test=false;
  if (obstacle[pos] ==0){
    test=true;
  }
  else 
  {
    test=false;
  }
  return test;
}

And this is the second version, which doesn’t work:

// Joystick movement
// Joystick on A0=X / A1= Y

#include "LPD8806.h"
#include "SPI.h"
int line= 32; // LED in one line
int column=8; // LED in a column

int nLEDs = 256; //


int dataPin  = 2;
int clockPin = 3;
LPD8806 strip = LPD8806(nLEDs, dataPin, clockPin);

int moveX=4*line;  // Startpoint green omver
int ziel=4*line+2; //Erstes Ziel
int testLed=0;
int lab = 0;

boolean left,right,up,down;


int obstacle[][256]={
  { 
    0,1,1,1,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,1,0,1,0,0,0,1,0,0,1,0,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,0,1,0,
    1,1,0,1,0,1,1,1,0,1,0,1,1,0,0,0,0,1,0,1,0,0,1,1,1,1,1,0,1,0,1,0,
    0,1,0,0,0,1,0,1,0,0,0,0,1,0,1,1,0,1,0,1,0,0,1,0,0,0,1,0,1,0,1,0,
    0,0,0,1,0,1,0,1,1,1,1,0,1,0,1,0,0,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,
    1,1,0,1,1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,
    0,0,0,1,0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,
    0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
  }
  ,{
    0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,
    0,1,0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,0,1,0,1,0,
    0,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,1,1,1,1,1,1,1,0,1,1,0,1,0,0,0,0,1,0,1,0,1,1,1,1,0,1,1,
    0,1,1,1,0,0,0,0,0,0,0,1,0,0,1,0,1,0,1,1,0,1,0,1,1,0,1,0,0,0,1,0,
    0,0,0,0,1,1,1,1,1,1,0,1,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,
    1,1,1,0,1,0,0,0,1,1,0,0,0,0,1,0,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,0,
    0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,
  }
  ,{
    0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,1,0,1,1,1,1,1,0,0,0,0,0,0,0,
    0,0,0,1,0,1,0,1,0,1,1,1,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,1,1,1,0,1,
    0,1,0,1,0,0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,1,0,1,0,1,0,1,1,0,0,0,0,
    0,1,0,1,0,1,1,1,1,0,0,0,1,0,0,1,1,0,1,0,0,0,1,0,0,0,0,1,1,1,1,0,
    0,1,0,1,0,1,0,0,0,0,1,0,0,0,1,1,0,0,1,0,1,1,1,0,1,1,1,1,0,1,0,0,
    0,0,0,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,
    0,1,1,1,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1,1,1,1,0,0,1,1,1,0,1,1,0,0,
    0,0,1,0,0,0,0,1,1,0,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
  }
};


void setup() {
  // Start up the LED strip
  strip.begin();
  // Update the strip, to start they are all 'off'
  strip.show();

  for (int i=0 ; i < nLEDs; i++) {
    if (obstacle[0][i]==1)   {
      strip.setPixelColor(i,0,10,117); // obstacle color
    }
    strip.show();
  }

  Serial.begin(9600);// Debugging only

  strip.setPixelColor(ziel,127,0,0);

}


void loop() {
  Serial.print(analogRead(A0));
  Serial.print("    ");
  Serial.println(analogRead(A1));
  if (analogRead(A1) > 1000){
    down=true;
  } 
  else {
    down=false;
  }
  if ((analogRead(A1) <800)&& (analogRead(A1)>100)){
    up=true;
  } 
  else {
    up=false;
  }
  if ((analogRead(A0) <650)&& (analogRead(A0)>100)){
    right=true;
  } 
  else {
    right=false;
  }
  if (analogRead(A0) >900)    {
    left=true;
  } 
  else {
    left=false;
  }
  go();
/*  if(moveX==ziel) {
    //    strip.setPixelColor(testLed,100,100,0);
    lab=1;
    for (int i=0 ; i < nLEDs; i++) {
      if (obstacle[0][i]==1)   {
        strip.setPixelColor(i,0,0,0); // obstacle color
      }
      strip.show();
    }
    int moveX=4*line;
    go();
  }*/
}

//
// set movement according reading
int go(){  

  strip.setPixelColor(moveX,0,32,0); // green mover on startposition
  strip.show();
  delay(10); //




  if ((left)&& (moveX%line !=0)) 
  { 
    int test=moveX -1;
    if (checkO(test)) {
      strip.setPixelColor(moveX,0); // clear green mover 
      moveX--;
    }
  }//walk right
  if ((right)&&(moveX%line !=(line-1))){
    int test=moveX+1;
    if (checkO(test)) {
      strip.setPixelColor(moveX,0); // clear green mover
      moveX++;
    }
  }

  if ((down)&& (moveX <(line*(column-1))))
  { 
    int test=moveX+line;
    if (checkO((test))) {
      strip.setPixelColor(moveX,0); // clear green mover
      moveX+=line; //walk down
    }
  }
  if ((up)&&(moveX > (line-1))){
    int test=moveX-line;
    if (checkO((test))) {
      strip.setPixelColor(moveX,0); // clear green mover
      moveX-=line;// walk up
    }

  }

}

// Check Obstacles

boolean checkO(int pos){

  boolean test=false;
  if (obstacle[0][pos] ==0){
    test=true;
  }
  else 
  {
    test=false;
  }
  return test;
}

boolean checkZiel(int pos){
  boolean test1=false;
  if (obstacle[0][pos] == ziel){
    test1 = true;
  } 
  else {
    test1 = false;
    return test1;
  }
}

Maybe someone can give me a little help. :slight_smile:

Regards
Patrick

Which Arduino board are you using ? I am wondering about lack of memory with all those ints. Try changing the array to the byte data type to save half of the memory used for the array. To save even more memory consider using an array of bytes with the positions indicated by the bits within the byte.

What happens when you run the program as it is now ?

Just about to say something very similar.

256 * 3 at 2 bytes per int is 1536 bytes of RAM

Hello Patrick and welcome :slight_smile:

The values in these arrays are only 0s or 1s, so you could use only a few bytes to store all those 0s and 1s

Actually you use 2 bytes per element, when you could use a single BIT per element (16 times less memory). Then with some bit shifting etc, you extract from a byte, the bit that you need.

I will show you an example if you can't do it yourself.

Then, another improvement will be to store this array in PROGMEM.

Yep - down to 192 bytes!

lloyddean:
Yep - down to 192 bytes!

Did you mean 96 bytes ? :slight_smile:

I'm bored so I made an example anyway

http://codepad.org/bQkrAFyG

Note to Patrick: in the Arduino IDE you can write in binary representation like in the commented lines "0bXXX..." :slight_smile: