This sketch works properly, as it does everything I expected, but I am unhappy with the performance. The buttons are too quick to respond. I think I need some kind of “de-bouncing” to make the buttons less responsive.
[code] /*
/-----( Import needed libraries )-----/
#include <stdint.h>
#include <TouchScreen.h>
#include <TFTv2.h>
#include <SPI.h>
#include <Wire.h> // specify use of Wire.h library.
/-----( Declare objects )-----/
/* create variable to store High Level Sensor status */
boolean LED_1_State = LOW;//storage for the current state of the LED (off/on)
boolean LED_2_State = LOW;//storage for the current state of the LED (off/on)
boolean LED_3_State = LOW;//storage for the current state of the LED (off/on)
boolean LED_4_State = LOW;//storage for the current state of the LED (off/on)
boolean LED_5_State = LOW;//storage for the current state of the LED (off/on)
boolean LED_6_State = LOW;//storage for the current state of the LED (off/on)
/-----( setup temp/humidity sensor )-----/
#define RELAY_ON 0 /* compensate for relay backwardness*/
#define RELAY_OFF 1 /* compensate for relay backwardness*/
//#def MEGA
#define YP A2 /* must be an analog pin, use “An” notation!*/
#define XM A1 // must be an analog pin, use “An” notation!
#define YM 54 // can be a digital pin, this is A0
#define XP 57 // can be a digital pin, this is A3
#define TS_MINX 140
#define TS_MAXX 900
#define TS_MINY 120
#define TS_MAXY 940
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// The 2.8" TFT Touch shield has 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
/----( SETUP: RUNS ONCE )----/
void setup()
{
Tft.TFTinit();
//Tft.setDisplayDirect(DOWN2UP);
// Tft.setDisplayDirect(UP2DOWN);
Tft.setDisplayDirect(LEFT2RIGHT);
//-------( Initialize Pins so relays are inactive at reset)----/
digitalWrite(32, RELAY_OFF);
digitalWrite(34, RELAY_OFF);
digitalWrite(36, RELAY_OFF);
digitalWrite(38, RELAY_OFF);
digitalWrite(40, RELAY_OFF);
digitalWrite(42, RELAY_OFF);
digitalWrite(44, RELAY_OFF);
digitalWrite(46, RELAY_OFF);
//—( THEN set pins as outputs )----/
pinMode(32, OUTPUT);
pinMode(34, OUTPUT);
pinMode(36, OUTPUT);
pinMode(38, OUTPUT);
pinMode(40, OUTPUT);
pinMode(42, OUTPUT);
pinMode(44, OUTPUT);
pinMode(46, OUTPUT);
delay(400); /—( Check that all relays are inactive at Rese)----/
Serial.begin(57600); /—( )----/
Draw_Button_Frames();
Fill_Buttons();
}/–(end setup )—/
void loop() /----( LOOP: RUNS CONSTANTLY )----/
{
Read_TS();
}
//////////////////////////////////////////////////
void Read_TS(){
// a point object holds x y and z coordinates
Point p = ts.getPoint();
if (p.z > ts.pressureThreshhold) {
}
p.x = map(p.x, TS_MINX, TS_MAXX, 240, 0);
p.y = map(p.y, TS_MINY, TS_MAXY, 320, 0);
if (p.z > ts.pressureThreshhold) {
Serial.print("X = “);
Serial.println(p.x);
Serial.print(”\tY = ");
Serial.println(p.y);
}
///////////////////////////////////
if (p.y > 1 && p.y < 45 && p.x > 200 && p.x < 220) {
//toggle the state of the LED
if (LED_1_State == HIGH){
digitalWrite(32, RELAY_OFF);
Tft.fillCircle(219, 30, 16,RED);
Serial.print("LED_1_State = ");
Serial.println(LED_1_State);
LED_1_State = LOW;
}
else {
digitalWrite(32, RELAY_ON);
Tft.fillCircle(219, 30, 16,GREEN);
Serial.print("LED_1_State = ");
Serial.println(LED_1_State);
LED_1_State = HIGH;
}
Serial.println("Button 1 Pressed");
}
///////////////////////////////////////////
if (p.y > 49 && p.y < 79 && p.x > 200 && p.x < 220) {
//toggle the state of the LED
if (LED_2_State == HIGH){
digitalWrite(34, RELAY_ON);
Tft.fillCircle(219, 75, 16,GREEN);
LED_2_State = LOW;
}
else {
Tft.fillCircle(219, 75, 16,RED);
LED_2_State = HIGH;
digitalWrite(34, RELAY_OFF);
}
Serial.println("Button 2 Pressed");
}
///////////////////////////////////////////
if (p.y > 94 && p.y < 130 && p.x > 200 && p.x < 220){
//toggle the state of the LED
if (LED_3_State == HIGH){
digitalWrite(36, RELAY_ON);
Tft.fillCircle(219, 120, 16,GREEN);
LED_3_State = LOW;
}
else {
Tft.fillCircle(219, 120, 16,RED);
LED_3_State = HIGH;
digitalWrite(36, RELAY_OFF);
}
Serial.println("Button 3 Pressed");
}
///////////////////////////////////////////
if (p.y > 150 && p.y < 176 && p.x > 200 && p.x < 220){
//toggle the state of the LED
if (LED_4_State == HIGH){
digitalWrite(38, RELAY_ON);
Tft.fillCircle(219, 165, 16,GREEN);
LED_4_State = LOW;
}
else {
Tft.fillCircle(219, 165, 16,RED);
LED_4_State = HIGH;
digitalWrite(38, RELAY_OFF);
}
Serial.println("Button 4 Pressed");
}
///////////////////////////////////////////
if (p.y > 190 && p.y < 220 && p.x > 205 && p.x < 240){
//toggle the state of the LED
if (LED_5_State == HIGH){
digitalWrite(40, RELAY_ON);
digitalWrite(42, RELAY_ON);
Tft.fillCircle(219, 210, 16,GREEN);
LED_5_State = LOW;
}
else {
Tft.fillCircle(219, 210, 16,RED);
LED_5_State = HIGH;
digitalWrite(40, RELAY_OFF);
digitalWrite(42, RELAY_OFF);
}
Serial.println("Button 5 Pressed");
}
if (p.y > 240 && p.y < 270 && p.x > 205 && p.x < 240){
if (LED_6_State == HIGH){
digitalWrite(44, RELAY_ON);
Tft.fillCircle(219, 255, 16,GREEN);
LED_6_State = LOW;
}
else {
Tft.fillCircle(219, 255, 16,RED);
LED_6_State = HIGH;
digitalWrite(44, RELAY_OFF);
}
//Serial.println(“Button 6 Pressed”);
}
if (p.y > 280 && p.y < 325 && p.x > 5 && p.x < 225){
digitalWrite(32, RELAY_OFF);
digitalWrite(34, RELAY_OFF);
digitalWrite(36, RELAY_OFF);
digitalWrite(38, RELAY_OFF);
digitalWrite(40, RELAY_OFF);
digitalWrite(42, RELAY_OFF);
digitalWrite(44, RELAY_OFF);
digitalWrite(46, RELAY_ON);
Tft.fillCircle(219, 30, 16,RED);
Tft.fillCircle(219, 75, 16,RED);
Tft.fillCircle(219, 120, 16,RED);
Tft.fillCircle(219, 165, 16,RED);
Tft.fillCircle(219, 210, 16,RED);
Tft.fillCircle(219, 255, 16,RED);
Serial.println("Button 7 Pressed");
}
}
///////////////////////////////////////////////////////
void Draw_Button_Frames(){
/—( Draw Button Frames )----/
/—( Button 1 )----/
Tft.drawCircle(219, 30, 17,BLUE);
Tft.drawCircle(219, 30, 18,BLUE);
Tft.drawCircle(219, 30, 19,BLUE);
Tft.drawCircle(219, 30, 20,BLUE);
/—( Button 2 )----/
Tft.drawCircle(219, 75, 17,BLUE);
Tft.drawCircle(219, 75, 18,BLUE);
Tft.drawCircle(219, 75, 19,BLUE);
Tft.drawCircle(219, 75, 20,BLUE);
/—( Button 3 )----/
Tft.drawCircle(219, 120, 17,BLUE);
Tft.drawCircle(219, 120, 18,BLUE);
Tft.drawCircle(219, 120, 19,BLUE);
Tft.drawCircle(219, 120, 20,BLUE);
/—( Button 4 )----/
Tft.drawCircle(219, 165, 17,BLUE);
Tft.drawCircle(219, 165, 18,BLUE);
Tft.drawCircle(219, 165, 19,BLUE);
Tft.drawCircle(219, 165, 20,BLUE);
/—( Button 5 )----/
Tft.drawCircle(219, 210, 17,BLUE);
Tft.drawCircle(219, 210, 18,BLUE);
Tft.drawCircle(219, 210, 19,BLUE);
Tft.drawCircle(219, 210, 20,BLUE);
/—( Button 6 )----/
Tft.drawCircle(219, 255, 17,BLUE);
Tft.drawCircle(219, 255, 18,BLUE);
Tft.drawCircle(219, 255, 19,BLUE);
Tft.drawCircle(219, 255, 20,BLUE);
}
/////////////////////////////////////////////////////////////////////
void Fill_Buttons(){
Tft.fillCircle(219, 30, 16,RED);
Tft.fillCircle(219, 75, 16,RED);
Tft.fillCircle(219, 120, 16,RED);
Tft.fillCircle(219, 165, 16,RED);
Tft.fillCircle(219, 210, 16,RED);
Tft.fillCircle(219, 255, 16,RED);
}[/code]