Help me reduce the memory usage of my sketch

I am having the same problem as discussed at Reducing code to half its size - #22, but I have an esp32, and the size of the code (I have 2 files, a header file, and the main file) one file has 1500 lines and the other has 1200 lines having 2700 lines, is there a way to reduce the size of the main file? I cant do anything to the .h but I need the main file to reduce a bit. Is this possible?

My code is based on pure conditionals, of the 1500 lines of code 1300 lines are conditionals, so, I have the conditionals written this way:

if (condition) {
     code
}

if I make them this way:

if (condition) { code }

will it reduce space in the chip or will it just reduce lines and not space?

im afraid not, just reduces lines
please start a new issue for unrelated questions next time

Please do not hijack threads. Moderator alerted.

Please don't hijack threads . I have split your post out to its own topic.

Hijacking is against the Arduino forum rules. The reason is that it may sidetrack the discussion, even preventing the creator of the topic from getting the assistance they need.

This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Post some kind of example, if you can't post the whole sketch, that will give us an idea of what pure if based coding looks like.

Imma guess you could do elsewise. No pun intended.

Also knowing what you are up to and the general idea of your goal woukd help.

a7

Here is a part of the code:

if (numc == 8) {
        if ( tm == 1 ) {
          if (d8 > 0) {
            d8--;
          }
        }
        if ( tm == 2 ) {
          if (d12 > 0) {
            h8--;
          }
        }
        if ( tm == 3 ) {
          if (d12 > 0) {
            m8--;
          }
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d8);
        tft.setCursor(147, 29);
        tft.print(h8);
        tft.setCursor(173, 29);
        tft.print(m8);
        tft.setCursor(202, 29);
        tft.print(s8);
      }

This is duplicated 12 times the only difference are the variables that are from 1 to 12 for example m1, m2, h5, s12

It is a bit long the explanation... just like my code.

The components are an ILI9341 and an esp32, The project consists of 12 different containers filled up with liquids, and each container has an adjustable timer, for example: In container 1 I want to put a timer of 2 days, 4 hours 36 minutes, and 30 seconds, for container 2 the timer will be 4 hours, 23 minutes, and 0 seconds, when the user finished to set up all the 12 timers, he will press a button to start the countdown, and when all containers reached 0, it just stops and sends an alarm.

That is the explanation of my project, hope you understood all that stuff.

Aha, uhuh.

Any time you see yourself doing that, introducing variables that differ in name only by an appended number, you should remind yourself to learn about array variables.

And just in case it is relevant in your circumstances, any time you are cutting, pasting and editing code, making you code longer and longer, you should think about using functions.

Arrays and functions. Move 'em up on your "gonna get around to figuring them out" list of things to learn up on.

hope you understood all that stuff.

Yes, thanks. If you dare post the entire sketch there may be more to say.

a7

It is very long... But if you need it I will post it, But I think I will send the file, it is way better

wrong, on this forum. Common, accepted practice is to post the entire code; it's better if you can consolidate it down to a smaller, working demonstration of your problem, but if not, whole code posted in code tags is the way to go. Why? because it's then dead simple for anyone interested in helping to grab the code for insertion into a new .ino file in the IDE, rather than opening a file, cutting and pasting, etc. which leads to errors and diversions of the thread while the errors get sorted out.

What is the memory use reported after compiling the sketch?

Has the OP heard about arrays?

Has the OP heard about Switch?

Has the OP heard about Finite State Machine?

2 Likes

Based on the tiny snippet posted, apparently not. Similar to my comment elsewhere about processing arrays of structs. That's why I posted the request for the 'whole enchilada', as I expect it will point at a series of reductionist activities. And possibly, a whole lot of blank line removals. The added blank lines in the snippet hint to me of a desperate struggle to make sense of a bloated, repetitive code structure. BTDTGTTS.
But only the OP can confirm or deny this, by providing the code.

Ok I will post it here it is:

#include <FS.h>
#include <SPI.h>
#include <TFT_eSPI.h> // Hardware-specific library
TFT_eSPI tft = TFT_eSPI();
#include "teclado.h"
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_STMPE610.h>
//-------------------DISPLAY--------------------------
#define CALIBRATION_FILE "/TouchCalData3"
#define REPEAT_CAL false
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
#define FRAME_X 52
#define FRAME_Y 94
#define FRAME_W 135
#define FRAME_H 235

#define REDBUTTON_X FRAME_X
#define REDBUTTON_Y FRAME_Y
#define REDBUTTON_W FRAME_W
#define REDBUTTON_H FRAME_H

void touchInterfaz();
void imagenInterfaz();
void imagenBoton();
//-------------------RELE------------------------------
#define pinRele_1 32
#define pinRele_2 35
#define pinRele_3 34

int  retraso;
bool estado = false;
int  numc = 1;

int tm  = 0;

