I have a TFT and a Temp sensor that I'm putting into my sketch, I have separate sketches for each component and they work as coded.
I have married the two sketches together into one sketch and when I uploaded it to my Arduino the touch events with the TFT would not register to flip the on screen buttons from red to green.
I added a serial.print underneath the touchRead(&tx, &ty); (in the Void GFXbuttonswitch) and it now works as coded.
Why would that make a difference?
I'm trying to get a better understanding what's going when I write code.
#include <Wire.h>
#include <SparkFun_TMP117.h>
TMP117 sensor; //inialize sensor (I2C 0x48)
byte AlertFlag = 0; //variable to hold high and low alert flags from configuration register
boolean H_AlertFlag = 0; //variable to hold state of high alert flag
boolean L_AlertFlag = 0; //variable to hold state of low alert flag
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1500;
unsigned long startMillis2;
unsigned long currentMillis2;
const unsigned long period2 = 500;
const int fanControl = 13;
/////////////////////////////////////////////////////////////////
//TFT SCREEN GLOBALS
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h"
#define RA8875_CS 10
#define RA8875_RESET 9
Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);
uint16_t tx, ty;
byte rotation = 0;
int x, y;
const byte row = 4;
const byte col = 3;
byte lastHit = 0;
byte currentHit = 0;
Adafruit_GFX_Button btn[row * col];
const int TS_MAXX = 980, TS_MINX = 60, TS_MAXY = 920, TS_MINY = 80;
////////////////////////////////////////////////////////////////////////////
//FOOT SWITCH GLOBALS
const int footswitch = 2;
int footswitchState = 0;
////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
pinMode(fanControl, OUTPUT); //Fan cooling control
digitalWrite(fanControl, HIGH); // turn fans off on startup(logic reversed from Mosfet)
pinMode(footswitch, INPUT), //program start switch
pinMode(18, INPUT_PULLUP); //front proximity sensor(stapler in position)
pinMode(19, INPUT_PULLUP); //back proximity sensor (stapler in start position)
Wire.begin();
Wire.setClock(400000); // Set clock speed to be the fastest for better communication (fast mode)
if (sensor.begin() == true) // Function to check if the sensor will correctly self-identify with the proper Device ID/Address
{
Serial.println("Begin");
}
else
{
Serial.println("Device failed to setup- Freezing code.");
while (1);
}
sensor.setHighLimit(35); //set high limit
sensor.setLowLimit(60); //set low limit
sensor.setAlertFunctionMode(0);//set to alert mode
if (!tft.begin(RA8875_800x480)) {
Serial.println("RA8875 Not Found!");
while (1);
}
Serial.println("Found RA8875");
tft.displayOn(true);
tft.GPIOX(true); // Enable TFT - display enable tied to GPIOX
tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
tft.PWM1out(255);
tft.fillScreen(RA8875_BLACK);
tft.textMode();
tft.textSetCursor(20, 15);
tft.textEnlarge(2);
tft.textColor(RA8875_WHITE, RA8875_RED);
tft.textWrite("Choose how many petals you need.");
tft.graphicsMode();
tft.touchEnable(true);
btnGrid();
}
void loop() {
GFXbuttonswitch();
EnclosureCooling();
}
void EnclosureCooling() {
if (sensor.dataReady() == true) // Function to make sure that there is data ready to be printed, only prints temperature values when data is ready
unsigned long currentMillis = millis();
{
currentMillis = millis();
if (currentMillis - startMillis >= period)
startMillis = currentMillis;
AlertFlag = sensor.getHighLowAlert(); //read the alert flags from the configuration register
H_AlertFlag = bitRead(AlertFlag, 1); //grab the high alert field using bitwise operator and save current to H_AlertFlag
L_AlertFlag = bitRead(AlertFlag, 0); //grab the low alert field using bitwise operator and save current L_AlertFlag
if (H_AlertFlag == true)
{
digitalWrite(13, LOW); //Logic is reversed from Mosfet
}
else if (L_AlertFlag == true)
{
digitalWrite(13, HIGH); //logic is reversed Mosfet
}
unsigned long currentMillis2 = millis();
{
currentMillis2 = millis();
if (currentMillis2 - startMillis2 >= period2)
startMillis2 = currentMillis2;
}
}
}
void GFXbuttonswitch() {
if (tft.touched()) {
tft.touchRead(&tx, &ty);
Serial.print(tx); Serial.print(", "); Serial.println(ty);
switch (rotation) {
case 0:
tx = map(tx, TS_MINX, TS_MAXX, 0, tft.width());
ty = map(ty, TS_MINY, TS_MAXY, 0, tft.height());
break;
case 1:
// p.x, p.y reversed //
tx = map(ty, TS_MINY, TS_MAXY, 0, tft.width());
ty = map(tx, TS_MAXX, TS_MINX, 0, tft.height());
break;
case 2:
tx = map(tx, TS_MAXX, TS_MINX, 0, tft.width());
ty = map(ty, TS_MAXY, TS_MINY, 0, tft.height());
break;
case 3:
// p.x, p.y reversed //
tx = map(ty, TS_MAXY, TS_MINY, 0, tft.width());
ty = map(tx, TS_MINX, TS_MAXX, 0, tft.height());
break;
}
}
if (tft.touched()) {
for (uint8_t b = 0; b < row * col; b++) {
if (btn[b].contains(tx, ty)) {
btn[b].press(true);
btn[b].drawButton(true);
currentHit = b;
}
else if (btn[b].contains(tx, ty) == false) {
btn[b].press(false);
if (b == lastHit) {
btn[b].drawButton(false);
}
} else {
return;
}
}
lastHit = currentHit;
//delay(100);
}
}
void btnGrid() {
int left, top;
int l = 50;
int t = 90;
int w = 200;
int h = 60;
byte hgap = 40;
byte vgap = 40;
byte id = 0;
char *titleStr[row * col] = {"4", "6", "8", "9", "12", "16", "Load", "1"};
for (byte j = 0; j < row; j++) {
for (byte i = 0; i < col; i++) {
left = l + i * (w + vgap);
top = t + j * (h + hgap);
btn[id].initButtonUL( &tft, left, top, w, h, RA8875_WHITE, RA8875_RED, RA8875_GREEN, titleStr[id], 3 );
if (id == currentHit) {
// inverted
btn[id].drawButton(true);
} else {
btn[id].drawButton(false);
}
id++;
}
}
}