More efficient code that won't upset my OCD

Hi,

I have been writing the following programme to create a GUI for a 4 channel DAC output. It is getting more verbose and ugly though; I think I can make it better by passing some values to functions but I'm not sure what would actually make a difference?

//
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h>
#include <TouchScreen.h>

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

// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

#define MINPRESSURE 10
#define MAXPRESSURE 1000

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);


#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

#define Ch1V_X 10
#define Ch1V_Y 10
#define Ch1A_X 170
#define Ch1A_Y 10
#define Ch2V_X 10
#define Ch2V_Y 90
#define Ch2A_X 170
#define Ch2A_Y 90
#define WINDOW_W 140
#define WINDOW_H 60

#define Inc_100_Button_X 10
#define Inc_100_Button_Y 170
#define Inc_10_Button_X 60
#define Inc_10_Button_Y 170
#define Inc_1_Button_X 110
#define Inc_1_Button_Y 170
#define Dec_100_Button_X 170
#define Dec_100_Button_Y 170
#define Dec_10_Button_X 220
#define Dec_10_Button_Y 170
#define Dec_1_Button_X 270
#define Dec_1_Button_Y 170
#define Button_W 40
#define Button_H 40

int Ch1V_DAC = 0;
int Ch1A_DAC = 0;
int Ch2V_DAC = 0;
int Ch2A_DAC = 0;

byte ChannelSelect = 0;

void drawbuttons() {
  tft.fillRect(Inc_100_Button_X, Inc_100_Button_Y, Button_W, Button_H, ILI9341_WHITE);
  tft.setCursor(Inc_100_Button_X + 6 , Inc_100_Button_Y + (Button_H / 2));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(1);
  tft.println("+100");

  tft.fillRect(Inc_10_Button_X, Inc_10_Button_Y, Button_W, Button_H, ILI9341_WHITE);
  tft.setCursor(Inc_10_Button_X + 6 , Inc_10_Button_Y + (Button_H / 2));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(1);
  tft.println("+10");

  tft.fillRect(Inc_1_Button_X, Inc_1_Button_Y, Button_W, Button_H, ILI9341_WHITE);
  tft.setCursor(Inc_1_Button_X + 6 , Inc_1_Button_Y + (Button_H / 2));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(1);
  tft.println("+1");

  tft.fillRect(Dec_100_Button_X, Dec_100_Button_Y, Button_W, Button_H, ILI9341_WHITE);
  tft.setCursor(Dec_100_Button_X + 6 , Dec_100_Button_Y + (Button_H / 2));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(1);
  tft.println("-100");

tft.fillRect(Dec_10_Button_X, Dec_10_Button_Y, Button_W, Button_H, ILI9341_WHITE);
  tft.setCursor(Dec_10_Button_X + 6 , Dec_10_Button_Y + (Button_H / 2));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(1);
  tft.println("-10");

  tft.fillRect(Dec_1_Button_X, Dec_1_Button_Y, Button_W, Button_H, ILI9341_WHITE);
  tft.setCursor(Dec_1_Button_X + 6 , Dec_1_Button_Y + (Button_H / 2));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(1);
  tft.println("-1");
  
}

