#include <Adafruit_GFX.h>    // Core graphics library
#include "LGDP4535.h" // Hardware-specific library
#include <zTouchScreen.h>
#define BOXSIZE   40
// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

LGDP4535 tft; // Using the shield, all control and data lines are fixed

#define YP A3  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin

// You need to calibrate to fit your LCD
#define TS_MINX 930
#define TS_MINY 903
#define TS_MAXX 142
#define TS_MAXY 117

#define PENRADIUS 3
#define MINPRESSURE 10
#define MAXPRESSURE 1000

int matrix[2][3]; // Initial Declaration
int array[3]

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

// array = start with 1 but the variable read start with 0 
String ButtonAction[9]={"a", "b", "c", "d", "e", "f", "g", "h", "i"};
boolean ActiveMenu=true;

void setup(void) {
   DrawIt(CYAN, "draw");
   DrawIt(BLACK, "text");

void loop() {//SOME OTHER STUFF}

unsigned long DrawIt(uint16_t color, String CurrentAction) {

  int timedcycle=0;

  if (CurrentAction == "text") {
     int matrix[2][3]={{25,150,265},{45,100,155}};
     int array[3]={3,3,3};


     Serial.println("Text Columns");

  else if (CurrentAction == "draw") {
    int matrix[2][3]={{260,140,20},{40,150,95}};
    int array[3]={3,3,3}; 


  for(int j=0; j<3; j++) {
     for(int i=0; i<array[j]; i++) {

       if (CurrentAction == "draw") {

         if (timedcycle == 7) {
           tft.fillCircle(100, 160, 15, WHITE);
         else {       
           tft.fillRect(matrix[1][j], matrix[0][i], 20,40, color);

       else if (CurrentAction == "text") {
         if (timedcycle == 4) {
           //tft.fillCircle(100, 160, 15, WHITE);
         else {       
           tft.setCursor(matrix[0][j], matrix[1][i]);         


So as you can see I'm trying to draw some things in a TFT Touch Screen. Everything seems to be working but no matter what I do I can't change the array value.

If I initialize the array variable with a predetermined value it won't change even if the statement is called correctly. I can see that it runs over serial but the array still doesn't change.

I suppose there's something wrong but I can't pinpoint what it is! Thanks in advance!

  • 1
    You have defined three matrix/array; one in the global context and one per block in the local context in DrawIt. The definitions in the local context are removed by the compiler as they are not used (drop out of context). What you need to do is assign the global matrix/array instead. Jan 10, 2016 at 13:40
  • So my mistake is involved with variable declaration as my values are discarded by natural behaviour of the compiler. I didn't think of that but it's the same on other languages as well but I didn't took the "loop" and "setup" functions into account. Thanks for the quick explanation
    – DarkXDroid
    Jan 12, 2016 at 5:24

1 Answer 1


It's because you are defining a new array every time, not modifying the existing one.

int matrix[2][3]={{25,150,265},{45,100,155}};

That line doesn't assign new values to the existing matrix array, instead it creates a new array called matrix which only exists within the current scope - that is, within the {...} defined by the if that it is within.

Instead you either need to assign values directly to the existing matrix array on a slice-by-slice basis (you cannot use {...} to assign them in bulk):

matrix[0][0] = 25;
matrix[0][1] = 150;
... etc ...

Alternatively, since the values are static, you can pre-define the different matrices in the global scope and then switch between them. This can be done with the right kind of pointer. For instance:

int textMatrix[2][3] = {{260,140,20},{40,150,95}};
int (*matrix)[3];

void whateverTheFunctionIs() {
    if (whatever...) {
        matrix = textMatrix;

In that example matrix is a pointer to an array of pointers, which is basically what a 2D array is internally. It starts off by not being assigned to anything at all - it's just "dangling in the breeze". When the if in your function is triggered the address that the matrix pointer points to is changed to that of the textMatrix 2D array, so any access to matrix[x][y] will be identical to accesses to textMatrix[x][y].

  • Can you provide an example on how can I accomplish that?
    – DarkXDroid
    Jan 9, 2016 at 10:37
  • 1
    Instead if using int matrix... you just assign new values to the individual slices. Note that you cannot use the {...} notation for that but must assign each value individually. matrix[2][1] = 43
    – Majenko
    Jan 9, 2016 at 10:40
  • So the best option is to create another array with fixed values? Thanks for your answer and I'll check on this to accept your answer
    – DarkXDroid
    Jan 9, 2016 at 10:50
  • I am at my desk now, so I can give a much fuller answer... check back in a mo for my edit.
    – Majenko
    Jan 9, 2016 at 12:26
  • I got an error with the global variable part: 182:13: error: cannot convert ‘int [2][3]’ to ‘int**’ in assignment. I'll try the first solution then
    – DarkXDroid
    Jan 10, 2016 at 4:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.