Twodimensional Arrays -

Hello there

I am failing at a very simple thing it seems, and that is to use a 2-Dimensional array to display pixels on e display.

// Display is a 84x48 pixel monochrome LCD display

//Include Libs
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

//Variables

const byte rows = 47;
const byte cols = 83;

int grid [rows][cols];

void setup() {

  Serial.begin(9600);

  display.begin();
  
  // Contrast Setting
  display.setContrast(50);

  // show splashscreen
  display.display(); 
  delay(1);
  // clears the screen and buffer
  display.clearDisplay();     
}



void loop() {


  for(int i = 0; i < cols; i++){

    for(int j = 0; j < rows; j++){

      //draw a single pixel
    // display.drawPixel(cols,rows, BLACK);
     display.drawPixel(5,5, BLACK);
     display.display();
     delay(100);
     //display.clearDisplay(); 
    }
    
  }

}

The part where I actually draw the pixel → display.drawPixel(5,5, BLACK); works.

But I need something like display.drawPixel(grid[i],grid[j] BLACK);

I want to extract these values from the array.

Does anyone know the synthax for this so this Adafruit Library will finally compile and stop moaning? :slight_smile:

Thank you in advance.

So you want to draw what’s in the array?

  for(int i = 0; i < cols; i++){

    for(int j = 0; j < rows; j++){

      //draw a single pixel
    // display.drawPixel(cols,rows, BLACK);
     if(grid[j][i]){
       display.drawPixel(j, i, BLACK);
     }
     display.display();
     delay(100);
     //display.clearDisplay();
    }
   
  }

But it’s a very bad way. For most of those libraries the library already has a buffer for the image. If you want to draw an image (that’s in grid) you should look at drawBitmap(). If you really want to draw a grid on the screen then just draw lines in the right places. Don’t wast a whole array to hold only grid lines…

I want to make Conway's game of life. (https://www.youtube.com/watch?v=0XI6s-TGzSs)

So I need a way to store the values for all the cells (on or off). And it seemed the easy way to do this is a two dimensional array that has the same size as my display.

Then my previous code does exactly that.

But using a int (that's two bytes!) to only indicate a cell is dead or alive is a bit wast full don't you think?

And isn't using every single pixel on a Nokia 5110 a bit small?

What does this condition do?

if(grid[j][i]){
       display.drawPixel(j, i, BLACK);
}

You paint at the coordinates giben by i and j. I want to navigate the array using i and j, and print out what is stored there (0 or 1 basically)

Your code prints by the position i and j as it seems to me.

And what would be a better variable type to save space? Boolean I guess if it's for true or false. But I doubt that Adafruit Library will accept bools.

But I doubt that Adafruit Library will accept bools.

I can't see why that's an objection. You're not currently passing the int to the library, are you?

I basically want osmething like this:

for(int i = 0; i < cols; i++){

    for(int j = 0; j < rows; j++){
     
     
     grid[j][i] =1; //Just to fill it with values, for this example.

if(grid[j][i]){
      
       display.drawPixel(j, i, BLACK);  //Now THIS is wrong. Since grid[j][i] INTERATES, i want to be able to use it to display that pixel. I need the synthax for this.

Something like:

isplay.drawPixel(grid[j][i] BLACK); //I want the Library to print the VALUE stored in the array at position i and j.

}
     display.display();
     delay(100);
     //display.clearDisplay(); 
    }
    
  }

I am a bit confused I must admit.

I think I ned something like:

for(int i = 0; i < cols; i++){

    for(int j = 0; j < rows; j++){
     
     
     grid[j][i] =1; //Just to fill it with values, for this example.

if(grid[j][i] = 1){
     
       display.drawPixel(j, i, BLACK);  

}

if(grid[j][i] = 0){
     
       display.drawPixel(j, i, WHITE);  

}


     display.display();
     delay(100);
     //display.clearDisplay();
    }
   
  }

johnnygueggu: Your code prints by the position i and j as it seems to me.

But what is printing the position i and j? In the form of pixels it does not exist. You draw a pixel or you don't. drawPixel() only draws a single pixel on that location. So you loop through the array like you do, if the entry is set and you want to draw a pixel you call drawPixel() for that location. If that entry isn't set you simply move on and don't draw a pixel..

You could clear a pixel if it's not set by drawing a white pixel. But simply clearing the whole screen buffer after you looped through every pixel makes sure every pixel is already cleared.

So indeed, you don't pass the array to the library so what's wrong with a bool?

[edit] In the mean time you found it as well. But clearing the whole screen before you start makes you can skip the clearing of each pixel :D

Why skip 1 row and 1 col when declaring your array? Panel is 84 by 48. (declared as bytes, u'll need 4k of memory.. a mega?)

Read status from the display buffer already there..