Update values in predefined variable array

Hello everybody

after some time I am back to discuss a programming problem which appears because I do not have enough experience in C/C++. First of all here is my code:

// TEST seven segment display on TFT
// XIAO ESP32-C3 Supermini
// May 2026
// Bernardo Gullasch
// including Wifi, TFT-Display, Setbuttons

/* TFT_eSPI parameters ILI9341 240x320 2,4inch
 * #define ILI9341_DRIVER       // Generic driver for common displays
 * #define TFT_RGB_ORDER TFT_BGR  // Colour order Blue-Green-Red
 * #define TFT_HEIGHT 320 // ST7789 240 x 320
 * #define TFT_INVERSION_OFF
 *  
 * #define TFT_MISO D9
   #define TFT_MOSI D10
   #define TFT_SCLK D8
   #define TFT_CS   D7   // Chip select control pin
   #define TFT_DC   D3  // Data Command control pin
   #define TFT_RST  D2  // Reset pin (could connect to RST pin)
 */
 
#include <TFT_eSPI.h>
#include <SPI.h>
#include <WiFi.h>
#include <time.h>
#include <ESP32Time.h>
#include "FreeFonts.h"

#define TFT_BLACK     0x0000
#define TFT_WHITE     0xFFFF
#define TFT_LIGHTGREY 0xBDF7
#define TFT_DARKGREY  0x7BEF
#define TFT_RED       0xF800
#define TFT_YELLOW    0xFFE0
#define TFT_BLUE      0x1F

#define SetBtn 3
#define UpBtn  6 //4
#define DwnBtn 7 //5
#define TFT_LED 2

// Use hardware SPI
TFT_eSPI tft = TFT_eSPI();

bool Led = HIGH;
int colors = 0;

// seven segment relative coordinates
int x = 50;
int y = 40;
int xoff = 45; // offset for repositioning
int yoff = 0;  // offset for repositioning
int lx = 40;   // length of segment
int ly = 40;   // length of segment
int z = 4;     // space between segments and segment line thickness

// segments drawing parameters for the tft.fillRoundRect statement
int a[4] = {x+lx-z, y+z, 2*z, ly-2*z};
int b[4] = {x+lx-z, y+ly+z, 2*z, ly-2*z};
int c[4] = {x+z, y+2*ly-z, lx-2*z, 2*z};
int d[4] = {x-z, y+ly+z, 2*z, ly-2*z};
int e[4] = {x-z, y+z, 2*z, ly-2*z};
int f[4] = {x+z, y-z, lx-2*z, 2*z};
int g[4] = {x+z, y+ly-z, lx-2*z, 2*z};

int seg[7][4] = { 
                 {a[0],a[1],a[2],a[3]},
                 {b[0],b[1],b[2],b[3]},
                 {c[0],c[1],c[2],c[3]},
                 {d[0],d[1],d[2],d[3]},
                 {e[0],e[1],e[2],e[3]},
                 {f[0],f[1],f[2],f[3]},
                 {g[0],g[1],g[2],g[3]}
               };  

// --------- SETUP ---------
void setup() 
 {
   pinMode(TFT_LED, OUTPUT);
   pinMode(SetBtn, INPUT_PULLUP);
   pinMode(UpBtn, INPUT_PULLUP);
   pinMode(DwnBtn, INPUT_PULLUP);
 

   digitalWrite(TFT_LED, Led);
   tft.begin();
   tft.setRotation(1);
   tft.fillScreen(TFT_LIGHTGREY);
 }

 // --------------------- finish Setup ------------------------ 

void loop() 
{ 
  print7seg();
} 
// ------------------------------------- End Loop ------------------------------


void print7seg()
 {
   for(int shift=0; shift<4; shift++)
   {
    a[0]=a[0]+shift*xoff; a[1]=a[1]+shift*yoff;
    b[0]=b[0]+shift*xoff; b[1]=b[1]+shift*yoff;
    c[0]=c[0]+shift*xoff; c[1]=c[1]+shift*yoff;
    d[0]=d[0]+shift*xoff; d[1]=d[1]+shift*yoff;
    e[0]=e[0]+shift*xoff; e[1]=e[1]+shift*yoff;
    f[0]=f[0]+shift*xoff; f[1]=f[1]+shift*yoff;
    g[0]=g[0]+shift*xoff; g[1]=g[1]+shift*yoff;
    
    for(int i=0; i<7; i++)
     {
        tft.fillRoundRect(seg[i][0],seg[i][1],seg[i][2],seg[i][3],z,TFT_BLUE);
     }
   } 
 }
 

to make it short, this should print the number 8 in seven segment format on my TFT display at the position x,y. and repeat this 3 times with a position offset of (i*xoff), (i*yoff) which result from the "i" loop . The code compiles good but it draws only the first digit on the display. This looks like the values of the segment drawing parameters do not update.

