//Touch sreen attempt for door control 8th Feb 2022
#if 1
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(52,53); // TX, RX
#define MINPRESSURE 200
#define MAXPRESSURE 1000
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x8230
const int TS_LEFT=920,TS_RT=94,TS_TOP=69,TS_BOT=896;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button Aon_btn, Aoff_btn; //Aon = TX A, Aoff = STOP A
Adafruit_GFX_Button Bon_btn, Boff_btn; //Bon = TX B, Boff = STOP B
Adafruit_GFX_Button Con_btn, Coff_btn;
//Global variables
int Reset = 47; // Reset function to reset TFT display
const int wakePB = 48; // This is wakeup Push Button at TFT LCD display
int wake = 49; // This picks up relay to power on TFT LCD display
int timeup1 = 0; //Variable for off delay picked up
int doorStateOpen; //Variable for current door position - reading
int doorStateClosed; //Variable for current door position - reading
int lastdoorStateOpen = HIGH; //Variable for last know button reading
int doorStateA; //Variable to hold doorA position from serial read
int doorStateB; //Variable to hold doorB position from serial read
int timerStart = LOW;
int buttonStateZ; //Variable for current 'RESET' button reading
int lastButtonStateZ; //Variable for last know button reading
unsigned long lastDebounceTimeZ = 0; //last time the pin was toggled, used to keep track of time
unsigned long debounceDelayZ = 50; //the debounce time which user sets prior to run
int ledStateA = LOW; //Variable for current LED state
int wakeStateY= HIGH; //Variable for current RELAY state
static unsigned long wakeCameON = 0; // Unused variable
int doorWarning = LOW; // Unused variable
unsigned long lastA = millis(); //set timer
unsigned long lastB = millis(); //set timer
unsigned long lastC = millis(); //set timer
unsigned long lastD = millis(); //set timer
unsigned long lastE = millis(); //set timer
unsigned long doorOpen; //Variable for door timer
unsigned long doorClose; //Variable for door timer
unsigned long dooropenTime; // Variable for millis door timer
unsigned long dooreventTime = 15000; //Variable for door timer 15sec
unsigned long previousdoorTime = 0; //Variable for door timer
int timerLatch = 0;
void(* resetFunc) (void) = 0; // create a standard reset function
//Buzzer variables....The following sketch beeps every second for 200ms, 5 times, and then goes silent. The sketch repeats every 1 minutes.
// Timer control
unsigned long currentTime;
// timer 1 variables
unsigned long previousTime1;
unsigned long delayTime1 = 1000; // 1 second
// timer 2 variables
unsigned long previousTime2;
unsigned long delayTime2 = 60000; // 60 seconds
int inDex = 0;
int buzzer = 50; // This is the digital IO pin that controls the buzzer
int buzVal; //value of buzzer output
int frequency = 3000;
int duration = 200;
// End of buzzer timer functions-variables
int pixel_x, pixel_y; //Touch_getXY() updates global vars
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()); //.kbv makes sense to me
pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
}
return pressed;
}
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define GREY 0x8410
void setup(void)
{
pinMode(buzzer, OUTPUT); // set pin mode as “output” for Door/s open warning
pinMode(wake, OUTPUT); // set pin mode as “output” to turn on relay for TFT Display
pinMode(wakePB, INPUT); // set pin mode as "input" for wake up Push Button
pinMode(Reset,OUTPUT); //Reset for TFT display to "RESET"
previousTime1 = millis();
previousTime2 = millis();
previousdoorTime = millis();
mySerial.begin(9600);
Serial.begin(9600);
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
Serial.println("Calibrate for your Touch Panel");
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(0); //PORTRAIT
tft.fillScreen(BLACK);
Aon_btn.initButton(&tft, 60, 180, 100, 40, WHITE, CYAN, BLACK, "TX A", 2);
Aoff_btn.initButton(&tft, 60, 230, 100, 40, WHITE, RED, BLACK, "STOP A", 2);
Bon_btn.initButton(&tft, 180, 180, 100, 40, WHITE, CYAN, BLACK, "TX B", 2);
Boff_btn.initButton(&tft, 180, 230, 100, 40, WHITE, RED, BLACK, "STOP B", 2);
Con_btn.initButton(&tft, 60, 280, 100, 40, WHITE, YELLOW, BLACK, "CONF", 2);
Coff_btn.initButton(&tft, 180, 280, 100, 40, WHITE, GREEN, BLACK, "RESET", 2);
Aon_btn.drawButton(false); // door A setup
Aoff_btn.drawButton(false);
/* tft.setTextColor(WHITE, RED); // garage door A message
tft.setTextSize(12);
tft.setCursor(15, 30);
tft.print("A");
tft.setTextColor(WHITE, RED); // garage door A closed message
tft.setTextSize(2);
tft.setCursor(15, 125);
tft.print("closed");
*/
Bon_btn.drawButton(false); // door B setup
Boff_btn.drawButton(false);
/* tft.setTextColor(WHITE, RED); // garage door message
tft.setTextSize(12);
tft.setCursor(140, 30);
tft.print("B");
tft.setTextColor(WHITE, RED); // garage door B closed message
tft.setTextSize(2);
tft.setCursor(140, 125);
tft.print("closed");
*/
Con_btn.drawButton(false); // TX/EXE
Coff_btn.drawButton(false);
}
void loop(void)
{
if(mySerial.available() > 1)
{
int input = mySerial.parseInt(); //read serial input and convert to integer (-32,768 to 32,767)
if(input == 0001) //if on code is received Door A open
{
tft.setTextColor(WHITE, GREEN); // garage door A message
tft.setTextSize(12);
tft.setCursor(15, 30);
tft.print("A");
tft.setTextColor(WHITE, GREEN); // garage door A open message
tft.setTextSize(2);
tft.setCursor(15, 125);
tft.print(" open ");
doorStateA = 1; // Set door A position OPEN
mySerial.flush(); //clear the serial buffer for unwanted inputs
/*Serial.print(" doorState A open: ");
Serial.println(doorStateA); */
}
if(input == 0010) //if off code is received Door A unknown
{
tft.setTextColor(BLACK, YELLOW); // garage door A unknown state
tft.setTextSize(12);
tft.setCursor(15, 30);
tft.print("!");
tft.setTextColor(BLACK, YELLOW); // garage door A open message
tft.setTextSize(2);
tft.setCursor(15, 125);
tft.print(" move ");
doorStateA = 2; // Set door A position MOVE
mySerial.flush(); //clear the serial buffer for unwanted inputs
/*Serial.print(" doorState A move: ");
Serial.println(doorStateA); */
}
if(input == 0011) //if on code is received Door A closed
{
tft.setTextColor(WHITE, RED); // garage door A message
tft.setTextSize(12);
tft.setCursor(15, 30);
tft.print("A");
tft.setTextColor(WHITE, RED); // garage door A closed message
tft.setTextSize(2);
tft.setCursor(15, 125);
tft.print("closed");
doorStateA = 3; // Set door A position CLOSED
mySerial.flush(); //clear the serial buffer for unwanted inputs
/*Serial.print(" doorState A closed: ");
Serial.println(doorStateA); */
}
if(input == 0100) //if off code is received
{
mySerial.flush(); //clear the serial buffer for unwanted inputs
}
if(input == 0101) //if on code is received Door B open
{
tft.setTextColor(WHITE, GREEN); // garage door message
tft.setTextSize(12);
tft.setCursor(140, 30);
tft.print("B");
tft.setTextColor(WHITE, GREEN); // garage Door B open message
tft.setTextSize(2);
tft.setCursor(140, 125);
tft.print(" open ");
doorStateB = 1; // Set door B position OPEN
mySerial.flush(); //clear the serial buffer for unwanted inputs
/*Serial.print (" doorState B open: ");
Serial.println (doorStateB); */
}
if(input == 0110) //if off code is received Door B unknown
{
tft.setTextColor(BLACK, YELLOW); // garage door message
tft.setTextSize(12);
tft.setCursor(140, 30);
tft.print("!");
tft.setTextColor(BLACK, YELLOW); // garage door B move message
tft.setTextSize(2);
tft.setCursor(140, 125);
tft.print(" move ");
doorStateB = 2; // Set door B position MOVE
mySerial.flush(); //clear the serial buffer for unwanted inputs
/*Serial.print(" doorState B move: ");
Serial.println(doorStateB);*/
}
if(input == 0111) //if on code is received Door B closed
{
tft.setTextColor(WHITE, RED); // garage door message
tft.setTextSize(12);
tft.setCursor(140, 30);
tft.print("B");
tft.setTextColor(WHITE, RED); // garage door B closed message
tft.setTextSize(2);
tft.setCursor(140, 125);
tft.print("closed");
doorStateB = 3; // Set door B position CLOSED
mySerial.flush(); //clear the serial buffer for unwanted inputs
/*Serial.print(" doorState B closed: ");
Serial.println(doorStateB);*/
}
if(input == 1000) //if off code is received light switch
{
mySerial.flush(); //clear the serial buffer for unwanted inputs
}
if(input == 1001) //if on code is received - light switc
{
mySerial.flush(); //clear the serial buffer for unwanted inputs
}
if(input == 1010) //if off code is received - light switc
{
mySerial.flush(); //clear the serial buffer for unwanted inputs
}
mySerial.flush();//clear the serial buffer for unwanted inputs
delay(10);//delay little for better serial communication
tft.setTextColor(WHITE, BLACK); // garage door message
tft.setTextSize(2);
tft.setCursor(50, 10);
tft.print("GARAGE DOORS");
bool down = Touch_getXY();
Aon_btn.press(down && Aon_btn.contains(pixel_x, pixel_y));
Aoff_btn.press(down && Aoff_btn.contains(pixel_x, pixel_y));
Bon_btn.press(down && Bon_btn.contains(pixel_x, pixel_y));
Boff_btn.press(down && Boff_btn.contains(pixel_x, pixel_y));
Con_btn.press(down && Con_btn.contains(pixel_x, pixel_y));
Coff_btn.press(down && Coff_btn.contains(pixel_x, pixel_y));
if (Aon_btn.justReleased())
Aon_btn.drawButton();
if (Aoff_btn.justReleased())
Aoff_btn.drawButton();
if (Bon_btn.justReleased())
Bon_btn.drawButton();
if (Boff_btn.justReleased())
Boff_btn.drawButton();
if (Con_btn.justReleased())
Con_btn.drawButton();
if (Coff_btn.justReleased())
Coff_btn.drawButton();
if (Aon_btn.justPressed())
{
Aon_btn.drawButton(true);
//WILL SEND CLOSE DOOR A COMMAND
}
if (Bon_btn.justPressed())
{
Bon_btn.drawButton(true);
// WILL SEND CLOSE DOOR B COMMAND
}
if (Aoff_btn.justPressed())
{
Aoff_btn.drawButton(true);
// WILL SEND DOOR A STOP/ REVERSE
}
if (Boff_btn.justPressed())
{
Boff_btn.drawButton(true);
//WILL SEND DOOR B STOP/REVERSE
}
if (Con_btn.justPressed())
{
Con_btn.drawButton(true);
// CONFIRM WILL GO GREEN
}
if (Coff_btn.justPressed()) //WILL SEND 'RESET' COMMAND
{
Coff_btn.drawButton(true);
mySerial.println(1110); //send unique code to the receiver to reset timer for doors open (RESET). In this case 1110
doorStateOpen = LOW; // Reset door open count
/*digitalWrite(buzzer, LOW); // set buzzer OFF
delay(1000);*/
mySerial.flush(); //clear the serial buffer for unwanted inputs
}
}
// Door buzzer alarm if doors open > 30min
if (doorStateA == 3 && doorStateB == 3) //if on code is received - door A and B closed
{
doorStateClosed = LOW;
timerStart = LOW; //Want timed delay but no millis function is working in this programme
doorStateOpen = LOW;
timerLatch = 0;
doorStateOpen = LOW;
}
if (doorStateClosed == LOW) // If door A or door B CLOSED > 'X' seconds then turn WARNING about to sound.
{
doorClose = millis();
Serial.println("Doors CLOSED");
if(millis() - doorClose > 2000) // Doors CLOSED 'X' seconds (1 Seconds) " This timer Does not work"
{
// Reset timers as required but no millis function working
}
}
if (doorStateA == 1 || doorStateB == 1 || doorStateB == 2 || doorStateB ==2) //if on code is received - door A or B open or in abnormal position A/B are (moving)
{
doorStateOpen = HIGH;
doorStateClosed = HIGH;
dooropenTime = millis();
}
if (doorStateOpen == HIGH && timerLatch == 0) // If door A or door B moving or open > 'X' seconds then turn WARNING about to sound.
{
if (dooropenTime - previousdoorTime > dooreventTime) // Doors open/move 'X' seconds (15 Seconds)
{
timerStart = HIGH;
timerLatch = 1;
previousdoorTime = dooropenTime;
Serial.println("OPEN");
Serial.println(dooropenTime);
}
if (timerStart == HIGH)
{
currentTime = millis();
// beep every second for 200ms, 5 times
if (currentTime >= previousTime1 + delayTime1)
{
Serial.println("Buzz Time loop");
inDex++; // increment every 1000ms
if (inDex < 6)
{ // 5 times
tone(buzzer, frequency, duration);
}
previousTime1 = currentTime;
}
// reset inDex after 300000ms (5 minutes)
if (currentTime >= previousTime2 + delayTime2)
{
inDex = 0;
previousTime2 = currentTime;
}
}
}
}
#endif