Hi all;
I wrote the code the door opened and closed no problem. Limit switches are working properly.
I made several attempts with this code but main lock switch (lockPin) did not work properly.
If the main lock is off can not perform any function. This must be the first priority. As I told,
code works perfectly in this state but main lock (lockPin) I can't control switch.
#include <tinyFAT.h>
#include <UTFT.h>
#include <UTFT_tinyFAT.h>
#include <UTouch.h>
extern uint8_t BigFont[];
char doorstate; // setup a variable for the open/closed status of door, using 0 for closed and 1 for open.
char lockstate; // door security lock.
UTouch myTouch(6,5,4,3,2);
UTFT myGLCD(CTE70, 38, 39, 40, 41); //
UTFT_tinyFAT myFiles(&myGLCD);
const int closePin = 8;
const int openPin = 9;
const int lockPin = 10;
const int closeRelay = 11;
const int openRelay = 12;
int x, y;
char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";
void drawButtons()
{
// Draw the upper row of buttons
for (x=0; x<5; x++)
{
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10+(x*60), 10, 60+(x*60), 60);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10+(x*60), 10, 60+(x*60), 60);
myGLCD.printNumI(x+1, 27+(x*60), 27);
}
// Draw the center row of buttons
for (x=0; x<5; x++)
{
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10+(x*60), 70, 60+(x*60), 120);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10+(x*60), 70, 60+(x*60), 120);
if (x<4)
myGLCD.printNumI(x+6, 27+(x*60), 87);
}
myGLCD.print("0", 267, 87);
// Draw the lower row of buttons
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10, 130, 150, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 130, 150, 180);
myGLCD.print("Clear", 40, 147);
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (160, 130, 300, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (160, 130, 300, 180);
myGLCD.print("Enter", 190, 147);
myGLCD.setBackColor (0, 0, 0);
}
void updateStr(int val)
{
if (stCurrentLen<20)
{
stCurrent[stCurrentLen]=val;
stCurrent[stCurrentLen+1]='\0';
stCurrentLen++;
myGLCD.setColor(0, 255, 0);
myGLCD.print(stCurrent, LEFT, 224);
}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("BUFFER FULL!", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
delay(500);
myGLCD.print("BUFFER FULL!", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
myGLCD.setColor(0, 255, 0);
}
}
void waitForIt(int x1, int y1, int x2, int y2)
{
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while (myTouch.dataAvailable())
myTouch.read();
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (x1, y1, x2, y2);
}
void setup()
{
myGLCD.InitLCD();
myGLCD.clrScr();
file.initFAT();
myGLCD.setColor(255,255,255);
myGLCD.setBackColor(VGA_WHITE);
//myGLCD.fillScr(VGA_WHITE);
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(0, 0, 255);
//pin modu ayarlari
pinMode(closePin, INPUT);
pinMode(openPin, INPUT);
pinMode(lockPin, INPUT);
pinMode(openRelay, OUTPUT);
pinMode(closeRelay, OUTPUT);
}
void loop()
{
setdoorstate();
myFiles.loadBitmap(150, 120, 256, 256, "stop.raw");
myFiles.loadBitmap(540, -10, 256, 256, "ust.raw");
myFiles.loadBitmap(540, 245, 256, 256, "alt.raw");
myFiles.loadBitmap(115, 400, 305, 61, "logo.raw");
while (true)
{
if (myTouch.dataAvailable())
{
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if ((y>=0) && (y<=240)) // Upper row
{
if ((x>=540) && (x<=795)) // Button: 1
{
waitForIt(540, 0, 795, 240);
setdoorstate();
opencase();
}
}
}
if ((y>=245) && (y<=480))
{
if ((x>=540) && (x<=795)) // Button: 2
{
waitForIt(540, 245, 795, 480);
//updateStr('2');
// if (digitalRead(lockPin) == 0) {
// while (digitalRead(lockPin) != 1)
// myGLCD.print("DOOR LOCKED ", 25, 25); }
// else{
// lockcheck();
setdoorstate();
closecase();
}
}
}
}
//void lockcheck()
// {
// if (digitalRead(lockPin) == 1)
// {
// myGLCD.print ("DOOR LOCKED! ", 25, 25);
// while (digitalRead(lockPin) != 1) {
// myGLCD.print ("DOOR LOCKED! ", 25, 25); } }
// if (digitalRead(lockPin) == 0)
// {
// lockstate = 0; }
// if (lockstate == 1) {
// while (digitalRead(lockPin) != 1) {
// myGLCD.print ("DOOR LOCKED! ", 25, 25); }
// }
// }
void setdoorstate() {
// if (digitalRead(lockPin) == 1) {
// lockstate = 1;
// } // check limit switches
if (digitalRead(openPin) == 1) {
doorstate = 1;
} // kasa acik
if (digitalRead(closePin) == 1) {
doorstate = 0;
} // kasa kapali
}
void opencase()
{
// if(doorstate == 2)
// {
// myGLCD.print ("DOOR LOCKED", 25, 25);
// }
if(doorstate == 1) {
myGLCD.print ("DOOR IS ALREADY OPEN! ", 25, 25);
}
else{
myGLCD.print ("DOOR OPENING ", 25, 25);
while (digitalRead(openPin) != 1)
{
digitalWrite(openRelay, HIGH);
if (myTouch.dataAvailable()) // Emergency Stop Button
{
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if ((y>=120) && (y<=370)) // Upper row
{
if ((x>=150) && (x<=405)) // Button: 1
{
waitForIt(150, 120, 405, 370);
break;
}
}
// delay(50);
}
// delay(50);
}
digitalWrite(openRelay, LOW);
myGLCD.print ("DOOR OPEN ", 25, 25);
}
}
void closecase()
{
if(doorstate == 0) {
myGLCD.print ("the door is already open! ", 25, 25);
}
else{
myGLCD.print ("DOOR CLOSING ", 25, 25);
while (digitalRead(closePin) != 1)
{
digitalWrite(closeRelay, HIGH);
if (myTouch.dataAvailable()) //Emergency Stop Button
{
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if ((y>=120) && (y<=370)) // Upper row
{
if ((x>=150) && (x<=405)) // Button: 1
{
waitForIt(150, 120, 405, 370);
//updateStr('1');
// myGLCD.print("DOOR OPENED", 25, 25);
break;
}
}
// delay(50);
}
}
digitalWrite(closeRelay, LOW);
myGLCD.print ("DOOR CLOSED ", 25, 25);
}
}
Thank you for help.