bool e1  = false;
bool e2  = false;
bool e3  = false;
bool e4  = false;
bool e5  = false;
bool e6  = false;
bool e7  = false;
bool e8  = false;
bool e9  = false;
bool e10 = false;
bool e11 = false;
bool e12 = false;


String dp = ":";
int sp = dp.toInt();
//****************************************************************************************
void setup() {
  Serial.begin(9600);
  //----------------------------------------------------
  pinMode(ledWifi, OUTPUT);
  digitalWrite(ledWifi, HIGH);
  //---------------------
  pinMode(pinRele_1, OUTPUT);
  pinMode(pinRele_2, OUTPUT);
  pinMode(pinRele_3, OUTPUT);
  digitalWrite(pinRele_1, LOW);
  digitalWrite(pinRele_2, LOW);
  digitalWrite(pinRele_3, LOW);
  //----------------------------------------------------
  tft.begin();

  tft.setRotation(0);
  tft.setSwapBytes(true);
  uint16_t calData[5] = { 303, 3452, 412, 3382, 4 };
  tft.setTouch(calData);
  tft.fillScreen(TFT_WHITE);
  imagenInterfaz();
  tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);

}
//******************************************************************************************

void touchInterfaz(
  int &min1, int &min2, int &min3, int &min4 , int &min5 , int &min6 ,
  int &min7, int &min8, int &min9, int &min10, int &min11, int &min12,
  int &hr1 , int &hr2 , int &hr3 , int &hr4  , int &hr5  , int &hr6  ,
  int &hr7 , int &hr8 , int &hr9 , int &hr10 , int &hr11 , int &hr12 ,
  int &dia1, int &dia2, int &dia3, int &dia4 , int &dia5 , int &dia6 ,
  int &dia7, int &dia8, int &dia9, int &dia10, int &dia11, int &dia12,
  int &sec1, int &sec2, int &sec3, int &sec4 , int &sec5 , int &sec6 ,
  int &sec7, int &sec8, int &sec9, int &sec10, int &sec11, int &sec12
);

void loop() {
  int m1 ;
  int m2 ;
  int m3 ;
  int m4 ;
  int m5 ;
  int m6 ;
  int m7 ;
  int m8 ;
  int m9 ;
  int m10;
  int m11;
  int m12;
           
  int h1;
  int h2;
  int h3;
  int h4;
  int h5;
  int h6;
  int h7;
  int h8;
  int h9;
  int h10;
  int h11;
  int h12;

  int d1;
  int d2;
  int d3;
  int d4;
  int d5;
  int d6;
  int d7;
  int d8;
  int d9;
  int d10;
  int d11;
  int d12;

  int s1  = 59;
  int s2  = 59;
  int s3  = 59;
  int s4  = 59;
  int s5  = 59;
  int s6  = 59;
  int s7  = 59;
  int s8  = 59;
  int s9  = 59;
  int s10 = 59;
  int s11 = 59;
  int s12 = 59;
  touchInterfaz(m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12,
                h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12,
                d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12,
                s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12);
}


//------------------------------------------------------------------------------------------
//FUNCION DECLARACION DE TECLAS DE DISPLAY
void imagenBoton() {
  tft.fillRoundRect(puntoX, puntoY, 112, 42, 2, TFT_WHITE);
}
void imagenBoton3() {
  tft.fillRoundRect(puntoX, puntoY, 87, 42, 2, TFT_WHITE);
}

void imagenBoton1() {
  tft.fillRoundRect(puntoX, puntoY, 52, 42, 2, TFT_WHITE);
}