what is wrong with my code ???

thanks in advance

seg[][] is created only once at startup and stores copies of the initial values from a to g.

Later, when you change a, b, c... inside print7seg(), seg[][] does not change with them, so fillRoundRect() still uses the original first-digit coordinates.

āžœ a C++ array isn't like excel. it's not because you do

that the content of seg is linked to the content of a b c...
you just got a one off copy.

EDIT: I see that @J-M-L answered while I was typing.

Updating the values in your a, b, c, d, e, f, g arrays does not update them in your seg array.

this looks logic and I understand but how to do it right now ?

update seg, not a, b, c, ...

...yes I got there and it works.

Here is the update of my code:

// TEST seven segment display on TFT
// XIAO ESP32-C3 Supermini
// May 2026
// Bernardo Gullasch
// including Wifi, TFT-Display, Setbuttons

/* TFT_eSPI parameters ILI9341 240x320 2,4inch
 * #define ILI9341_DRIVER       // Generic driver for common displays
 * #define TFT_RGB_ORDER TFT_BGR  // Colour order Blue-Green-Red
 * #define TFT_HEIGHT 320 // ST7789 240 x 320
 * #define TFT_INVERSION_OFF
 *  
 * #define TFT_MISO D9
   #define TFT_MOSI D10
   #define TFT_SCLK D8
   #define TFT_CS   D7   // Chip select control pin
   #define TFT_DC   D3  // Data Command control pin
   #define TFT_RST  D2  // Reset pin (could connect to RST pin)
 */
 
#include <TFT_eSPI.h>
#include <SPI.h>
#include <WiFi.h>
#include <time.h>
#include <ESP32Time.h>
#include "FreeFonts.h"

#define TFT_BLACK     0x0000
#define TFT_WHITE     0xFFFF
#define TFT_LIGHTGREY 0xBDF7
#define TFT_DARKGREY  0x7BEF
#define TFT_RED       0xF800
#define TFT_YELLOW    0xFFE0
#define TFT_BLUE      0x1F

#define SetBtn 3
#define UpBtn  6 //4
#define DwnBtn 7 //5
#define TFT_LED 2

// Use hardware SPI
TFT_eSPI tft = TFT_eSPI();

bool Led = HIGH;
int colors = 0;

// seven segment relative coordinates
int x = 10;
int y = 60;
int xoff = 30; // offset for repositioning
int yoff = 10;  // offset for repositioning
int lx = 20;   // length of segment
int ly = 20;   // length of segment
int z = 2;     // space between segments and segment line thickness
int cx; // calculated x-coordinate
int cy; // calculated y-coordinate
int vx; // x-length
int vy; // y-length

int a[4] = {x+lx-z, y+z, 2*z, ly-2*z}; //ly-2*z
int b[4] = {x+lx-z, y+ly+z, 2*z, ly-2*z};
int c[4] = {x+z, y+2*ly-z, lx-2*z, 2*z};
int d[4] = {x-z, y+ly+z, 2*z, ly-2*z};
int e[4] = {x-z, y+z, 2*z, ly-2*z};
int f[4] = {x+z, y-z, lx-2*z, 2*z};
int g[4] = {x+z, y+ly-z, lx-2*z, 2*z};

int seg[7][4] = { 
                 {a[0],a[1],a[2],a[3]},
                 {b[0],b[1],b[2],b[3]},
                 {c[0],c[1],c[2],c[3]},
                 {d[0],d[1],d[2],d[3]},
                 {e[0],e[1],e[2],e[3]},
                 {f[0],f[1],f[2],f[3]},
                 {g[0],g[1],g[2],g[3]}
               };  

// --------- SETUP ---------
void setup() 
 {
   pinMode(TFT_LED, OUTPUT);
   pinMode(SetBtn, INPUT_PULLUP);
   pinMode(UpBtn, INPUT_PULLUP);
   pinMode(DwnBtn, INPUT_PULLUP);
 

   digitalWrite(TFT_LED, Led);
   tft.begin();
   tft.setRotation(1);
   tft.fillScreen(TFT_LIGHTGREY);
 }

 // --------------------- finish Setup ------------------------ 

void loop() 
{ 
  print7seg();
} 
// ------------------------------------- End Loop ------------------------------


void print7seg()
 {
   for(int i=0; i<4; i++) // number of digits
    { 
    for(int s=0; s<7; s++)
     {
      cx= seg[s][0] + i*xoff; cy = seg[s][1] + i*yoff;
      vx= seg[s][2]; vy= seg[s][3];
      tft.fillRoundRect(cx,cy,vx,vy,z,TFT_BLUE);
     }
    } 
 }
 

thanks for your help :melting_face: