Touch Buttons are not very responsive - Is it my code?

So i am using an arduino clone and 2.8" tft lcd screen for my project. With the massive help I recieved from David Prentice I was able to get the screen to work and it displays my code as expected.

The only problem i have now is that when pressing the buttons it can take anywhere from 3 to 13 seconds for the buttons to work.

I have run many of the examples in the MCUFRIEND-kbv and using Touch_Shield_new i can see that the screen is responsive to my moving the stylus giving me the x and y coordinates instantly, therefore i suspect the issue lies with my code.

This is for a solder reflow plate if that helps any......

anyway the code if someone could kindly look and help is

#include <TouchScreen.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <max6675.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "icon.h"

// Pinouts
#define fan        1
#define relay      0
#define buzzer     11

#define thermoCS   10
#define thermoDO   12
#define thermoCLK  13

#define oneWireBus A5

// Variables
int temp = 0;
int temp1 = 0;
unsigned int preHeat = 120;
unsigned int reflow = 180;
unsigned int coolDown = 50;
unsigned int timer = 60;
unsigned int mm = 88;
unsigned int ss = 88;
bool reflowing;
bool cooling;
bool fanState;
int saveTimer;
unsigned long previousMillis = 0;
int x = 165, y = 231, w = 36, h = 36;


MCUFRIEND_kbv tft;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);

// require touch screen calibration
// Put your calibrated touchSensor values here
const int XP=7,XM=A1,YP=A2,YM=6; //240x320 ID=0x9338
const int TS_LEFT=917,TS_RT=152,TS_TOP=138,TS_BOT=919;

#define MINPRESSURE 200
#define MAXPRESSURE 1000

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

// buttons
Adafruit_GFX_Button on_btn, off_btn, up1, dwn1, up2, dwn2, up3, dwn3, up4, dwn4, fanBtn;

// get touch input
int pixel_x, pixel_y;
bool Touch_getXY(void)
{
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);      //restore shared pins
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);   //because TFT control pins
  digitalWrite(XM, HIGH);
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  if (pressed) {
    pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width());
    pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
  }
  return pressed;
}

#define BLACK   0x0000
#define WHITE   0xFFFF
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0

void setup(void)
{
  //Serial.begin(9600);   // Turn off serial communication while using serial ports
  pinMode(buzzer, OUTPUT);
  pinMode(relay, OUTPUT);
  pinMode(fan, OUTPUT);
  digitalWrite(buzzer, LOW);
  digitalWrite(relay, LOW);
  digitalWrite(fan, LOW);
  //sensors.begin();
  tft.begin(0x9338);
  tft.setRotation(0);
  tft.fillScreen(BLACK);
  tft.setTextColor(BLACK, YELLOW);
  tft.setTextSize(2);
  tft.setCursor(45, 5);
  tft.print(" REFLOW IRON ");
  tft.fillRect(45, 0, 156, 5, YELLOW);
  tft.fillRect(45, 20, 156, 5, YELLOW);

  tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
  tft.pushColors((const uint8_t*)fan1, w * h, 1, false);
  tft.setAddrWindow(0, 0, tft.width() - 1, tft.height() - 1);
  fanBtn.initButton(&tft,  220, 250, 18, 24, WHITE, BLACK, WHITE, "F", 2);
  fanBtn.drawButton(false);

  // initialise buttons
  up1.initButton(&tft,  195, 108, 20, 20, WHITE, BLACK, WHITE, "+", 2);
  dwn1.initButton(&tft,  220, 108, 20, 20, WHITE, BLACK, WHITE, "-", 2);
  up1.drawButton(false);
  dwn1.drawButton(false);

  up2.initButton(&tft,  195, 143, 20, 20, WHITE, BLACK, WHITE, "+", 2);
  dwn2.initButton(&tft,  220, 143, 20, 20, WHITE, BLACK, WHITE, "-", 2);
  up2.drawButton(false);
  dwn2.drawButton(false);

  up3.initButton(&tft,  195, 178, 20, 20, WHITE, BLACK, WHITE, "+", 2);
  dwn3.initButton(&tft,  220, 178, 20, 20, WHITE, BLACK, WHITE, "-", 2);
  up3.drawButton(false);
  dwn3.drawButton(false);

  up4.initButton(&tft,  195, 213, 20, 20, WHITE, BLACK, WHITE, "+", 2);
  dwn4.initButton(&tft,  220, 213, 20, 20, WHITE, BLACK, WHITE, "-", 2);
  up4.drawButton(false);
  dwn4.drawButton(false);

  on_btn.initButton(&tft,  60, 295, 100, 40, WHITE, 0x068B, WHITE, "START", 2);
  off_btn.initButton(&tft, 180, 295, 100, 40, WHITE, 0xF340, WHITE, "RESET", 2);
  on_btn.drawButton(false);
  off_btn.drawButton(false);
}

void loop(void)
{
  if (millis() - previousMillis >= 1000) {
    previousMillis = millis();

    temp = thermocouple.readCelsius();      // get temperature from thermocouple
    //sensors.requestTemperatures();
    //temp1 = sensors.getTempCByIndex(0);   // get ambient temperature

    if (reflowing) {
      // pre heating stage
      if (temp < preHeat)
        tft.drawRect(0, 90, 240, 35, RED);

      // reflow stage
      if (temp > preHeat) {
        tft.drawRect(0, 90, 240, 35, BLACK);
        tft.drawRect(0, 125, 240, 35, GREEN);

        // turn off the heating element if temp exceeds set reflow temp
        if (temp > reflow) {
          digitalWrite(relay, LOW);
        }
        // turn it back on otherwise
        if (temp < reflow) {
          digitalWrite(relay, HIGH);
        }
        // countdown timer
        timer--;
        // timer finished
        if (timer == 0) {
          reflowing = false;        // turn off reflow
          cooling = true;           // turn on cooling
          digitalWrite(relay, LOW); // turn off relay
          digitalWrite(buzzer, HIGH); // turn off buzzer
          delay(500);
          digitalWrite(buzzer, LOW); // turn off buzzer
          tft.drawRect(0, 125, 240, 35, BLACK);
        }
        mm = timer / 60;
        ss = timer % 60;
        tft.fillRect(130, 203, 50, 20, BLACK);
        tft.fillRect(30, 233, 120, 40, BLACK);
      }
    }
    tft.fillRoundRect(115, 40, 115, 41, 15, RED);
  }

  if (cooling) {
    tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
    tft.pushColors((const uint8_t*)fan1, w * h, 1, false);
    tft.setAddrWindow(0, 0, tft.width() - 1, tft.height() - 1);
    delay(50);
    tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
    tft.pushColors((const uint8_t*)fan2, w * h, 1, false);
    tft.setAddrWindow(0, 0, tft.width() - 1, tft.height() - 1);
    delay(50);
    if (temp > coolDown) {
      digitalWrite(fan, HIGH);
      tft.drawRect(0, 160, 240, 35, BLUE);
    }
    if (temp < coolDown) {
      cooling = false;
      timer = saveTimer;
      digitalWrite(fan, LOW);
      tft.drawRect(0, 160, 240, 35, BLACK);
      tft.fillRect(130, 203, 50, 20, BLACK);
    }
  }

  bool down = Touch_getXY();

  // Start | Reset
  on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
  off_btn.press(down && off_btn.contains(pixel_x, pixel_y));

  if (on_btn.justPressed())  on_btn.drawButton(true);
  if (off_btn.justPressed()) off_btn.drawButton(true);
  if (on_btn.justReleased()) {
    on_btn.drawButton();
    if (timer > 30) {
      saveTimer = timer;
      reflowing = true;
      mm = timer / 60;
      ss = timer % 60;
      tft.fillRect(130, 203, 50, 20, BLACK);
      tft.fillRect(30, 233, 120, 40, BLACK);
      digitalWrite(relay, HIGH);
    }
  }
  if (off_btn.justReleased()) {
    off_btn.drawButton();
    asm volatile ("jmp 0");   // reset
  }
  // PreHeat
  up1.press(down && up1.contains(pixel_x, pixel_y));
  dwn1.press(down && dwn1.contains(pixel_x, pixel_y));

  if (up1.justPressed())  up1.drawButton(true);
  if (dwn1.justPressed()) dwn1.drawButton(true);
  if (up1.justReleased()) {
    up1.drawButton();
    tft.fillRect(130, 98, 50, 20, BLACK);
    preHeat++;
  }
  if (dwn1.justReleased()) {
    dwn1.drawButton();
    tft.fillRect(130, 98, 50, 20, BLACK);
    preHeat--;
  }
  // Reflow
  up2.press(down && up2.contains(pixel_x, pixel_y));
  dwn2.press(down && dwn2.contains(pixel_x, pixel_y));

  if (up2.justPressed())  up2.drawButton(true);
  if (dwn2.justPressed()) dwn2.drawButton(true);
  if (up2.justReleased()) {
    up2.drawButton();
    tft.fillRect(130, 133, 50, 20, BLACK);
    reflow++;
  }
  if (dwn2.justReleased()) {
    dwn2.drawButton();
    tft.fillRect(130, 133, 50, 20, BLACK);
    reflow--;
  }
  // cooldown
  up3.press(down && up3.contains(pixel_x, pixel_y));
  dwn3.press(down && dwn3.contains(pixel_x, pixel_y));

  if (up3.justPressed())  up3.drawButton(true);
  if (dwn3.justPressed()) dwn3.drawButton(true);
  if (up3.justReleased()) {
    up3.drawButton();
    tft.fillRect(130, 168, 50, 20, BLACK);
    coolDown++;
  }
  if (dwn3.justReleased()) {
    dwn3.drawButton();
    tft.fillRect(130, 168, 50, 20, BLACK);
    coolDown--;
  }
  // set timer
  up4.press(down && up4.contains(pixel_x, pixel_y));
  dwn4.press(down && dwn4.contains(pixel_x, pixel_y));

  if (up4.justPressed())  up4.drawButton(true);
  if (dwn4.justPressed()) dwn4.drawButton(true);
  if (up4.justReleased()) {
    up4.drawButton();
    tft.fillRect(130, 203, 50, 20, BLACK);
    timer += 5;
  }
  if (dwn4.justReleased()) {
    dwn4.drawButton();
    tft.fillRect(130, 203, 50, 20, BLACK);
    timer -= 5;
  }
  // fan
  fanBtn.press(down && fanBtn.contains(pixel_x, pixel_y));

  if (fanBtn.justPressed())  fanBtn.drawButton(true);
  if (fanBtn.justReleased()) {
    fanBtn.drawButton();
    fanState = !fanState;
  }

  if (fanState) {
    digitalWrite(fan, HIGH);
    tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
    tft.pushColors((const uint8_t*)fan1, w * h, 1, false);
    tft.setAddrWindow(0, 0, tft.width() - 1, tft.height() - 1);
    delay(50);
    tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
    tft.pushColors((const uint8_t*)fan2, w * h, 1, false);
    tft.setAddrWindow(0, 0, tft.width() - 1, tft.height() - 1);
    delay(50);
  }
  else {
    digitalWrite(fan, LOW);
  }

  tft.setTextSize(3);
  tft.setTextColor(RED);
  tft.setCursor(5, 50);
  tft.print("Temp : ");
  tft.setTextColor(WHITE);
  tft.print(temp);
  tft.print((char)247);
  tft.print("C");
  tft.setTextSize(2);
  tft.setTextColor(WHITE);
  tft.setCursor(10, 100);
  tft.print("Pre Heat : ");
  tft.print(preHeat);
  tft.setTextColor(WHITE);
  tft.setCursor(10, 135);
  tft.print("Reflow   : ");
  tft.print(reflow);
  tft.setTextColor(WHITE);
  tft.setCursor(10, 170);
  tft.print("Cooldown : ");
  tft.print(coolDown);
  tft.setTextColor(WHITE);
  tft.setCursor(10, 205);
  tft.print("Timer(s) : ");
  tft.print(timer);

  tft.setTextSize(4);
  tft.setTextColor(WHITE);
  tft.setCursor(30, 235);
  tft.print(mm);
  tft.print(":");
  if (ss < 10) tft.print(0);
  tft.print(ss);
  tft.setTextSize(2);
}

Thanks in advance for any help you can offer

Geo.

    timer -= 5;

Out the door just now. Are you sure about all the timer maths?

In particular, can it ever go negative with a line like the above?

Some printing statements might help you trace the flow and catch things like this.

L8R

a7

1 Like

Thanks for the response.

I am very new to coding but from what i can see

 timer -= 5;

simply reduces the timer time by increments of 5 when the - button is pressed.

What if timer was less than 5 when you did that?

a7

Right!

unsigned int timer = 60;

@geologeek didn't tell us what type of board is being used. But, assuming AVR-based, then:
4 - 5 = 65535.

If using a 32-bit ARM or ESP:
4 - 5 = 4294967295

1 Like

so if i change this to say 1 - then the timer - button will drop the timer down by increments of 1

and if i change it to 4 for example the increments will go down by 4 when the - button is pressed

Sorry i should have said it was an arduino uno clone.......my mistake sorry!

So do you understand this?

I am afraid i do not.........

I will have a read about unassigned intergers and see if i can catch up with you.....

right i am starting to understand........if i press my button until it goes past 0 it then jumps to 65535.

definately something i need to learn and fix.

It would be good to figure out where your code is spending it's time.

We don't have your screen and hardware setup so it's hard to tell what is going on.

For responsiveness, I'd start by using something like Serial.print("c") or Serial.print("f") near the delays in is(cooling){...} or if (fanState) { to see if being trapped in those delays is interfering with the buttons.

If unsigned int timer goes below zero and rolls over to 65535, then the unsigned int mm should manifest as 65535/60 = 1092 or so. Is that happening?

Since timer is an important state variable, I'd Serial.print() that often. I'd try once per loop, to get an indication if your loop is running fast, and if so, I might move it into the millis() protected code so it's lest distracting. If your loop() is running slow, then since it's only checking for the button once per loop, you need to fix that.

DaveX

thanks ever so much for your input i will have a look at your suggestions and see as you suggest where the code is spending time or getting hung up.

Again thanks to you all.

Geo.

Yes it does indeed show 1090:00 if i roll the timer beyond 0

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.