void touchInterfaz(int &s1, int &s2, int &s3, int &s4, int &s5, int &s6, int &s7, int &s8, int &s9, int &s10, int &s11, int &s12, int &m1, int &m2, int &m3, int &m4, int &m5, int &m6, int &m7, int &m8, int &m9, int &m10, int &m11, int &m12, int &h1, int &h2, int &h3, int &h4, int &h5, int &h6, int &h7, int &h8, int &h9, int &h10, int &h11, int &h12, int &d1, int &d2, int &d3, int &d4, int &d5, int &d6, int &d7, int &d8, int &d9, int &d10, int &d11, int &d12) {
  //FUNCION PARA BOTON EN SEGUNDO int

  if (tft.getTouch(&x, &y))
  { //20, 25 135, 25
    //----------------------------------- RESET
    if (x > 7 && x < 52  && y > 44 && y < 88) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      d1  = 0;
      d2  = 0;
      d3  = 0;
      d4  = 0;
      d5  = 0;
      d6  = 0;
      d7  = 0;
      d8  = 0;
      d9  = 0;
      d10 = 0;
      d11 = 0;
      d12 = 0;

      h1  = 0;
      h2  = 0;
      h3  = 0;
      h4  = 0;
      h5  = 0;
      h6  = 0;
      h7  = 0;
      h8  = 0;
      h9  = 0;
      h10 = 0;
      h11 = 0;
      h12 = 0;

      m1  = 0;
      m2  = 0;
      m3  = 0;
      m4  = 0;
      m5  = 0;
      m6  = 0;
      m7  = 0;
      m8  = 0;
      m9  = 0;
      m10 = 0;
      m11 = 0;
      m12 = 0;

      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
      tft.setTextSize(2);
      tft.setCursor(10, 5);
      tft.print("Can.");
      tft.setCursor(55, 5);
      tft.print(numc);
      tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
      tft.setTextSize(1);
      tft.setCursor(128, 20);
      tft.print("D");
      tft.setCursor(150, 20);
      tft.print("H");
      tft.setCursor(176, 20);
      tft.print("M");
      tft.setCursor(205, 20);
      tft.print("S");
      tft.setTextSize(2);

      //-----------------
      switch (numc) {
        case 1 :
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d1);
          tft.setCursor(147, 29);
          tft.print(h1);
          tft.setCursor(173, 29);
          tft.print(m1);
          tft.setCursor(202, 29);
          tft.print(s1);
          break;

        case 2:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d2);
          tft.setCursor(147, 29);
          tft.print(h2);
          tft.setCursor(173, 29);
          tft.print(m2);
          tft.setCursor(202, 29);
          tft.print(s2);
          break;

        case 3:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d3);
          tft.setCursor(147, 29);
          tft.print(h3);
          tft.setCursor(173, 29);
          tft.print(m3);
          tft.setCursor(202, 29);
          tft.print(s3);
          break;

        case 4:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d4);
          tft.setCursor(147, 29);
          tft.print(h4);
          tft.setCursor(173, 29);
          tft.print(m4);
          tft.setCursor(202, 29);
          tft.print(s4);
          break;

        case 5:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d5);
          tft.setCursor(147, 29);
          tft.print(h5);
          tft.setCursor(173, 29);
          tft.print(m5);
          tft.setCursor(202, 29);
          tft.print(s5);
          break;

        case 6:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d6);
          tft.setCursor(147, 29);
          tft.print(h6);
          tft.setCursor(173, 29);
          tft.print(m6);
          tft.setCursor(202, 29);
          tft.print(s6);
          break;

        case 7:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d7);
          tft.setCursor(147, 29);
          tft.print(h7);
          tft.setCursor(173, 29);
          tft.print(m7);
          tft.setCursor(202, 29);
          tft.print(s7);
          break;

        case 8:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d8);
          tft.setCursor(147, 29);
          tft.print(h8);
          tft.setCursor(173, 29);
          tft.print(m8);
          tft.setCursor(202, 29);
          tft.print(s8);
          break;

        case 9:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d9);
          tft.setCursor(147, 29);
          tft.print(h9);
          tft.setCursor(173, 29);
          tft.print(m9);
          tft.setCursor(202, 29);
          tft.print(s9);
          break;

        case 10:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d10);
          tft.setCursor(147, 29);
          tft.print(h10);
          tft.setCursor(173, 29);
          tft.print(m10);
          tft.setCursor(202, 29);
          tft.print(s10);
          break;

        case 11:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d11);
          tft.setCursor(147, 29);
          tft.print(h11);
          tft.setCursor(173, 29);
          tft.print(m11);
          tft.setCursor(202, 29);
          tft.print(s11);
          break;

        case 12:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d12);
          tft.setCursor(147, 29);
          tft.print(h12);
          tft.setCursor(173, 29);
          tft.print(m12);
          tft.setCursor(202, 29);
          tft.print(s12);
          break;
      }
    }

    if (x > 14 && x < 40 && y > 1 && y < 30 ) {
      switch (numc) {
        case 1:
          e1 = true;
          break;

        case 2:
          e2 = true;
          break;

        case 3:
          e3 = true;
          break;

        case 4:
          e4 = true;
          break;

        case 5:
          e5 = true;
          break;

        case 6:
          e6 = true;
          break;

        case 7:
          e7 = true;
          break;

        case 8:
          e8 = true;
          break;

        case 9:
          e9 = true;
          break;

        case 10:
          e10 = true;
          break;

        case 11:
          e11 = true;
          break;

        case 12:
          e12 = true;
          break;
      }
    }

    if (x > 44 && x < 70 && y > 1 && y < 30 ) {
      switch (numc) {
        case 1:
          e1 = false;
          break;

        case 2:
          e2 = false;
          break;

        case 3:
          e3 = false;
          break;

        case 4:
          e4 = false;
          break;

        case 5:
          e5 = false;
          break;

        case 6:
          e6 = false;
          break;

        case 7:
          e7 = false;
          break;

        case 8:
          e8 = false;
          break;

        case 9:
          e9 = false;
          break;

        case 10:
          e10 = false;
          break;

        case 11:
          e11 = false;
          break;

        case 12:
          e12 = false;
          break;
      }
    }
    if (x > 120 && x < 141 && y > 12 && y < 27 ) {
      tm = 1;
    }

    if (x > 146 && x < 165 && y > 12 && y < 27 ) {
      tm = 2;
    }

    if (x > 170 && x < 189 && y > 12 && y < 27 ) {
      tm = 3;
    }
    //----------------------------------- STEP //57
    if (x > 64 && x < 121 && y > 44 && y < 88 ) {
      //...---...---.--.--.-.-.-.-
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
    }
    //-----------------

    //----------------------------------PROG
    if (x > 7 && x < 52 && y > 94 && y < 141 ) {
      //._._._._._._._._._._._._._._._._._._._._.
    }

    //----------------------------------- -
    if (x > 124 && x < 169 && y > 46 && y < 88 ) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
      tft.setTextSize(2);
      tft.setCursor(10, 5);
      tft.print("Can.");
      tft.setCursor(55, 5);
      tft.print(numc);
      tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
      tft.setTextSize(1);
      tft.setCursor(128, 20);
      tft.print("D");
      tft.setCursor(150, 20);
      tft.print("H");
      tft.setCursor(176, 20);
      tft.print("M");
      tft.setCursor(205, 20);
      tft.print("S");
      tft.setTextSize(2);

      if (numc == 1) {
        if ( tm == 1 ) {
          if (d1 > 0) {
            d1--;
          }
        }
        if ( tm == 2 ) {
          if (h1 > 0) {
            h1--;
          }
        }
        if ( tm == 3 ) {
          if (m1 > 0) {
            m1--;
          }
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d1);
        tft.setCursor(147, 29);
        tft.print(h1);
        tft.setCursor(173, 29);
        tft.print(m1);
        tft.setCursor(202, 29);
        tft.print(s1);
      }
      if (numc == 2) {
        if ( tm == 1 ) {
          if (d2 > 0) {
            d2--;
          }
        }
        if ( tm == 2 ) {
          if (h2 > 0) {
            h2--;
          }
        }
        if ( tm == 3 ) {
          if (m2 > 0) {
            m2--;
          }
        }

        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d2);
        tft.setCursor(147, 29);
        tft.print(h2);
        tft.setCursor(173, 29);
        tft.print(m2);
        tft.setCursor(202, 29);
        tft.print(s2);
      }

      if (numc == 3) {
        if ( tm == 1 ) {
          if (d3 > 0) {
            d3--;
          }
        }
        if ( tm == 2 ) {
          if (h3 > 0) {
            h3--;
          }
        }
        if ( tm == 3 ) {
          if (m3 > 0) {
            m3--;
          }
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d3);
        tft.setCursor(147, 29);
        tft.print(h3);
        tft.setCursor(173, 29);
        tft.print(m3);
        tft.setCursor(202, 29);
        tft.print(s3);
      }

      if (numc == 4) {
        if ( tm == 1 ) {
          if (d4 > 0) {
            d4--;
          }
        }
        if ( tm == 2 ) {
          if (h4 > 0) {
            h4--;
          }
        }
        if ( tm == 3 ) {
          if (m4 > 0) {
            m4--;
          }
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d4);
        tft.setCursor(147, 29);
        tft.print(h4);
        tft.setCursor(173, 29);
        tft.print(m4);
        tft.setCursor(202, 29);
        tft.print(s4);
      }

      if (numc == 5) {
        if ( tm == 1 ) {
          if (d5 > 0) {
            d5--;
          }
        }
        if ( tm == 2 ) {
          if (h5 > 0) {
            h5--;
          }
        }
        if ( tm == 3 ) {
          if (m5 > 0) {
            m5--;
          }
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d5);
        tft.setCursor(147, 29);
        tft.print(h5);
        tft.setCursor(173, 29);
        tft.print(m5);
        tft.setCursor(202, 29);
        tft.print(s5);
      }
      if (numc == 6) {
        if ( tm == 1 ) {
          if (d6 > 0) {
            d6--;
          }
        }
        if ( tm == 2 ) {
          if (h6 > 0) {
            h6--;
          }
        }
        if ( tm == 3 ) {
          if (m6 > 0) {
            m6--;
          }
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d6);
        tft.setCursor(147, 29);
        tft.print(h6);
        tft.setCursor(173, 29);
        tft.print(m6);
        tft.setCursor(202, 29);
        tft.print(s6);
      }
      if (numc == 7) {
        if ( tm == 1 ) {
          if (d7 > 0) {
            d7--;
          }
        }
        if ( tm == 2 ) {
          if (h7 > 0) {
            h7--;
          }
        }
        if ( tm == 3 ) {
          if (m7 > 0) {
            m7--;
          }
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d7);
        tft.setCursor(147, 29);
        tft.print(h7);
        tft.setCursor(173, 29);
        tft.print(m7);
        tft.setCursor(202, 29);
        tft.print(s7);
      }

      if (numc == 8) {
        if ( tm == 1 ) {
          if (d8 > 0) {
            d8--;
          }
        }
        if ( tm == 2 ) {
          if (d12 > 0) {
            h8--;
          }
        }
        if ( tm == 3 ) {
          if (d12 > 0) {
            m8--;
          }
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d8);
        tft.setCursor(147, 29);
        tft.print(h8);
        tft.setCursor(173, 29);
        tft.print(m8);
        tft.setCursor(202, 29);
        tft.print(s8);
      }
      if (numc == 9) {
        if ( tm == 1 ) {
          if (d9 > 0) {
            d9--;
          }
        }
        if ( tm == 2 ) {
          if (h9 > 0) {
            h9--;
          }
        }
        if ( tm == 3 ) {
          if (m9 > 0) {
            m9--;
          }
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d9);
        tft.setCursor(147, 29);
        tft.print(h9);
        tft.setCursor(173, 29);
        tft.print(m9);
        tft.setCursor(202, 29);
        tft.print(s9);
      }

      if (numc == 10) {
        if ( tm == 1 ) {
          if (d10 > 0) {
            d10--;
          }
        }
        if ( tm == 2 ) {
          if (h10 > 0) {
            h10--;
          }
        }
        if ( tm == 3 ) {
          if (m10 > 0) {
            m10--;
          }
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d10);
        tft.setCursor(147, 29);
        tft.print(h10);
        tft.setCursor(173, 29);
        tft.print(m10);
        tft.setCursor(202, 29);
        tft.print(s10);
      }

      if (numc == 11) {
        if ( tm == 1 ) {
          if (d11 > 0) {
            d11--;
          }
        }
        if ( tm == 2 ) {
          if (h11 > 0) {
            h11--;
          }
        }
        if ( tm == 3 ) {
          if (m11 > 0) {
            m11--;
          }
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d11);
        tft.setCursor(147, 29);
        tft.print(h11);
        tft.setCursor(173, 29);
        tft.print(m11);
        tft.setCursor(202, 29);
        tft.print(s11);
      }

      if (numc == 12) {
        if ( tm == 1 ) {
          if (d12 > 0) {
            d12--;
          }
        }
        if ( tm == 2 ) {
          if (h12 > 0) {
            h12--;
          }
        }
        if ( tm == 3 ) {
          if (m12 > 0) {
            m12--;
          }
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d12);
        tft.setCursor(147, 29);
        tft.print(h12);
        tft.setCursor(173, 29);
        tft.print(m12);
        tft.setCursor(202, 29);
        tft.print(s12);
      }
      delay(400);
    }
    //----------------------------------- +
    if (x > 182 && x < 227 && y > 46 && y < 88 ) {
      digitalWrite(pinRele_1, LOW);
      digitalWrite(pinRele_2, LOW);
      digitalWrite(pinRele_3, LOW);
      //-----------------
      puntoX = 182; puntoY = 46;
      delay(400);
      if (numc == 1) {
        if ( tm == 1 ) {
          d1++;
        }
        delay(50);
        if ( tm == 2 ) {
          h1++;
        }
        delay(50);
        if ( tm == 3 ) {
          m1++;
        }
        delay(50);
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d1);
        tft.setCursor(147, 29);
        tft.print(h1);
        tft.setCursor(173, 29);
        tft.print(m1);
        tft.setCursor(202, 29);
        tft.print(s1);
      }
      if (numc == 2) {
        if ( tm == 1 ) {
          d2++;
        }
        if ( tm == 2 ) {
          h2++;
        }
        if ( tm == 3 ) {
          m2++;
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d2);
        tft.setCursor(147, 29);
        tft.print(h2);
        tft.setCursor(173, 29);
        tft.print(m2);
        tft.setCursor(202, 29);
        tft.print(s2);
      }
      if (numc == 3) {
        if ( tm == 1 ) {
          d3++;
        }
        if ( tm == 2 ) {
          h3++;
        }
        if ( tm == 3 ) {
          m3++;
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d3);
        tft.setCursor(147, 29);
        tft.print(h3);
        tft.setCursor(173, 29);
        tft.print(m3);
        tft.setCursor(202, 29);
        tft.print(s3);
      }
      if (numc == 4) {
        if ( tm == 1 ) {
          d4++;
        }
        if ( tm == 2 ) {
          h4++;
        }
        if ( tm == 3 ) {
          m4++;
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d4);
        tft.setCursor(147, 29);
        tft.print(h4);
        tft.setCursor(173, 29);
        tft.print(m4);
        tft.setCursor(202, 29);
        tft.print(s4);
      }
      if (numc == 5) {
        if ( tm == 1 ) {
          d5++;
        }
        if ( tm == 2 ) {
          h5++;
        }
        if ( tm == 3 ) {
          m5++;
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d5);
        tft.setCursor(147, 29);
        tft.print(h5);
        tft.setCursor(173, 29);
        tft.print(m5);
        tft.setCursor(202, 29);
        tft.print(s5);
      }
      if (numc == 6) {
        if ( tm == 1 ) {
          d6++;
        }
        if ( tm == 2 ) {
          h6++;
        }
        if ( tm == 3 ) {
          m6++;
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d6);
        tft.setCursor(147, 29);
        tft.print(h6);
        tft.setCursor(173, 29);
        tft.print(m6);
        tft.setCursor(202, 29);
        tft.print(s6);
      }
      if (numc == 7) {
        if ( tm == 1 ) {
          d7++;
        }
        if ( tm == 2 ) {
          h7++;
        }
        if ( tm == 3 ) {
          m7++;
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d7);
        tft.setCursor(147, 29);
        tft.print(h7);
        tft.setCursor(173, 29);
        tft.print(m7);
        tft.setCursor(202, 29);
        tft.print(s7);
      }
      if (numc == 8) {
        if ( tm == 1 ) {
          d8++;
        }
        if ( tm == 2 ) {
          h8++;
        }
        if ( tm == 3 ) {
          m8++;
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d8);
        tft.setCursor(147, 29);
        tft.print(h8);
        tft.setCursor(173, 29);
        tft.print(m8);
        tft.setCursor(202, 29);
        tft.print(s8);
      }
      if (numc == 9) {
        if ( tm == 1 ) {
          d9++;
        }
        if ( tm == 2 ) {
          h9++;
        }
        if ( tm == 3 ) {
          m9++;
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d9);
        tft.setCursor(147, 29);
        tft.print(h9);
        tft.setCursor(173, 29);
        tft.print(m9);
        tft.setCursor(202, 29);
        tft.print(s9);
      }
      if (numc == 10) {
        if ( tm == 1 ) {
          d10++;
        }
        if ( tm == 2 ) {
          h10++;
        }
        if ( tm == 3 ) {
          m10++;
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d10);
        tft.setCursor(147, 29);
        tft.print(h10);
        tft.setCursor(173, 29);
        tft.print(m10);
        tft.setCursor(202, 29);
        tft.print(s10);
      }
      if (numc == 11) {
        if ( tm == 1 ) {
          d11++;
        }
        if ( tm == 2 ) {
          h11++;
        }
        if ( tm == 3 ) {
          m11++;
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d11);
        tft.setCursor(147, 29);
        tft.print(h11);
        tft.setCursor(173, 29);
        tft.print(m11);
        tft.setCursor(202, 29);
        tft.print(s11);
      }
      if (numc == 12) {
        if ( tm == 1 ) {
          d12++;
        }
        if ( tm == 2 ) {
          h12++;
        }
        if ( tm == 3 ) {
          m12++;
        }
        tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
        tft.setTextSize(2);
        tft.setCursor(125, 29);
        tft.print(d12);
        tft.setCursor(147, 29);
        tft.print(h12);
        tft.setCursor(173, 29);
        tft.print(m12);
        tft.setCursor(202, 29);
        tft.print(s12);
      }
    }

    //----------------------------------- VAC
    if (x > 66 && x < 121 && y > 94 && y < 141 ) {
      //-----------------r
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      digitalWrite(pinRele_1, LOW);
      digitalWrite(pinRele_2, LOW);
      digitalWrite(pinRele_3, LOW);
      //-----------------
      puntoX = 64 ; puntoY = 93;
      imagenBoton1();
      delay(400);
      imagenInterfaz();

      //-----------------
    }

    //----------------------------------- -   // RPD
    if (x > 120 && x < 240 && y > 85 && y < 138 ) {
      //-----------------
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      digitalWrite(pinRele_1, LOW);
      digitalWrite(pinRele_2, LOW);
      digitalWrite(pinRele_3, LOW);
      //-----------------
      puntoX = 122; puntoY = 93;
      imagenBoton();
      delay(400);
      imagenInterfaz();
      //-----------------
    }

    //----------------------------------AS
    if (x > 7 && x < 52 && y > 151 && y < 190 ) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      //-----------------
      puntoX = 7; puntoY = 139;
      imagenBoton1();
      delay(400);
      imagenInterfaz();
      //-----------------
    }

    //----------------------------------- MS
    if (x > 66 && x < 121 && y > 151 && y < 190 ) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      //-----------------
      digitalWrite(pinRele_1, LOW);
      digitalWrite(pinRele_2, LOW);
      digitalWrite(pinRele_3, LOW);
      //-----------------
      puntoX = 66; puntoY = 139;
      imagenBoton1();
      delay(400);
      imagenInterfaz();
      //-----------------
    }

    //----------------------------------- -   // ENTER
    if (x > 130 && x < 230 && y > 152 && y < 180 ) {
      //-----------------
      digitalWrite(pinRele_1, LOW);
      digitalWrite(pinRele_2, LOW);
      digitalWrite(pinRele_3, LOW);
      //-----------------
      tft.fillRoundRect(7, 20, 227, 25, 2, ILI9341_DARKGREEN);
      tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
      tft.setTextSize(2);
      tft.setCursor(100, 25);
      tft.print("Seguro?");
      tft.setCursor(20, 25);
      tft.setTextSize(2);
      tft.setTextColor(ILI9341_GREEN, ILI9341_DARKGREEN);
      tft.print("Si");
      tft.setTextSize(2);
      tft.setCursor(50, 25);
      tft.setTextColor(ILI9341_RED, ILI9341_DARKGREEN);
      tft.print("No");
      x = 0; y = 0;
      puntoX = 130; puntoY = 152;
      //-----------------
    }



    //----------------------------------CLOCK
    if (x > 7 && x < 52 && y > 185 && y < 212 ) {
      //-----------------
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
      tft.setTextSize(2);
      tft.setCursor(10, 5);
      tft.print("Can.");
      tft.setCursor(55, 5);
      tft.print(numc);
      tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
      tft.setTextSize(1);
      tft.setCursor(128, 20);
      tft.print("D");
      tft.setCursor(150, 20);
      tft.print("H");
      tft.setCursor(176, 20);
      tft.print("M");
      tft.setCursor(205, 20);
      tft.print("S");
      tft.setTextSize(2);

      //-----------------
      switch (numc) {
        case 1:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d1);
          tft.setCursor(147, 29);
          tft.print(h1);
          tft.setCursor(173, 29);
          tft.print(m1);
          tft.setCursor(202, 29);
          tft.print(s1);
          break;

        case 2:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d2);
          tft.setCursor(147, 29);
          tft.print(h2);
          tft.setCursor(173, 29);
          tft.print(m2);
          tft.setCursor(202, 29);
          tft.print(s2);
          break;

        case 3:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d3);
          tft.setCursor(147, 29);
          tft.print(h3);
          tft.setCursor(173, 29);
          tft.print(m3);
          tft.setCursor(202, 29);
          tft.print(s3);
          break;

        case 4:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d4);
          tft.setCursor(147, 29);
          tft.print(h4);
          tft.setCursor(173, 29);
          tft.print(m4);
          tft.setCursor(202, 29);
          tft.print(s4);
          break;

        case 5:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d5);
          tft.setCursor(147, 29);
          tft.print(h5);
          tft.setCursor(173, 29);
          tft.print(m5);
          tft.setCursor(202, 29);
          tft.print(s5);
          break;

        case 6:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d6);
          tft.setCursor(147, 29);
          tft.print(h6);
          tft.setCursor(173, 29);
          tft.print(m6);
          tft.setCursor(202, 29);
          tft.print(s6);
          break;

        case 7:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d7);
          tft.setCursor(147, 29);
          tft.print(h7);
          tft.setCursor(173, 29);
          tft.print(m7);
          tft.setCursor(202, 29);
          tft.print(s7);
          break;

        case 8:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d8);
          tft.setCursor(147, 29);
          tft.print(h8);
          tft.setCursor(173, 29);
          tft.print(m8);
          tft.setCursor(202, 29);
          tft.print(s8);
          break;

        case 9:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d9);
          tft.setCursor(147, 29);
          tft.print(h9);
          tft.setCursor(173, 29);
          tft.print(m9);
          tft.setCursor(202, 29);
          tft.print(s9);
          break;

        case 10:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d10);
          tft.setCursor(147, 29);
          tft.print(h10);
          tft.setCursor(173, 29);
          tft.print(m10);
          tft.setCursor(202, 29);
          tft.print(s10);
          break;

        case 11:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d11);
          tft.setCursor(147, 29);
          tft.print(h11);
          tft.setCursor(173, 29);
          tft.print(m11);
          tft.setCursor(202, 29);
          tft.print(s11);
          break;

        case 12:
          tft.setTextColor(ILI9341_WHITE, ILI9341_DARKGREEN);
          tft.setTextSize(2);
          tft.setCursor(125, 29);
          tft.print(d12);
          tft.setCursor(147, 29);
          tft.print(h12);
          tft.setCursor(173, 29);
          tft.print(m12);
          tft.setCursor(202, 29);
          tft.print(s12);
          break;
      }

      //-----------------
    }

    //----------------------------------- ST
    if (x > 64 && x < 121 && y > 185 && y < 212 ) {

    }

    //----------------------------------- -   DD
    if (x > 124 && x < 169 && y > 185 && y < 212 ) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      //-----------------
      digitalWrite(pinRele_1, LOW);
      digitalWrite(pinRele_2, LOW);
      digitalWrite(pinRele_3, LOW);
      //-----------------
      puntoX = 124; puntoY = 185;
      imagenBoton1();
      delay(400);
      imagenInterfaz();
      //-----------------
    }
    //----------------------------------- DO
    if (x > 182 && x < 227 && y > 185 && y < 212 ) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      //-----------------
      digitalWrite(pinRele_1, LOW);
      digitalWrite(pinRele_2, LOW);
      digitalWrite(pinRele_3, LOW);
      //-----------------
      puntoX = 182; puntoY = 185;
      imagenBoton1();
      delay(400);
      imagenInterfaz();
      //-----------------
    }


    //----------------------------------ROTATE
    if (x > 7 && x < 52 && y > 230 && y < 275 ) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      //-----------------
      if (numc <= 12) {
        numc = numc + 1;
      }

      if ( numc == 13) {
        numc = 1;
      }
      puntoX = 7; puntoY = 230;
      imagenBoton1();
      imagenInterfaz();
      delay(400);
      //-----------------
    }

    //----------------------------------- RAISE
    if (x > 64 && x < 121 && y > 230 && y < 275 ) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      //-----------------
      digitalWrite(pinRele_1, LOW);
      digitalWrite(pinRele_2, LOW);
      digitalWrite(pinRele_3, LOW);
      //-----------------
      puntoX = 64; puntoY = 230;
      imagenBoton1();
      delay(400);
      imagenInterfaz();
      //-----------------
    }

    //----------------------------------- -   // LOWER
    if (x > 124 && x < 169 && y > 230 && y < 275 ) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      //-----------------
      digitalWrite(pinRele_1, LOW);
      digitalWrite(pinRele_2, LOW);
      digitalWrite(pinRele_3, LOW);
      //-----------------
      puntoX = 124; puntoY = 230;
      imagenBoton1();
      delay(400);
      imagenInterfaz();
      //-----------------
    }
    //----------------------------------- GO TO
    if (x > 182 && x < 227 && y > 230 && y < 275 ) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      //-----------------
      digitalWrite(pinRele_1, LOW);
      digitalWrite(pinRele_2, LOW);
      digitalWrite(pinRele_3, LOW);
      //-----------------
      puntoX = 182; puntoY = 230;
      imagenBoton1();
      delay(400);
      imagenInterfaz();
      //-----------------
    }

    //----------------------------------CF
    if (x > 7 && x < 52 && y > 280 && y < 355 ) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      //-----------------
      puntoX = 7; puntoY = 275;
      imagenBoton1();
      delay(400);
      imagenInterfaz();
      //-----------------
    }

    //----------------------------------- ES
    if (x > 64 && x < 150 && y > 280 && y < 355 ) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      //-----------------
      digitalWrite(pinRele_1, LOW);
      digitalWrite(pinRele_3, LOW);
      //-----------------
      puntoX = 64; puntoY = 275;
      imagenBoton3();
      delay(400);
      imagenInterfaz();
      //-----------------
    }

    //-----------------------------------   CA
    if (x > 153 && x < 300 && y > 280 && y < 355 ) {
      tft.fillRoundRect(7, 0, 229, 45, 2, ILI9341_DARKGREEN);
      //-----------------
      digitalWrite(pinRele_1, LOW);
      digitalWrite(pinRele_2, LOW);
      digitalWrite(pinRele_3, LOW);
      //-----------------
      puntoX = 152; puntoY = 275;
      imagenBoton3();
      delay(400);
      imagenInterfaz();
    }



  }//FIN tft.getTouch
}
//------------------------------------------------------------------------------------------
//REFRESCO DE DISPLAY
void imagenInterfaz() {
  tft.pushImage(0, 0, animation_width, animation_height, frame);
}
//------------------------------------------------------------------------------------------