void updateCh1V() {
  tft.fillRect(Ch1V_X, Ch1V_Y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
  tft.setCursor(Ch1V_X + 5 , Ch1V_Y + 4);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("Channel 1 V");
  tft.setCursor(Ch1V_X + 10 , Ch1V_Y + (WINDOW_H / 2));
  if (ChannelSelect == 1) {
    tft.setTextColor(ILI9341_RED);
  }
  else {
    tft.setTextColor(ILI9341_BLUE);
  }
  tft.setTextSize(3);
  tft.println(Ch1V_DAC);
}

void updateCh1A() {
  tft.fillRect(Ch1A_X, Ch1A_Y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
  tft.setCursor(Ch1A_X + 5 , Ch1A_Y + 4);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("Channel 1 A");
  tft.setCursor(Ch1A_X + 10 , Ch1A_Y + (WINDOW_H / 2));
  if (ChannelSelect == 2) {
    tft.setTextColor(ILI9341_RED);
  }
  else {
    tft.setTextColor(ILI9341_BLUE);
  }
  tft.setTextSize(3);
  tft.println(Ch1A_DAC);
}

void updateCh2V() {
  tft.fillRect(Ch2V_X, Ch2V_Y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
  tft.setCursor(Ch2V_X + 5 , Ch2V_Y + 4);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("Channel 2 V");
  tft.setCursor(Ch2V_X + 10 , Ch2V_Y + (WINDOW_H / 2));
  if (ChannelSelect == 3) {
    tft.setTextColor(ILI9341_RED);
  }
  else {
    tft.setTextColor(ILI9341_BLUE);
  }
  tft.setTextSize(3);
  tft.println(Ch2V_DAC);
}

void updateCh2A() {
  tft.fillRect(Ch2A_X, Ch2A_Y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
  tft.setCursor(Ch2A_X + 5 , Ch2A_Y + 4);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("Channel 2 A");
  tft.setCursor(Ch2A_X + 10 , Ch2A_Y + (WINDOW_H / 2));
  if (ChannelSelect == 4) {
    tft.setTextColor(ILI9341_RED);
  }
  else {
    tft.setTextColor(ILI9341_BLUE);
  }
  tft.setTextSize(3);
  tft.println(Ch2A_DAC);
}

void setup() {
  Serial.begin(9600);
  tft.begin();

  tft.fillScreen(ILI9341_BLUE);
  // origin = left,top landscape (USB left upper)
  tft.setRotation(1);

  updateCh1V();
  updateCh1A();
  updateCh2V();
  updateCh2A();
  drawbuttons();
}

void loop() {
  // Retrieve a point
  TSPoint p = ts.getPoint();

  // See if there's any  touch data for us
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
  {
    // Scale using the calibration #'s
    // and rotate coordinate system
    p.x = map(p.x, TS_MINY, TS_MAXY, 0, tft.height());
    p.y = map(p.y, TS_MINX, TS_MAXX, 0, tft.width());
    int y = tft.height() - p.x;
    int x = p.y;

    if ((x > Ch1V_X) && (x < (Ch1V_X + WINDOW_W))) {
      if ((y > Ch1V_Y) && (y <= (Ch1V_Y + WINDOW_H))) {
        ChannelSelect = 1;
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
      }
    }

    if ((x > Ch1A_X) && (x < (Ch1A_X + WINDOW_W))) {
      if ((y > Ch1A_Y) && (y <= (Ch1A_Y + WINDOW_H))) {
        ChannelSelect = 2;
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
      }
    }

    if ((x > Ch2V_X) && (x < (Ch2V_X + WINDOW_W))) {
      if ((y > Ch2V_Y) && (y <= (Ch2V_Y + WINDOW_H))) {
        ChannelSelect = 3;
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
      }
    }

    if ((x > Ch2A_X) && (x < (Ch2A_X + WINDOW_W))) {
      if ((y > Ch2A_Y) && (y <= (Ch2A_Y + WINDOW_H))) {
        ChannelSelect = 4;
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
      }
    }

    if ((x > Inc_100_Button_X) && (x < (Inc_100_Button_X + Button_W))) {
      if ((y > Inc_100_Button_Y) && (y <= (Inc_100_Button_Y + Button_H))) {
        if (ChannelSelect == 1) {
          if(Ch1V_DAC >=2048){ Ch1V_DAC = 2048;}
          else {Ch1V_DAC += 100;}
        }
        if (ChannelSelect == 2) {
          if(Ch1A_DAC >=2048){ Ch1A_DAC = 2048;}
          else {Ch1A_DAC += 100;}
        }
        if (ChannelSelect == 3) {
          if(Ch2V_DAC >=2048){ Ch2V_DAC = 2048;}
          else {Ch2V_DAC += 100;}
        }
       if (ChannelSelect == 4) {
          if(Ch2A_DAC >=2048){ Ch2A_DAC = 2048;}
          else {Ch2A_DAC += 100;}
        }
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
        delay(100);
      }
    }

  if ((x > Inc_10_Button_X) && (x < (Inc_10_Button_X + Button_W))) {
      if ((y > Inc_10_Button_Y) && (y <= (Inc_10_Button_Y + Button_H))) {
        if (ChannelSelect == 1) {
          if(Ch1V_DAC >=2048){ Ch1V_DAC = 2048;}
          else {Ch1V_DAC += 10;}
        }
        if (ChannelSelect == 2) {
          if(Ch1A_DAC >=2048){ Ch1A_DAC = 2048;}
          else {Ch1A_DAC += 10;}
        }
        if (ChannelSelect == 3) {
          if(Ch2V_DAC >=2048){ Ch2V_DAC = 2048;}
          else {Ch2V_DAC += 10;}
        }
        if (ChannelSelect == 4) {
          if(Ch2A_DAC >=2048){ Ch2A_DAC = 2048;}
          else {Ch2A_DAC += 10;}
        }
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
        delay(100);
      }
    }

     if ((x > Inc_1_Button_X) && (x < (Inc_1_Button_X + Button_W))) {
      if ((y > Inc_1_Button_Y) && (y <= (Inc_1_Button_Y + Button_H))) {
        if (ChannelSelect == 1) {
          if(Ch1V_DAC >=2048){ Ch1V_DAC = 2048;}
          else {Ch1V_DAC += 1;}
        }
        if (ChannelSelect == 2) {
          if(Ch1A_DAC >=2048){ Ch1A_DAC = 2048;}
          else {Ch1A_DAC += 10;}
        }
        if (ChannelSelect == 3) {
          if(Ch2V_DAC >=2048){ Ch2V_DAC = 2048;}
          else {Ch2V_DAC += 1;}
        }
        if (ChannelSelect == 4) {
          if(Ch2A_DAC >=2048){ Ch2A_DAC = 2048;}
          else {Ch2A_DAC += 1;}
        }
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
        delay(100);
      }
    }

    if ((x > Dec_100_Button_X) && (x < (Dec_100_Button_X + Button_W))) {
      if ((y > Dec_100_Button_Y) && (y <= (Dec_100_Button_Y + Button_H))) {
        if (ChannelSelect == 1) {
          if(Ch1V_DAC <=0){ Ch1V_DAC = 0;}
          else {Ch1V_DAC -= 100;}
        }
        if (ChannelSelect == 2) {
          if(Ch1A_DAC <=0){ Ch1A_DAC = 0;}
          else {Ch1A_DAC -= 100;}
        }
        if (ChannelSelect == 3) {
             if(Ch2V_DAC <=0){ Ch2V_DAC = 0;}
          else {Ch2V_DAC -= 100;}
        }
        if (ChannelSelect == 4) {
              if(Ch2A_DAC <=0){ Ch2A_DAC = 0;}
          else {Ch2A_DAC -= 100;}
        }
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
        delay(100);
      }
    }

    if ((x > Dec_10_Button_X) && (x < (Dec_10_Button_X + Button_W))) {
      if ((y > Dec_10_Button_Y) && (y <= (Dec_10_Button_Y + Button_H))) {
        if (ChannelSelect == 1) {
            if(Ch1V_DAC <=0){ Ch1V_DAC = 0;}
          else {Ch1V_DAC -= 10;}
        }
        if (ChannelSelect == 2) {
          if(Ch1A_DAC <=0){ Ch1A_DAC = 0;}
          else {Ch1A_DAC -= 10;}
        }
        if (ChannelSelect == 3) {
             if(Ch2V_DAC <=0){ Ch2V_DAC = 0;}
          else {Ch2V_DAC -= 10;}
        }
        if (ChannelSelect == 4) {
              if(Ch2A_DAC <=0){ Ch2A_DAC = 0;}
          else {Ch2A_DAC -= 10;}
        }
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
        delay(100);
      }
    }

    if ((x > Dec_1_Button_X) && (x < (Dec_1_Button_X + Button_W))) {
      if ((y > Dec_1_Button_Y) && (y <= (Dec_1_Button_Y + Button_H))) {
        if (ChannelSelect == 1) {
        if(Ch1V_DAC <=0){ Ch1V_DAC = 0;}
          else {Ch1V_DAC -= 1;}
        }
        if (ChannelSelect == 2) {
          if(Ch1A_DAC <=0){ Ch1A_DAC = 0;}
          else {Ch1A_DAC -= 1;}
        }
        if (ChannelSelect == 3) {
             if(Ch2V_DAC <=0){ Ch2V_DAC = 0;}
          else {Ch2V_DAC -= 1;}
        }
        if (ChannelSelect == 4) {
              if(Ch2A_DAC <=0){ Ch2A_DAC = 0;}
          else {Ch2A_DAC -= 1;}
        }
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
        delay(100);
      }
    }
    
  }
}

No explanation on what’s not working.
No comments.
No formatting.
Not posting the code properly.


= no help.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

Your "buttons" are all the same except for X,Y position and Label, right?
So you could have a function:

   drawButton(int x, int y, const char * label);

and Similar for updating the display.

Or classes. Display areas are a real natural for "objects"; sometimes I think that OOP only came about so that programmers could more easily manipulate the graphics displays that were starting to become popular. That may be more "advanced" than you want to get into. Or it might be an "interesting exercise."

 class button...
 class displayWindow...

looks like you have several functions that are similar (e.g. updateCh1V) that could be replaced by a single sub-function with arguments for the different cases

the parameters (e.g. _X, _Y and _DAC) could be specified in a table and that table entry capture all info regarding that channel.

is this what you're looking for?

What do you think this does?

If you said which DAC you are using and how many bits it has I missed it. I am guessing from 2048 that it is 12 bit. 12 bits gives you 0 to 2047, 2048 is out of the range of the DAC. You check to see if the value of Ch1V_DAC is 2048 or greater than 2048 and then set it to 2048 if it is, which will be 1 more than its maximum. Then, if it is 2047 or less you add 100, making it possibly 2147, 100 more than the maximum.

Do the addition first, then check if you've gone over 2047 and remember that 2047 if the maximum, not 2048.

int Ch1V_DAC = 0;
int Ch1A_DAC = 0;
int Ch2V_DAC = 0;
int Ch2A_DAC = 0;

When you have something like that it cries out for an array - Arduino Reference

Maybe

int ChxDAC [4];

I would not use an int for something that cannot ever be negative. My preference (it is a preference, not everyone agrees) is to use types that make clear the number of bits, so in this case I would use uint16_t, which is a 16 bit unsigned integer. Depending on the platform int and unsigned int can be 16 or 32 bits.

And please, edit your post to use code tags </>

Apologies, new to this forum. The code works, but there is a lot more to go and I feel I am making it unwieldy and clunky already....


//
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h>
#include <TouchScreen.h>

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

// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

#define MINPRESSURE 10
#define MAXPRESSURE 1000

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);


#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

#define Ch1V_X 10
#define Ch1V_Y 10
#define Ch1A_X 170
#define Ch1A_Y 10
#define Ch2V_X 10
#define Ch2V_Y 90
#define Ch2A_X 170
#define Ch2A_Y 90
#define WINDOW_W 140
#define WINDOW_H 60

#define ButtonBarLeft_X 10
#define ButtonBarLeft_Y 210
#define ButtonBarRight_X 170
#define ButtonBarRight_Y 210
#define ButtonBar_W 140
#define ButtonBar_H 20

#define Inc_100_Button_X 10
#define Inc_100_Button_Y 170
#define Inc_10_Button_X 60
#define Inc_10_Button_Y 170
#define Inc_1_Button_X 110
#define Inc_1_Button_Y 170
#define Dec_100_Button_X 170
#define Dec_100_Button_Y 170
#define Dec_10_Button_X 220
#define Dec_10_Button_Y 170
#define Dec_1_Button_X 270
#define Dec_1_Button_Y 170
#define Button_W 40
#define Button_H 30

// Store DAC values

int Ch1V_DAC = 0;
int Ch1A_DAC = 0;
int Ch2V_DAC = 0;
int Ch2A_DAC = 0;

byte ChannelSelect = 0; // Contols selected channel on touchscreen

void drawbuttons() {

  tft.fillRect(ButtonBarLeft_X, ButtonBarLeft_Y, ButtonBar_W, ButtonBar_H, ILI9341_WHITE);
  tft.setCursor(ButtonBarLeft_X + 40 , ButtonBarLeft_Y + (ButtonBar_H / 3));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(1);
  tft.println("Increase");

  tft.fillRect(Inc_100_Button_X, Inc_100_Button_Y, Button_W, Button_H, ILI9341_WHITE);
  tft.setCursor(Inc_100_Button_X + 2 , Inc_100_Button_Y + (Button_H / 3));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("100");

  tft.fillRect(Inc_10_Button_X, Inc_10_Button_Y, Button_W, Button_H, ILI9341_WHITE);
  tft.setCursor(Inc_10_Button_X + 6 , Inc_10_Button_Y + (Button_H / 3));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("10");

  tft.fillRect(Inc_1_Button_X, Inc_1_Button_Y, Button_W, Button_H, ILI9341_WHITE);
  tft.setCursor(Inc_1_Button_X + 15 , Inc_1_Button_Y + (Button_H / 3));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("1");

  tft.fillRect(ButtonBarRight_X, ButtonBarRight_Y, ButtonBar_W, ButtonBar_H, ILI9341_WHITE);
  tft.setCursor(ButtonBarRight_X + 40 , ButtonBarRight_Y + (ButtonBar_H / 3));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(1);
  tft.println("Decrease");

  tft.fillRect(Dec_100_Button_X, Dec_100_Button_Y, Button_W, Button_H, ILI9341_WHITE);
  tft.setCursor(Dec_100_Button_X + 2 , Dec_100_Button_Y + (Button_H / 3));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("100");

  tft.fillRect(Dec_10_Button_X, Dec_10_Button_Y, Button_W, Button_H, ILI9341_WHITE);
  tft.setCursor(Dec_10_Button_X + 6 , Dec_10_Button_Y + (Button_H / 3));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("10");

  tft.fillRect(Dec_1_Button_X, Dec_1_Button_Y, Button_W, Button_H, ILI9341_WHITE);
  tft.setCursor(Dec_1_Button_X + 15 , Dec_1_Button_Y + (Button_H / 3));
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("1");

}

void updateCh1V() {
  tft.fillRect(Ch1V_X, Ch1V_Y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
  tft.setCursor(Ch1V_X + 5 , Ch1V_Y + 4);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("Channel 1 V");
  tft.setCursor(Ch1V_X + 10 , Ch1V_Y + (WINDOW_H / 2));
  if (ChannelSelect == 1) {
    tft.setTextColor(ILI9341_RED);  // Changes colour if this channel is selected
  }
  else {
    tft.setTextColor(ILI9341_BLUE);
  }
  tft.setTextSize(3);
  tft.println(Ch1V_DAC);
}

void updateCh1A() {
  tft.fillRect(Ch1A_X, Ch1A_Y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
  tft.setCursor(Ch1A_X + 5 , Ch1A_Y + 4);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("Channel 1 A");
  tft.setCursor(Ch1A_X + 10 , Ch1A_Y + (WINDOW_H / 2));
  if (ChannelSelect == 2) {
    tft.setTextColor(ILI9341_RED); // Changes colour if this channel is selected
  }
  else {
    tft.setTextColor(ILI9341_BLUE);
  }
  tft.setTextSize(3);
  tft.println(Ch1A_DAC);
}

void updateCh2V() {
  tft.fillRect(Ch2V_X, Ch2V_Y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
  tft.setCursor(Ch2V_X + 5 , Ch2V_Y + 4);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("Channel 2 V");
  tft.setCursor(Ch2V_X + 10 , Ch2V_Y + (WINDOW_H / 2));
  if (ChannelSelect == 3) {
    tft.setTextColor(ILI9341_RED);  // Changes colour if this channel is selected
  }
  else {
    tft.setTextColor(ILI9341_BLUE);
  }
  tft.setTextSize(3);
  tft.println(Ch2V_DAC);
}

void updateCh2A() {
  tft.fillRect(Ch2A_X, Ch2A_Y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
  tft.setCursor(Ch2A_X + 5 , Ch2A_Y + 4);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(2);
  tft.println("Channel 2 A");
  tft.setCursor(Ch2A_X + 10 , Ch2A_Y + (WINDOW_H / 2));
  if (ChannelSelect == 4) {
    tft.setTextColor(ILI9341_RED);  // Changes colour if this channel is selected
  }
  else {
    tft.setTextColor(ILI9341_BLUE);
  }
  tft.setTextSize(3);
  tft.println(Ch2A_DAC);
}

void setup() {
  Serial.begin(9600);
  tft.begin();

  tft.fillScreen(ILI9341_BLUE);
  // origin = left,top landscape (USB left upper)
  tft.setRotation(1);

  updateCh1V();
  updateCh1A();
  updateCh2V();
  updateCh2A();
  drawbuttons();
}

void loop() {
  // Retrieve a point
  TSPoint p = ts.getPoint();

  // See if there's any  touch data for us
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
  {
    // Scale using the calibration #'s
    // and rotate coordinate system
    p.x = map(p.x, TS_MINY, TS_MAXY, 0, tft.height());
    p.y = map(p.y, TS_MINX, TS_MAXX, 0, tft.width());
    int y = tft.height() - p.x;
    int x = p.y;

    if ((x > Ch1V_X) && (x < (Ch1V_X + WINDOW_W))) {
      if ((y > Ch1V_Y) && (y <= (Ch1V_Y + WINDOW_H))) {
        ChannelSelect = 1;
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
      }
    }

    if ((x > Ch1A_X) && (x < (Ch1A_X + WINDOW_W))) {
      if ((y > Ch1A_Y) && (y <= (Ch1A_Y + WINDOW_H))) {
        ChannelSelect = 2;
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
      }
    }

    if ((x > Ch2V_X) && (x < (Ch2V_X + WINDOW_W))) {
      if ((y > Ch2V_Y) && (y <= (Ch2V_Y + WINDOW_H))) {
        ChannelSelect = 3;
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
      }
    }

    if ((x > Ch2A_X) && (x < (Ch2A_X + WINDOW_W))) {
      if ((y > Ch2A_Y) && (y <= (Ch2A_Y + WINDOW_H))) {
        ChannelSelect = 4;
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
      }
    }


    // This button increases the DAC value by 100

    if ((x > Inc_100_Button_X) && (x < (Inc_100_Button_X + Button_W))) {
      .
      if ((y > Inc_100_Button_Y) && (y <= (Inc_100_Button_Y + Button_H))) {
        if (ChannelSelect == 1) {
          if (Ch1V_DAC >= 2048) {
            Ch1V_DAC = 2048;
          }
          else {
            Ch1V_DAC += 100;
          }
        }
        if (ChannelSelect == 2) {
          if (Ch1A_DAC >= 2048) {
            Ch1A_DAC = 2048;
          }
          else {
            Ch1A_DAC += 100;
          }
        }
        if (ChannelSelect == 3) {
          if (Ch2V_DAC >= 2048) {
            Ch2V_DAC = 2048;
          }
          else {
            Ch2V_DAC += 100;
          }
        }
        if (ChannelSelect == 4) {
          if (Ch2A_DAC >= 2048) {
            Ch2A_DAC = 2048;
          }
          else {
            Ch2A_DAC += 100;
          }
        }
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
        delay(100);
      }
    }


    // This button increases the DAC value by 10

    if ((x > Inc_10_Button_X) && (x < (Inc_10_Button_X + Button_W))) {
      if ((y > Inc_10_Button_Y) && (y <= (Inc_10_Button_Y + Button_H))) {
        if (ChannelSelect == 1) {
          if (Ch1V_DAC >= 2048) {
            Ch1V_DAC = 2048;
          }
          else {
            Ch1V_DAC += 10;
          }
        }
        if (ChannelSelect == 2) {
          if (Ch1A_DAC >= 2048) {
            Ch1A_DAC = 2048;
          }
          else {
            Ch1A_DAC += 10;
          }
        }
        if (ChannelSelect == 3) {
          if (Ch2V_DAC >= 2048) {
            Ch2V_DAC = 2048;
          }
          else {
            Ch2V_DAC += 10;
          }
        }
        if (ChannelSelect == 4) {
          if (Ch2A_DAC >= 2048) {
            Ch2A_DAC = 2048;
          }
          else {
            Ch2A_DAC += 10;
          }
        }
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
        delay(100);
      }
    }

    // This button increases the DAC value by 1

    if ((x > Inc_1_Button_X) && (x < (Inc_1_Button_X + Button_W))) {
      if ((y > Inc_1_Button_Y) && (y <= (Inc_1_Button_Y + Button_H))) {
        if (ChannelSelect == 1) {
          if (Ch1V_DAC >= 2048) {
            Ch1V_DAC = 2048;
          }
          else {
            Ch1V_DAC += 1;
          }
        }
        if (ChannelSelect == 2) {
          if (Ch1A_DAC >= 2048) {
            Ch1A_DAC = 2048;
          }
          else {
            Ch1A_DAC += 10;
          }
        }
        if (ChannelSelect == 3) {
          if (Ch2V_DAC >= 2048) {
            Ch2V_DAC = 2048;
          }
          else {
            Ch2V_DAC += 1;
          }
        }
        if (ChannelSelect == 4) {
          if (Ch2A_DAC >= 2048) {
            Ch2A_DAC = 2048;
          }
          else {
            Ch2A_DAC += 1;
          }
        }
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
        delay(100);
      }
    }


    // This button decreaes the DAC value by 100

    if ((x > Dec_100_Button_X) && (x < (Dec_100_Button_X + Button_W))) {
      if ((y > Dec_100_Button_Y) && (y <= (Dec_100_Button_Y + Button_H))) {
        if (ChannelSelect == 1) {
          if (Ch1V_DAC <= 0) {
            Ch1V_DAC = 0;
          }
          else {
            Ch1V_DAC -= 100;
          }
        }
        if (ChannelSelect == 2) {
          if (Ch1A_DAC <= 0) {
            Ch1A_DAC = 0;
          }
          else {
            Ch1A_DAC -= 100;
          }
        }
        if (ChannelSelect == 3) {
          if (Ch2V_DAC <= 0) {
            Ch2V_DAC = 0;
          }
          else {
            Ch2V_DAC -= 100;
          }
        }
        if (ChannelSelect == 4) {
          if (Ch2A_DAC <= 0) {
            Ch2A_DAC = 0;
          }
          else {
            Ch2A_DAC -= 100;
          }
        }
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
        delay(100);
      }
    }


    // This button decreaes the DAC value by 10

    if ((x > Dec_10_Button_X) && (x < (Dec_10_Button_X + Button_W))) {
      if ((y > Dec_10_Button_Y) && (y <= (Dec_10_Button_Y + Button_H))) {
        if (ChannelSelect == 1) {
          if (Ch1V_DAC <= 0) {
            Ch1V_DAC = 0;
          }
          else {
            Ch1V_DAC -= 10;
          }
        }
        if (ChannelSelect == 2) {
          if (Ch1A_DAC <= 0) {
            Ch1A_DAC = 0;
          }
          else {
            Ch1A_DAC -= 10;
          }
        }
        if (ChannelSelect == 3) {
          if (Ch2V_DAC <= 0) {
            Ch2V_DAC = 0;
          }
          else {
            Ch2V_DAC -= 10;
          }
        }
        if (ChannelSelect == 4) {
          if (Ch2A_DAC <= 0) {
            Ch2A_DAC = 0;
          }
          else {
            Ch2A_DAC -= 10;
          }
        }
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
        delay(100);
      }
    }

    // This button decreaes the DAC value by 1

    if ((x > Dec_1_Button_X) && (x < (Dec_1_Button_X + Button_W))) {
      if ((y > Dec_1_Button_Y) && (y <= (Dec_1_Button_Y + Button_H))) {
        if (ChannelSelect == 1) {
          if (Ch1V_DAC <= 0) {
            Ch1V_DAC = 0;
          }
          else {
            Ch1V_DAC -= 1;
          }
        }
        if (ChannelSelect == 2) {
          if (Ch1A_DAC <= 0) {
            Ch1A_DAC = 0;
          }
          else {
            Ch1A_DAC -= 1;
          }
        }
        if (ChannelSelect == 3) {
          if (Ch2V_DAC <= 0) {
            Ch2V_DAC = 0;
          }
          else {
            Ch2V_DAC -= 1;
          }
        }
        if (ChannelSelect == 4) {
          if (Ch2A_DAC <= 0) {
            Ch2A_DAC = 0;
          }
          else {
            Ch2A_DAC -= 1;
          }
        }
        updateCh1V();
        updateCh1A();
        updateCh2V();
        updateCh2A();
        delay(100);
      }
    }

  }
}

A a general rule, you want to try and not repeat yourself in your code. Anywhere you 'could' copy/paste your code and only change a variable or two (not literally), you should consider turning it into a function and passing in the variables needed.

If you have lots of variables / functions associated with the same thing, it might be worth considering grouping them together into a class or struct. If even then, you have lots of objects of that class (say here if you had a button class then you'd still have lots of button objects) you can consider maybe using an array of objects.

So I think if you had a button class, instantiated as an array of buttons, and... for example... your drawButtons function took in a button to act on, it would make your code a lot shorter, simpler, and easier to extend/maintain.

For loops will also be a huge help too!

So to draw all your buttons at once, you'd simple write something along the lines of:

for (int i = 0; i < NUM_BTNS; i++)
    drawButtons(allButtons[i]);

And boom, all your fancy-schmancy buttons pop up on the screen! The draw button uses the data stored in each button, so button.x, button.y etc and instead of having 8 (essentially copy/pastes) of the draw code, it only needs one copy to acts on the button that has been passed in.

take a look at the changes in this version that uses a parameter table

many more changes can be made to simplify the code

//
#define MyHW
#ifdef MyHW
enum { ILI9341_BLUE, ILI9341_RED, ILI9341_WHITE };

struct Adafruit_ILI9341  {
    Adafruit_ILI9341 (int cs, int dc)  { };
    void begin (void)  { };
    int height (void)  { return 0; }
    int width  (void)  { return 0; }
    int  setRotation  (int i)  { return 0; };
    void setTextColor (int c)  { };
    void setTextSize  (int c)  { };
    int  setCursor    (int x, int y)  { return 0; };
    void fillScreen   (int c)  { };
    void fillRect    (int x, int y, int h, int w, int i)  { };
    void    println  (int i)  { };
    void    println  (const char *s)  { };
};

struct TSPoint {
    int  x;
    int  y;
    int  z;
};

struct TouchScreen {
    TouchScreen (int x, int y, int w, int h, int n) { };

    void    begin    (void)  { };
    TSPoint getPoint (void)  { TSPoint p; return p; };
};

#else
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <TouchScreen.h>
#endif

#include <SPI.h>
#include <Wire.h>

//Touchscreen X+ X- Y+ Y- pins
#define YP A2  // must be an analog pin, use "An" notation!
#define XM A3  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin
// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
#define MINPRESSURE 10
#define MAXPRESSURE 1000
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#if 0
# define Ch1V_X 10
# define Ch1V_Y 10
# define Ch1A_X 170
# define Ch1A_Y 10
# define Ch2V_X 10
# define Ch2V_Y 90
# define Ch2A_X 170
# define Ch2A_Y 90
#endif

#define WINDOW_W 140
#define WINDOW_H 60
#define ButtonBarLeft_X 10
#define ButtonBarLeft_Y 210
#define ButtonBarRight_X 170
#define ButtonBarRight_Y 210
#define ButtonBar_W 140
#define ButtonBar_H 20
#define Inc_100_Button_X 10
#define Inc_100_Button_Y 170
#define Inc_10_Button_X 60
#define Inc_10_Button_Y 170
#define Inc_1_Button_X 110
#define Inc_1_Button_Y 170
#define Dec_100_Button_X 170
#define Dec_100_Button_Y 170
#define Dec_10_Button_X 220
#define Dec_10_Button_Y 170
#define Dec_1_Button_X 270
#define Dec_1_Button_Y 170
#define Button_W 40
#define Button_H 30
// Store DAC values
#if 0
int Ch1V_DAC = 0;
int Ch1A_DAC = 0;
int Ch2V_DAC = 0;
int Ch2A_DAC = 0;
#endif
byte ChannelSelect = 0; // Contols selected channel on touchscreen

void drawbuttons() {
    tft.fillRect(ButtonBarLeft_X, ButtonBarLeft_Y, ButtonBar_W, ButtonBar_H, ILI9341_WHITE);
    tft.setCursor(ButtonBarLeft_X + 40 , ButtonBarLeft_Y + (ButtonBar_H / 3));
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(1);
    tft.println("Increase");
    tft.fillRect(Inc_100_Button_X, Inc_100_Button_Y, Button_W, Button_H, ILI9341_WHITE);
    tft.setCursor(Inc_100_Button_X + 2 , Inc_100_Button_Y + (Button_H / 3));
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(2);
    tft.println("100");
    tft.fillRect(Inc_10_Button_X, Inc_10_Button_Y, Button_W, Button_H, ILI9341_WHITE);
    tft.setCursor(Inc_10_Button_X + 6 , Inc_10_Button_Y + (Button_H / 3));
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(2);
    tft.println("10");
    tft.fillRect(Inc_1_Button_X, Inc_1_Button_Y, Button_W, Button_H, ILI9341_WHITE);
    tft.setCursor(Inc_1_Button_X + 15 , Inc_1_Button_Y + (Button_H / 3));
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(2);
    tft.println("1");
    tft.fillRect(ButtonBarRight_X, ButtonBarRight_Y, ButtonBar_W, ButtonBar_H, ILI9341_WHITE);
    tft.setCursor(ButtonBarRight_X + 40 , ButtonBarRight_Y + (ButtonBar_H / 3));
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(1);
    tft.println("Decrease");
    tft.fillRect(Dec_100_Button_X, Dec_100_Button_Y, Button_W, Button_H, ILI9341_WHITE);
    tft.setCursor(Dec_100_Button_X + 2 , Dec_100_Button_Y + (Button_H / 3));
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(2);
    tft.println("100");
    tft.fillRect(Dec_10_Button_X, Dec_10_Button_Y, Button_W, Button_H, ILI9341_WHITE);
    tft.setCursor(Dec_10_Button_X + 6 , Dec_10_Button_Y + (Button_H / 3));
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(2);
    tft.println("10");
    tft.fillRect(Dec_1_Button_X, Dec_1_Button_Y, Button_W, Button_H, ILI9341_WHITE);
    tft.setCursor(Dec_1_Button_X + 15 , Dec_1_Button_Y + (Button_H / 3));
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(2);
    tft.println("1");
}

#if 0
void updateCh1V() {
    tft.fillRect(Ch1V_X, Ch1V_Y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
    tft.setCursor(Ch1V_X + 5 , Ch1V_Y + 4);
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(2);
    tft.println("Channel 1 V");
    tft.setCursor(Ch1V_X + 10 , Ch1V_Y + (WINDOW_H / 2));
    if (ChannelSelect == 1) {
        tft.setTextColor(ILI9341_RED);  // Changes colour if this channel is selected
    }
    else {
        tft.setTextColor(ILI9341_BLUE);
    }
    tft.setTextSize(3);
    tft.println(Ch1V_DAC);
}

void updateCh1A() {
    tft.fillRect(Ch1A_X, Ch1A_Y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
    tft.setCursor(Ch1A_X + 5 , Ch1A_Y + 4);
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(2);
    tft.println("Channel 1 A");
    tft.setCursor(Ch1A_X + 10 , Ch1A_Y + (WINDOW_H / 2));
    if (ChannelSelect == 2) {
        tft.setTextColor(ILI9341_RED); // Changes colour if this channel is selected
    }
    else {
        tft.setTextColor(ILI9341_BLUE);
    }
    tft.setTextSize(3);
    tft.println(Ch1A_DAC);
}

void updateCh2V() {
    tft.fillRect(Ch2V_X, Ch2V_Y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
    tft.setCursor(Ch2V_X + 5 , Ch2V_Y + 4);
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(2);
    tft.println("Channel 2 V");
    tft.setCursor(Ch2V_X + 10 , Ch2V_Y + (WINDOW_H / 2));
    if (ChannelSelect == 3) {
        tft.setTextColor(ILI9341_RED);  // Changes colour if this channel is selected
    }
    else {
        tft.setTextColor(ILI9341_BLUE);
    }
    tft.setTextSize(3);
    tft.println(Ch2V_DAC);
}

void updateCh2A() {
    tft.fillRect(Ch2A_X, Ch2A_Y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
    tft.setCursor(Ch2A_X + 5 , Ch2A_Y + 4);
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(2);
    tft.println("Channel 2 A");
    tft.setCursor(Ch2A_X + 10 , Ch2A_Y + (WINDOW_H / 2));
    if (ChannelSelect == 4) {
        tft.setTextColor(ILI9341_RED);  // Changes colour if this channel is selected
    }
    else {
        tft.setTextColor(ILI9341_BLUE);
    }
    tft.setTextSize(3);
    tft.println(Ch2A_DAC);
}

// ---------------------------------------------------------
// demonstrates use of parameter table and single function
#else
struct Desc {
    int     x;
    int     y;
    const char *label;
    int     dac;
};

Desc desc [] = {
    {  10, 10, "Ch1V" },
    { 170, 10, "Ch1A" },
    {  10, 90, "Ch2V" },
    { 170, 90, "Ch2A" },
};

// -------------------------------------
enum { Ch1V, Ch1A, Ch2V, Ch2A, ChLast };

void
update (
    int  idx )
{
    Desc  *d = & desc [idx];

    tft.fillRect      (d->x, d->y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
    tft.setCursor     (d->x + 5 , d->y + 4);
    tft.setTextColor  (ILI9341_BLUE);
    tft.setTextSize  (2);
    tft.println  ("Channel 2 A");
    tft.setCursor    (d->x + 10 , d->y +  (WINDOW_H / 2));
    if  (ChannelSelect == 4) {
        tft.setTextColor  (ILI9341_RED);  // Changes colour if this channel is selected
    }
    else {
        tft.setTextColor  (ILI9341_BLUE);
    }
    tft.setTextSize  (3);
    tft.println  (d->dac);
}

// -------------------------------------
void
updateAll (void)
{
    for (unsigned ch = 0; ch < ChLast; ch++)
        update (ch);
}
#endif

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin(9600);
    tft.begin();
    tft.fillScreen(ILI9341_BLUE);
    // origin = left,top landscape (USB left upper)
    tft.setRotation(1);
#if 0
    updateCh1V();
    updateCh1A();
    updateCh2V();
    updateCh2A();
#else
    updateAll ();
#endif
    drawbuttons();
}

void loop() {
    // Retrieve a point
    TSPoint p = ts.getPoint();
    // See if there's any  touch data for us
    if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
    {
        // Scale using the calibration #'s
        // and rotate coordinate system
        p.x = map(p.x, TS_MINY, TS_MAXY, 0, tft.height());
        p.y = map(p.y, TS_MINX, TS_MAXX, 0, tft.width());
        int y = tft.height() - p.x;
        int x = p.y;


        if ((x > desc [Ch1V].x) && (x < (desc [Ch1V].x + WINDOW_W))) {
            if ((y > desc [Ch1V].y) && (y <= (desc [Ch1V].y + WINDOW_H))) {
                ChannelSelect = 1;
#if 0
                updateCh1V();
                updateCh1A();
                updateCh2V();
                updateCh2A();
#else
                updateAll ();
#endif
            }
        }
        if ((x > desc [Ch1A].x) && (x < (desc [Ch1A].x + WINDOW_W))) {
            if ((y > desc [Ch1A].y) && (y <= (desc [Ch1A].y + WINDOW_H))) {
                ChannelSelect = 2;
#if 0
                updateCh1V();
                updateCh1A();
                updateCh2V();
                updateCh2A();
#else
                updateAll ();
#endif
            }
        }
        if ((x > desc [Ch2V].x) && (x < (desc [Ch2V].x + WINDOW_W))) {
            if ((y > desc [Ch2V].y) && (y <= (desc [Ch2V].y + WINDOW_H))) {
                ChannelSelect = 3;
#if 0
                updateCh1V();
                updateCh1A();
                updateCh2V();
                updateCh2A();
#else
                updateAll ();
#endif
            }
        }
        if ((x > desc [Ch2A].x) && (x < (desc [Ch2A].x + WINDOW_W))) {
            if ((y > desc [Ch2A].y) && (y <= (desc [Ch2A].y + WINDOW_H))) {
                ChannelSelect = 4;
#if 0
                updateCh1V();
                updateCh1A();
                updateCh2V();
                updateCh2A();
#else
                updateAll ();
#endif
            }
        }
        // This button increases the DAC value by 100
        if ((x > Inc_100_Button_X) && (x < (Inc_100_Button_X + Button_W))) {
            if ((y > Inc_100_Button_Y) && (y <= (Inc_100_Button_Y + Button_H))) {
                if (ChannelSelect == 1) {
                    if (desc [Ch1V].dac >= 2048) {
                        desc [Ch1V].dac = 2048;
                    }
                    else {
                        desc [Ch1V].dac += 100;
                    }
                }
                if (ChannelSelect == 2) {
                    if (desc [Ch1A].dac >= 2048) {
                        desc [Ch1A].dac = 2048;
                    }
                    else {
                        desc [Ch1A].dac += 100;
                    }
                }
                if (ChannelSelect == 3) {
                    if (desc [Ch2V].dac >= 2048) {
                        desc [Ch2V].dac = 2048;
                    }
                    else {
                        desc [Ch2V].dac += 100;
                    }
                }
                if (ChannelSelect == 4) {
                    if (desc [Ch2A].dac >= 2048) {
                        desc [Ch2A].dac = 2048;
                    }
                    else {
                        desc [Ch2A].dac += 100;
                    }
                }
#if 0
                updateCh1V();
                updateCh1A();
                updateCh2V();
                updateCh2A();
#else
                updateAll ();
#endif
                delay(100);
            }
        }
        // This button increases the DAC value by 10
        if ((x > Inc_10_Button_X) && (x < (Inc_10_Button_X + Button_W))) {
            if ((y > Inc_10_Button_Y) && (y <= (Inc_10_Button_Y + Button_H))) {
                if (ChannelSelect == 1) {
                    if (desc [Ch1V].dac >= 2048) {
                        desc [Ch1V].dac = 2048;
                    }
                    else {
                        desc [Ch1V].dac += 10;
                    }
                }
                if (ChannelSelect == 2) {
                    if (desc [Ch1A].dac >= 2048) {
                        desc [Ch1A].dac = 2048;
                    }
                    else {
                        desc [Ch1A].dac += 10;
                    }
                }
                if (ChannelSelect == 3) {
                    if (desc [Ch2V].dac >= 2048) {
                        desc [Ch2V].dac = 2048;
                    }
                    else {
                        desc [Ch2V].dac += 10;
                    }
                }
                if (ChannelSelect == 4) {
                    if (desc [Ch2A].dac >= 2048) {
                        desc [Ch2A].dac = 2048;
                    }
                    else {
                        desc [Ch2A].dac += 10;
                    }
                }
#if 0
                updateCh1V();
                updateCh1A();
                updateCh2V();
                updateCh2A();
#else
                updateAll ();
#endif
                delay(100);
            }
        }
        // This button increases the DAC value by 1
        if ((x > Inc_1_Button_X) && (x < (Inc_1_Button_X + Button_W))) {
            if ((y > Inc_1_Button_Y) && (y <= (Inc_1_Button_Y + Button_H))) {
                if (ChannelSelect == 1) {
                    if (desc [Ch1V].dac >= 2048) {
                        desc [Ch1V].dac = 2048;
                    }
                    else {
                        desc [Ch1V].dac += 1;
                    }
                }
                if (ChannelSelect == 2) {
                    if (desc [Ch1A].dac >= 2048) {
                        desc [Ch1A].dac = 2048;
                    }
                    else {
                        desc [Ch1A].dac += 10;
                    }
                }
                if (ChannelSelect == 3) {
                    if (desc [Ch2V].dac >= 2048) {
                        desc [Ch2V].dac = 2048;
                    }
                    else {
                        desc [Ch2V].dac += 1;
                    }
                }
                if (ChannelSelect == 4) {
                    if (desc [Ch2A].dac >= 2048) {
                        desc [Ch2A].dac = 2048;
                    }
                    else {
                        desc [Ch2A].dac += 1;
                    }
                }
#if 0
                updateCh1V();
                updateCh1A();
                updateCh2V();
                updateCh2A();
#else
                updateAll ();
#endif
                delay(100);
            }
        }
        // This button decreaes the DAC value by 100
        if ((x > Dec_100_Button_X) && (x < (Dec_100_Button_X + Button_W))) {
            if ((y > Dec_100_Button_Y) && (y <= (Dec_100_Button_Y + Button_H))) {
                if (ChannelSelect == 1) {
                    if (desc [Ch1V].dac <= 0) {
                        desc [Ch1V].dac = 0;
                    }
                    else {
                        desc [Ch1V].dac -= 100;
                    }
                }
                if (ChannelSelect == 2) {
                    if (desc [Ch1A].dac <= 0) {
                        desc [Ch1A].dac = 0;
                    }
                    else {
                        desc [Ch1A].dac -= 100;
                    }
                }
                if (ChannelSelect == 3) {
                    if (desc [Ch2V].dac <= 0) {
                        desc [Ch2V].dac = 0;
                    }
                    else {
                        desc [Ch2V].dac -= 100;
                    }
                }
                if (ChannelSelect == 4) {
                    if (desc [Ch2A].dac <= 0) {
                        desc [Ch2A].dac = 0;
                    }
                    else {
                        desc [Ch2A].dac -= 100;
                    }
                }
#if 0
                updateCh1V();
                updateCh1A();
                updateCh2V();
                updateCh2A();
#else
                updateAll ();
#endif
                delay(100);
            }
        }
        // This button decreaes the DAC value by 10
        if ((x > Dec_10_Button_X) && (x < (Dec_10_Button_X + Button_W))) {
            if ((y > Dec_10_Button_Y) && (y <= (Dec_10_Button_Y + Button_H))) {
                if (ChannelSelect == 1) {
                    if (desc [Ch1V].dac <= 0) {
                        desc [Ch1V].dac = 0;
                    }
                    else {
                        desc [Ch1V].dac -= 10;
                    }
                }
                if (ChannelSelect == 2) {
                    if (desc [Ch1A].dac <= 0) {
                        desc [Ch1A].dac = 0;
                    }
                    else {
                        desc [Ch1A].dac -= 10;
                    }
                }
                if (ChannelSelect == 3) {
                    if (desc [Ch2V].dac <= 0) {
                        desc [Ch2V].dac = 0;
                    }
                    else {
                        desc [Ch2V].dac -= 10;
                    }
                }
                if (ChannelSelect == 4) {
                    if (desc [Ch2A].dac <= 0) {
                        desc [Ch2A].dac = 0;
                    }
                    else {
                        desc [Ch2A].dac -= 10;
                    }
                }
#if 0
                updateCh1V();
                updateCh1A();
                updateCh2V();
                updateCh2A();
#else
                updateAll ();
#endif
                delay(100);
            }
        }
        // This button decreaes the DAC value by 1
        if ((x > Dec_1_Button_X) && (x < (Dec_1_Button_X + Button_W))) {
            if ((y > Dec_1_Button_Y) && (y <= (Dec_1_Button_Y + Button_H))) {
                if (ChannelSelect == 1) {
                    if (desc [Ch1V].dac <= 0) {
                        desc [Ch1V].dac = 0;
                    }
                    else {
                        desc [Ch1V].dac -= 1;
                    }
                }
                if (ChannelSelect == 2) {
                    if (desc [Ch1A].dac <= 0) {
                        desc [Ch1A].dac = 0;
                    }
                    else {
                        desc [Ch1A].dac -= 1;
                    }
                }
                if (ChannelSelect == 3) {
                    if (desc [Ch2V].dac <= 0) {
                        desc [Ch2V].dac = 0;
                    }
                    else {
                        desc [Ch2V].dac -= 1;
                    }
                }
                if (ChannelSelect == 4) {
                    if (desc [Ch2A].dac <= 0) {
                        desc [Ch2A].dac = 0;
                    }
                    else {
                        desc [Ch2A].dac -= 1;
                    }
                }
#if 0
                updateCh1V();
                updateCh1A();
                updateCh2V();
                updateCh2A();
#else
                updateAll ();
#endif
                delay(100);
            }
        }
    }
}

Thank you I can see my error on both counts here. The DAC is MCP3428 and I will be using 2.048V ref so 0-2047 not 2048. I did re-check and found that I did go over 2048 and below zero due to the ordering.

I also like the idea of the array, I think this is where I am trying to get..

This is great. I am trying to understand it now though; I understand how the Desc struct is formed and that the desc array stores variables for this. I am just trying to work out

this creates a pointer to description (e.g. Ch1V, Ch2A) using an index (i.e. "idx") into the table.

Ah I see. I was just going through the code before and changing the 2048 to 2047 and reordering the addition to occur above the check as Perry suggested

 if (ChannelSelect == 1) {
          Ch1V_DAC += 100;
          if (Ch1V_DAC >= 2047) {
            Ch1V_DAC = 2047;
          }

Then I realised I would have to do this 24 times! which I can see is ridiculous. It feels like I should be able to do this without writing if statements for ChannelSelect each time... also should the DAC values be stored in an Array?

hopefully you noticed how updateAll() simply invokes update() for each table entry.

elsewhere in your code you cut&paste with different parameters. the following could be put into a sub-function that uses the table parameters

the same could be done for the code below

a separate table can specify button parameters

hope you see how your code could be simplified and only additional table entries are needed if the # of channels or buttons increased

i think of this as data driven coding

Have you studied how arrays work? Any variable that you have multiple instances of it like you have you can put into an array then use an index to step through them all. Something like:

const uint8_t numValues = 4;
uint8_t i;
uint16_t someArray[numValues];

for (i = 0; i < numValues; ++i) {
  someArray[i] += 10;
}

Adds 10 to each of the 4 variables stored in the array someArray. Note you only type the adding of 10 once.

I must admit I am have read your code and some extra stuff but I am not quite getting to grips with what is happening here

struct Desc {
    int     x;
    int     y;
    const char *label;
    int     dac;
};

Desc desc [] = {
    {  10, 10, "Ch1V" },
    { 170, 10, "Ch1A" },
    {  10, 90, "Ch2V" },
    { 170, 90, "Ch2A" },
};

// -------------------------------------
enum { Ch1V, Ch1A, Ch2V, Ch2A, ChLast };

void
update (
    int  idx )
{
    Desc  *d = & desc [idx];

    tft.fillRect      (d->x, d->y, WINDOW_W, WINDOW_H, ILI9341_WHITE);
    tft.setCursor     (d->x + 5 , d->y + 4);
    tft.setTextColor  (ILI9341_BLUE);
    tft.setTextSize  (2);
    tft.println  ("Channel 2 A");
    tft.setCursor    (d->x + 10 , d->y +  (WINDOW_H / 2));
    if  (ChannelSelect == 4) {
        tft.setTextColor  (ILI9341_RED);  // Changes colour if this channel is selected
    }
    else {
        tft.setTextColor  (ILI9341_BLUE);
    }
    tft.setTextSize  (3);
    tft.println  (d->dac);
}

// -------------------------------------
void
updateAll (void)
{
    for (unsigned ch = 0; ch < ChLast; ch++)
        update (ch);
}

I'm not sure how it prints the correct label or gets the value for dac in particular.

I have tried to start from scratch and have put this together so far

////////////////////////////////////////////////////////////////////////////
//
// Libraries
//
////////////////////////////////////////////////////////////////////////////
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h>
#include <TouchScreen.h>

////////////////////////////////////////////////////////////////////////////
//
// TFT and Touchscreen definitions
//
////////////////////////////////////////////////////////////////////////////

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

// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

#define MINPRESSURE 10
#define MAXPRESSURE 1000

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);


#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

////////////////////////////////////////////////////////////////////////////
//
// Display window size and location variables
//
////////////////////////////////////////////////////////////////////////////

#define Ch1V_X 10
#define Ch1V_Y 10
#define Ch1A_X 170
#define Ch1A_Y 10
#define Ch2V_X 10
#define Ch2V_Y 90
#define Ch2A_X 170
#define Ch2A_Y 90
#define WINDOW_W 140
#define WINDOW_H 60

////////////////////////////////////////////////////////////////////////////
//
// Main variables
//
////////////////////////////////////////////////////////////////////////////

uint16_t Ch1V_DAC = 0;
uint16_t Ch1A_DAC = 0;
uint16_t Ch2V_DAC = 0;
uint16_t Ch2A_DAC = 0;
int *DAC1;
int *DAC2;
int *DAC3;
int *DAC4;

byte ChannelSelect = 0; // Contols selected channel on touchscreen

////////////////////////////////////////////////////////////////////////////
//
// Button size and location variables
//
////////////////////////////////////////////////////////////////////////////

byte buttonNum = 8;
int button_X[8] = {10, 60, 110, 170, 220, 270, 10, 170};
int button_Y[8] = {170, 170, 170, 170, 170, 170, 210, 210};
uint8_t button_W[8] {40, 40, 40, 40, 40, 40, 140, 140};
#define button_H 30
uint8_t button_cursor_posn[8] = {2, 6, 15, 2, 6, 15, 20, 20};
String button_label[8] = {"100", "10", "1", "100", "10", "1", "increase", "decrease"};

////////////////////////////////////////////////////////////////////////////
//
// Window size and location variables
//
////////////////////////////////////////////////////////////////////////////

byte windowNum = 4;
int window_X[4] = {10, 170, 10, 170};
int window_Y[4] = {10, 10, 90, 90};
uint8_t window_W = 140;
uint8_t window_H = 60;
String window_label[4] = {"Channel 1 V", "Channel 1 A", "Channel 2 V", "Channel 2 A"};
int DAC[4] = {*DAC1, *DAC2, *DAC3, *DAC4};

////////////////////////////////////////////////////////////////////////////
//
// Draw the buttons
//
////////////////////////////////////////////////////////////////////////////

void drawbuttons() {
  for (int i = 0; i < buttonNum; i++) {
    tft.fillRect(button_X[i], button_Y[i], button_W[i], button_H, ILI9341_WHITE);
    tft.setCursor(button_X[i] + button_cursor_posn[i], button_Y[i] + (button_H / 3));
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(2);
    tft.println(button_label[i]);
  }
}

////////////////////////////////////////////////////////////////////////////
//
// Draw and update the windows
//
////////////////////////////////////////////////////////////////////////////

void updateAll() {
  for (int i = 0; i < windowNum; i++) {
    tft.fillRect(window_X[i], window_Y[i], window_W, window_H, ILI9341_WHITE);
    tft.setCursor(window_X[i] + 5 , window_Y[i] + 4);
    tft.setTextColor(ILI9341_BLUE);
    tft.setTextSize(2);
    tft.println(window_label[i]);
    tft.setCursor(window_X[i] + 10 , window_Y[i] + (WINDOW_H / 2));
    tft.setTextSize(3);
    // needs a way of changing text colour if channel is selected
    tft.println(DAC[i]); // needs a pointer
  }
}

void setup() {
  Serial.begin(9600);
  tft.begin();

  tft.fillScreen(ILI9341_BLUE);
  // origin = left,top landscape (USB left upper)
  tft.setRotation(1);

  drawbuttons();

}

void loop() {
  // put your main code here, to run repeatedly:

}

I know the buttons draw correctly, can I use pointers in this way for DAC values though. I'm guessing that in the main loop I would need to do something like this?

DAC1 = &Ch1V_DAC;
DAC2 = &Ch1A_DAC;
DAC3 = &Ch2V_DAC;
DAC4 = &Ch2A_DAC;

you can certainly use separate arrays for each parameters or you can have an array of structures that capture all the parameters. this way all the parameters for a specific thing are on one line which could be easily commented out

field names should be short (e.g. x) improving readability using a short ptr variable (e.g. p->x)

as i've mentioned, it looks like separate structures are needed for the channels and buttons.

consider

////////////////////////////////////////////////////////////////////////////
//
// Libraries
//
////////////////////////////////////////////////////////////////////////////
#define MyHW
#ifdef MyHW
# include "simDav.h"
#else
# include <Adafruit_GFX.h>
# include <Adafruit_ILI9341.h>
# include <TouchScreen.h>
#endif

#include <SPI.h>
#include <Wire.h>
////////////////////////////////////////////////////////////////////////////
//
// TFT and Touchscreen definitions
//
////////////////////////////////////////////////////////////////////////////
//Touchscreen X+ X- Y+ Y- pins
#define YP A2  // must be an analog pin, use "An" notation!
#define XM A3  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin
// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
#define MINPRESSURE 10
#define MAXPRESSURE 1000
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
////////////////////////////////////////////////////////////////////////////
//
// Display window size and location variables
//
////////////////////////////////////////////////////////////////////////////
#define Ch1V_X 10
#define Ch1V_Y 10
#define Ch1A_X 170
#define Ch1A_Y 10
#define Ch2V_X 10
#define Ch2V_Y 90
#define Ch2A_X 170
#define Ch2A_Y 90
#define WINDOW_W 140
#define WINDOW_H 60
////////////////////////////////////////////////////////////////////////////
//
// Main variables
//
////////////////////////////////////////////////////////////////////////////
#if 0
uint16_t Ch1V_DAC = 0;
uint16_t Ch1A_DAC = 0;
uint16_t Ch2V_DAC = 0;
uint16_t Ch2A_DAC = 0;
int *DAC1;
int *DAC2;
int *DAC3;
int *DAC4;
byte ChannelSelect = 0; // Contols selected channel on touchscreen
#endif
////////////////////////////////////////////////////////////////////////////
//
// Button size and location variables
//
////////////////////////////////////////////////////////////////////////////
#if 0
byte buttonNum = 8;
int button_X[8] = {10, 60, 110, 170, 220, 270, 10, 170};
int button_Y[8] = {170, 170, 170, 170, 170, 170, 210, 210};
uint8_t button_W[8] {40, 40, 40, 40, 40, 40, 140, 140};
#define button_H 30
uint8_t button_cursor_posn[8] = {2, 6, 15, 2, 6, 15, 20, 20};
String button_label[8] = {"100", "10", "1", "100", "10", "1", "increase", "decrease"};

#else
const int Button_H = 30;

struct But {
    int     X;
    int     Y;
    int     W;
    int     cursor_posn;
    const char *label;
};

But buts [] = {
    {  10, 170,  40,  2, "100" },
    {  60, 170,  40,  6,  "10" },
    { 110, 170,  40, 15,   "1" },
    { 170, 170,  40,  2, "100" },
    { 220, 170,  40,  6,  "10" },

    { 270, 170,  40, 15,   "1" },
    {  10, 210, 140, 20, "increase" },
    { 170, 210, 140, 20, "decrease" },
};
int buttonNum = sizeof(buts)/sizeof(But);
#endif

////////////////////////////////////////////////////////////////////////////
//
// Window size and location variables
//
////////////////////////////////////////////////////////////////////////////
#if 0
byte windowNum = 4;
int window_X[4] = {10, 170, 10, 170};
int window_Y[4] = {10, 10, 90, 90};
uint8_t window_W = 140;
uint8_t window_H = 60;
String window_label[4] = {"Channel 1 V", "Channel 1 A", "Channel 2 V", "Channel 2 A"};

#else
const int Window_W = 140;
const int Window_H = 60;

struct Window {
    int         x;
    int         y;
    const char *label;

    uint16_t  dac;
};

Window windows [] = {
    {  10,  10, "Channel 1 V" },
    { 170,  10, "Channel 1 A" },
    {  10,  90, "Channel 2 V" },
    { 170,  90, "Channel 2 A" },
};
int windowNum = sizeof(windows)/sizeof(Window);

#endif

////////////////////////////////////////////////////////////////////////////
//
// Draw the buttons
//
////////////////////////////////////////////////////////////////////////////
void drawbuttons()
{
    But *p = buts;
    for (int i = 0; i < buttonNum; i++, p++) {
        tft.fillRect     (p->X, p->Y, Window_W, Button_H, ILI9341_WHITE);
        tft.setCursor    (p->X + p->cursor_posn, p->Y + (Button_H / 3));
        tft.setTextColor (ILI9341_BLUE);
        tft.setTextSize  (2);
        tft.println      (p->label);
    }
}

////////////////////////////////////////////////////////////////////////////
//
// Draw and update the windows
//
////////////////////////////////////////////////////////////////////////////
void updateAll()
{
    Window *p = windows;

    for (int i = 0; i < windowNum; i++) {
        tft.fillRect     (p->x, p->y, Window_W, Window_H, ILI9341_WHITE);
        tft.setCursor    (p->x + 5 , p->y + 4);
        tft.setTextColor (ILI9341_BLUE);
        tft.setTextSize  (2);
        tft.println      (p->label);
        tft.setCursor    (p->x + 10 , p->y + (WINDOW_H / 2));
        tft.setTextSize  (3);

        // needs a way of changing text colour if channel is selected
        tft.println      (p->dac); // needs a pointer
    }
}

void setup() {
    Serial.begin(9600);
    tft.begin();
    tft.fillScreen(ILI9341_BLUE);
    // origin = left,top landscape (USB left upper)
    tft.setRotation(1);
    drawbuttons();
}

void loop() {
    // put your main code here, to run repeatedly:
}
  • it's common practice to only Capitalize constants.
  • i aligned the function argument lists for readability
  • i define labels as c-strings instead String
  • i made the dac value a field of window, but not sure this is best. seems like dac should be in a channel structure and as you've hinted, the window struct could have a ptr to a dac value or the channel struct

Awesome, I understand now; as will be shown by my 'basic' comments to myself when I repost

I have made this change, but why are c-strings better?

how would I have a ptr to the channel struct?

more standard since Arduino String is not the same as C++ string

similar to

Can I update the values inside the struct; i.e. when I add 10 or 100 to the dac value. I guess I'm trying to ascertain if I need to update a global variable and just use the pointer to that either inside the struct or as an external array?

I am now also looking at how to evaluate the touchscreen presses, is there anyway to do this other than using 'if' for every single instance?

I think I have figured part out.

if ((x > windows [0].x) && (x < (windows [0].x + Window_W))) {
            if ((y > windows [0].y) && (y <= (windows [0].y + Window_H))) {
                windows [0].dac += 10;
                updateAll();
      }
    }