This thread will be updated over the next few weeks stay tuned....
#include <UTFT.h>
#include <UTouch.h>
#include <EEPROM.h>
#define PULSEIN A0
#define WEST 2
#define WESTRELAY 3
#define EAST 4
#define EASTRELAY 5
//EEPROM
#define EE_CURPOS 10
#define EE_EASTLIMIT 100
#define EE_WESTLIMIT 200
// Declare fonts used
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t UbuntuBold[]; //24x32
extern uint8_t SevenSegNumFont[];
extern uint8_t various_symbols[];
extern uint8_t SixteenSegment16x24[];
extern uint8_t SixteenSegment32x48[];
extern uint8_t SixteenSegment40x60[];
extern uint8_t SixteenSegment128x192Num[];
extern uint8_t Grotesk16x32[];
extern uint8_t GroteskBold32x64[];
extern uint8_t DotMatrix_M_Slash[];//16x22
extern uint8_t DotMatrix_XL_Num[];//32x50
extern uint8_t battery_24x48[];
extern uint8_t ArialNumFontPlus[];//32x50 Num and : only
extern uint8_t swiss721_outline[];//16x16
extern uint8_t Arial_round_16x24[];
extern uint8_t Inconsola[]; //24x32
//extern uint8_t tmp_agfx2[]; //64x128
extern uint8_t SixteenSegment64x96Num[];
extern uint8_t SevenSegment96x144Num[];
extern uint8_t Calibri32x64GR[];
// Set the pins to the correct ones for your development shield
// ------------------------------------------------------------
// Standard Arduino Mega/Due shield : <display model>,38,39,40,41
// Remember to change the model parameter to suit your display module!
UTFT TFT(SSD1963_800480, 38, 39, 40, 41); //(byte model, int RS, int WR, int CS, int RST, int SER)
UTouch TOUCH( 43, 42, 44, 45, 46); //byte tclk, byte tcs, byte din, byte dout, byte irq
char Version[] = "FIRMWARE 0.0.94";
char VDate[] = "March 02, 2020";
int pos = 0; //Current Position
int prePos = 0; //Previous Position
bool posPulseReady = 1; //Positive Pulse or reed switch closure
bool zeroPulseReady = 0; //Zero pulse or reed switch open
int eastLimit = 0;
int westLimit = 0;
bool errorClearOK = 0;
bool posError = 0;
static bool getTimeOK = 1;
static unsigned long startTime = 0;
static unsigned long currentTime = 0;
bool eastBlock = 0;
bool westBlock = 0;
//==============================
//========Touch=================
bool westTouch = 0;
bool westTActive = 0;
bool eastTouch = 0;
bool eastTActive = 0;
void setup()
{
pinMode(PULSEIN, INPUT);
pinMode(WEST, INPUT);
pinMode(EAST, INPUT);
pinMode(WESTRELAY, OUTPUT);
pinMode(EASTRELAY, OUTPUT);
// Setup the LCD
TFT.InitLCD();
// -------------------------------------------------------------
pinMode(8, OUTPUT); //backlight
digitalWrite(8, HIGH);//on
// -------------------------------------------------------------
TFT.setFont(BigFont);
TFT.clrScr();
//----------------------TOUCH---------------
TOUCH.InitTouch();
TOUCH.setPrecision(PREC_MEDIUM);
//------------------------------------------
TFT.setColor(255, 255, 255);
TFT.fillRect(0, 0, 799, 16);
TFT.setColor(0, 0, 255);
TFT.fillRect(0, 466, 799, 479);
TFT.setColor(255, 255, 255);
TFT.setBackColor(255, 0, 0);
TFT.print("* KE4EST 01.15.2020 *", CENTER, 1);
TFT.setBackColor(64, 64, 64);
TFT.setColor(255, 255, 0);
TFT.print("Dish Mover Beta", CENTER, 465);
// TFT.setColor(0, 0, 255);
// TFT.drawRect(0, 14, 799, 465);
TFT.setBackColor(0, 0, 0);
TFT.setFont(Calibri32x64GR);
TFT.setColor(255, 255, 255); //WHITE
Serial.begin(115200);
TFT.setFont(UbuntuBold);
TFT.printNumI(pos, 320, 100);
//EEPROM================
pos = EEPROM.read(EE_CURPOS);
eastLimit = EEPROM.read(EE_EASTLIMIT);
westLimit = EEPROM.read(EE_WESTLIMIT);
Setup();
DrawButtons();
}
void loop()
{
CurrentPosition(); //Display current position number if changed
//=====================================================
//===== GET TOUCH INPUT================================
ScanForTouchWest();
ScanForTouchEast();
ScanForTouchMenu();
//=====================================================
//======================WEST===========================
if (!westBlock)
{
if (West() || westTouch)
{
eastBlock = 1;
WestActive();
}
else //else west button not depressed
{
eastBlock = 0;
WestNotActive();
}
}
//======================EAST========================
if (!eastBlock)
{
if (East() || eastTouch)
{
westBlock = 1;
EastActive();
}
else //else west button not depressed
{
westBlock = 0;
EastNotActive();
}
}
}
bool Pulse()
{
bool pulse = digitalRead(PULSEIN);
return pulse;
}
bool West()
{
bool west = digitalRead(WEST);
return west;
}
bool East()
{
bool east = digitalRead(EAST);
return east;
}
unsigned long PulseErrorCheck()
{
if (getTimeOK)
{
startTime = millis();
getTimeOK = 0;
}
currentTime = (millis() - startTime);
return currentTime;
}
void CurrentPosition()
{
int posPrint = 0;
if (pos != prePos)
{
if (pos < 0)
{
posPrint = pos * -1;
TFT.print("-", 295, 100);
}
else
{
posPrint = pos;
TFT.print(" ", 295, 100);
}
if (posPrint >= 0 && posPrint < 10)
{
TFT.printNumI(posPrint, 320, 100);
TFT.setColor(0, 0, 0);
TFT.fillRect(343, 100, 415, 130);
TFT.setColor(255, 255, 255);
}
else if (posPrint >= 10 && posPrint < 100)
{
TFT.printNumI(posPrint, 320, 100);
TFT.setColor(0, 0, 0);
TFT.fillRect(367, 100, 415, 130);
TFT.setColor(255, 255, 255);
}
else if (posPrint >= 100 && posPrint < 1000)
{
TFT.printNumI(posPrint, 320, 100);
TFT.setColor(0, 0, 0);
TFT.fillRect(391, 100, 415, 130);
TFT.setColor(255, 255, 255);
}
else if (posPrint >= 1000 && posPrint < 10000)
{
TFT.printNumI(posPrint, 320, 100);
// TFT.setColor(0, 255, 255);
// TFT.fillRect(432, 100, 468, 130);
TFT.setColor(255, 255, 255);
}
else if (posPrint >= 10 && posPrint < 100)
{
TFT.printNumI(posPrint, 320, 100);
TFT.setColor(0, 0, 0);
TFT.fillRect(367, 100, 415, 130);
TFT.setColor(255, 255, 255);
}
else
{
TFT.printNumI(posPrint, 320, 100);
//TFT.setColor(0, 0, 0);
// TFT.fillRect(368, 140, 468, 170);
TFT.setColor(255, 255, 255);
}
Serial.println(pos);
prePos = pos;
}
}
void DrawButtons()
{
//Draw Button for Menu===================================
//Outer rectangle
TFT.setColor(255, 255, 255);
TFT.drawRoundRect(320, 400, 480, 460);
//inner rectangle
TFT.setColor(0, 0, 255);
TFT.drawRoundRect(322, 402, 478, 458);
//background color of button
TFT.setColor(255, 255, 255);
TFT.fillRoundRect(324, 404, 476, 456);
//Text for West
TFT.setColor(0, 0, 255);
TFT.setBackColor(255, 255, 255);
//TFT.setFont(BigFont);
TFT.print("MENU", 355, 415);
TFT.setBackColor(0, 0, 0);
}
void EastActive()
{
if (!posError)
{
digitalWrite(EASTRELAY, 1);
TFT.setColor(0, 255, 0);
TFT.fillCircle(768, 160, 12);
TFT.setColor(0, 255, 0);
TFT.print("EAST", 700, 100);
TFT.setColor(255, 255, 255);
errorClearOK = 1;
}
if (PulseErrorCheck() >= 1000)
{
TFT.setColor(255, 255, 255);
TFT.print("EAST ERROR", CENTER, 315);
digitalWrite(EASTRELAY, 0);
TFT.setColor(255, 0, 0);
TFT.fillCircle(768, 160, 12);
TFT.setColor(255, 255, 255);
posError = 1;
}
if (Pulse())
{
if (posPulseReady)
{
pos--;
posPulseReady = 0;
zeroPulseReady = 1;
getTimeOK = 1;
}
}
else
{
if (zeroPulseReady)
{
posPulseReady = 1;
zeroPulseReady = 0;
}
}
}
void EastNotActive()
{
TFT.print(" ", 700, 100); //clear the word EAST off of right hand of screen
getTimeOK = 1;
posError = 0;
if (errorClearOK)
{
TFT.print(" ", CENTER, 315); //Clear ERROR if exists
errorClearOK = 0;
digitalWrite(EASTRELAY, 0);
TFT.setColor(0, 0, 0);
TFT.fillCircle(768, 160, 12);
TFT.setColor(255, 255, 255);
}
}
void WestActive()
{
if (!posError)
{
digitalWrite(WESTRELAY, 1);
TFT.setColor(0, 255, 0);
TFT.fillCircle(20, 160, 12);
TFT.setColor(0, 255, 0);
TFT.print("WEST", 0, 100);
TFT.setColor(255, 255, 255);
errorClearOK = 1;
}
if (PulseErrorCheck() >= 1000)
{
TFT.setColor(255, 255, 255);
TFT.print("WEST ERROR", CENTER, 315);
digitalWrite(WESTRELAY, 0);
TFT.setColor(255, 0, 0);
TFT.fillCircle(20, 160, 12);
TFT.setColor(255, 255, 255);
posError = 1;
}
if (Pulse())
{
if (posPulseReady)
{
pos++;
posPulseReady = 0;
zeroPulseReady = 1;
getTimeOK = 1;
}
}
else
{
if (zeroPulseReady)
{
posPulseReady = 1;
zeroPulseReady = 0;
}
}
}
void WestNotActive()
{
TFT.print(" ", 0, 100); //clear the word WEST off of left hand of screen
getTimeOK = 1;
posError = 0;
if (errorClearOK)
{
TFT.print(" ", CENTER, 315);
errorClearOK = 0;
digitalWrite(WESTRELAY, 0);
TFT.setColor(0, 0, 0);
TFT.fillCircle(20, 160, 12);
TFT.setColor(255, 255, 255);
}
}
void Setup()
{
//Draw Button for West===================================
//Outer rectangle
TFT.setColor(255, 255, 255);
TFT.drawRoundRect(40, 300, 200, 360);
//inner rectangle
TFT.setColor(0, 0, 255);
TFT.drawRoundRect(42, 302, 198, 358);
//background color of button
TFT.setColor(255, 255, 255);
TFT.fillRoundRect(44, 304, 196, 356);
//Text for West
TFT.setColor(0, 0, 255);
TFT.setBackColor(255, 255, 255);
//TFT.setFont(BigFont);
TFT.print("WEST", 70, 315);
TFT.setBackColor(0, 0, 0);
//Draw Button for East===================================
//Outer rectangle
TFT.setColor(255, 255, 255);
TFT.drawRoundRect(601, 300, 759, 360);
//inner rectangle
TFT.setColor(0, 0, 255);
TFT.drawRoundRect(603, 302, 757, 358);
//background color of button
TFT.setColor(255, 255, 255);
TFT.fillRoundRect(605, 304, 755, 356);
//Text for West
TFT.setColor(0, 0, 255);
TFT.setBackColor(255, 255, 255);
//TFT.setFont(BigFont);
TFT.print("EAST", 635, 315);
TFT.setBackColor(0, 0, 0);
}
void ScanForTouchMenu()
{
}
void ScanForTouchEast()
{
int xT, yT;
if (TOUCH.dataAvailable())
{
TOUCH.read();
xT = TOUCH.getX();
yT = TOUCH.getY();
if ((yT >= 300) && (yT <= 360))
{
if ((xT >= 601) && (xT <= 759))
{
eastTActive = 1;
eastTouch = 1;
TFT.setColor(0, 0, 0);
TFT.drawRoundRect(601, 300, 759, 360);
TFT.setColor(0, 255, 255);
TFT.drawRoundRect(603, 302, 757, 358);
// TFT.setColor(255, 255, 255);
// TFT.print("West Touched ", CENTER, 200);
}
}
}
else
{
eastTouch = 0;
TFT.setColor(255, 255, 255);
TFT.drawRoundRect(601, 300, 759, 360);
TFT.setColor(0, 0, 255);
TFT.drawRoundRect(603, 302, 757, 358);
eastTActive = 0;
}
}
void ScanForTouchWest()
{
int xT, yT;
if (TOUCH.dataAvailable())
{
TOUCH.read();
xT = TOUCH.getX();
yT = TOUCH.getY();
if ((yT >= 300) && (yT <= 360))
{
if ((xT >= 40) && (xT <= 200))
{
westTActive = 1;
westTouch = 1;
TFT.setColor(0, 0, 0);
TFT.drawRoundRect(40, 300, 200, 360);
TFT.setColor(0, 255, 255);
TFT.drawRoundRect(42, 302, 198, 358);
// TFT.setColor(255, 255, 255);
// TFT.print("West Touched ", CENTER, 200);
}
}
}
else
{
westTouch = 0;
TFT.setColor(255, 255, 255);
TFT.drawRoundRect(40, 300, 200, 360);
TFT.setColor(0, 0, 255);
TFT.drawRoundRect(42, 302, 198, 358);
westTActive = 0;
}
}