I told you it was long, I have the other file which is the .h file but there are only hexadecimal to make an image appear on the screen

@davids2374 yikes.

Admit it, you did quite a bit of cutting, pasting and editing whilst composing this monstrosity, no?

Learn about functions and array variables.

In combination with loops, this will allow you to hugely exploit the several basic repeating patterns discernible in code as I scrolled scrolled scrolled though it.

Your code will be smaller, easier to write, get right, modify and enhance.

I also seem to recall seeing a few totally reducible switch/case statements flying by, I'll take a closer look when I am back in the lab.

a7

Of course I did, when I needed to change a variable I needed to change all, I did that a lot of times

Ok thank you

Yes, that is the point @alto777 is making: when you find yourself doing that, you know you have left the path of "coding wisdom".

true, so I instead decide to use the find and replace tool but it changes things that aren't supposed to change so I need to manually change all.

Here's your first thing to fix. This should be an array.

const byte pinRele[3] = {32, 35, 34};

so then

becomes

  for(byte j=0; j<3; j++) {
    pinMode(pinRele[j], OUTPUT);
    digitalWrite(pinRele[j], LOW);
  }

and that's just the start.

1 Like

cool! Thanks for that, I will see if it works

Good, good. Next up:

bool e1  = false;
bool e2  = false;
bool e3  = false;
bool e4  = false;
bool e5  = false;
bool e6  = false;
bool e7  = false;
bool e8  = false;
bool e9  = false;
bool e10 = false;
bool e11 = false;
bool e12 = false;

becomes

bool e[12];

(No need to set each to false, that will happen by default).

Also "e" is not a good name for an array, it says nothing about what the array is for. So think of a better name.

2 Likes

I'm still on at the beach, but here's what two switch/case statements might look like if the e variable was an arrray instead.

   if (x > 14 && x < 40 && y > 1 && y < 30 )
          eAsAnArray[numc] = true;

   if (x > 44 && x < 70 && y > 1 && y < 30 )
      eAsAnArray[numc] = false;

HTH

a7

1 Like