// a Program to Simulate a Numpad using a 2.8" TFT Touchscreen // Open the Serial Monitor to see the program running #include #include #include #include MCUFRIEND_kbv tft; #include // copy-paste results from TouchScreen_Calibr_native.ino const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x9341 const int TS_LEFT=927,TS_RT=126,TS_TOP=70,TS_BOT=910; // Introduction to 5v Solid State relay. int relayPin = 39;// set pin 39 for relay output //SPI Communication #define LCD_CS A3 #define LCD_CD A2 #define LCD_WR A1 #define LCD_RD A0 // optional #define LCD_RESET A4 //Color Definitons #define BLACK 0x0000 #define BLUE 0x001F #define BLUEVIOLET 0x895C #define BROWN 0xA145 #define CHOCOLATE 0xD343 #define DARKGREEN 0x0320 #define DARKSALMON 0xECAF #define FIREBRICK 0xB104 #define GREEN 0x0400 #define GREENYELLOW 0xAFE5 #define IVORY 0xFFFE #define LIGHTBLUE 0xAEDC #define LIGHTGREEN 0x9772 #define LIGHTSKYBLUE 0x867F #define LIGHTSTEELBLUE 0xB63B #define LIGHTYELLOW 0xFFFC #define LIME 0x07E0 #define LIMEGREEN 0x3666 #define MAGENTA 0xF81F #define MAROON 0x8000 #define MIDNIGHTBLUE 0x18CE #define NAVY 0x0010 #define ORANGE 0xFD20 #define PINK 0xFE19 #define PLUM 0xDD1B #define POWDERBLUE 0xB71C #define PURPLE 0x8010 #define RED 0xF800 #define SPRINGGREEN 0x07EF #define YELLOW 0xFFE0 #define WHITE 0xFFFF #define MINPRESSURE 1 #define MAXPRESSURE 1000 // For better pressure precision, we need to know the resistance // between X+ and X- Use any multimeter to read it // For the one we're using, its 300 ohms across the X plate // Pins A2-A6 TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364); //initialize password to 1234 //you can use password.set(newPassword) to overwrite it Password password = Password( "1234" ); //Size of key containers 70px #define BOXSIZE 70 //Container variables for touch coordinates int X, Y, Z; //Screen height without hidden pixel double tHeight = tft.height() - 1; //Centering the mid square double center = (tft.width() / 2) - (BOXSIZE / 2); //Space between squares double padding = 10; //Position of squares to the left and right of center double fromCenter = BOXSIZE + padding; //Second row Y-Axis position double secondRow = BOXSIZE + padding; //Third row Y-Axis position double thirdRow = secondRow + BOXSIZE + padding; //Fourth row Y-Axis position double fourthRow = thirdRow + BOXSIZE + padding; //Y-Axis align for all squares double verticalAlign = (tHeight - ((BOXSIZE * 4) + (padding * 3))) / 2; //Left column starting x posision double leftColPositionX = center - fromCenter; //Mid column starting x posision double midColPositionX = center; //Right column starting x posision double rightColPositionX = center + fromCenter; void Buttons(); void setup() { Serial.begin(9600); tft.reset(); uint16_t identifier = tft.readID(); Serial.print("TFT ID = 0x"); Serial.println(identifier, HEX); tft.begin(identifier); //Relay Pin pinMode(relayPin, OUTPUT); // Turn the relay switch OFF to keep door closed digitalWrite(relayPin, LOW);// set relay pin to LOW Serial.println("Relay OFF door locked"); //Background color tft.fillScreen(BLACK); //PORTRAIT tft.setRotation(0); // draw num pad createButtons(); insertNumbers(); Serial.println(F("Press any button on the TFT screen: ")); } void loop() { retrieveTouch(); int boxHeightRow1 = verticalAlign + BOXSIZE; int boxHeightRow2 = secondRow + BOXSIZE; int boxHeightRow3 = thirdRow + BOXSIZE; int boxHeightRow4 = fourthRow + BOXSIZE; if (Z > MINPRESSURE && Z < MAXPRESSURE) { //redraw numpad to clear old number tft.fillScreen(BLACK); createButtons(); insertNumbers(); //default text setup for number display on tft, tft.setCursor(100, 120); tft.setTextColor(WHITE); tft.setTextSize(6); //Check if element clicked is in left column if (X > leftColPositionX && X < (leftColPositionX + BOXSIZE)) { //Check if element clicked is in row 1 if (Y > verticalAlign) { if (Y < boxHeightRow1) { Serial.println("1"); tft.println("1"); password.append('1');//add 1 to the guessed password; delay(150); } //Check if element clicked is in row 2 else if (Y < boxHeightRow2) { Serial.println("4"); tft.println("4"); password.append('4');//add 4 to the guessed password; delay(150); } //Check if element clicked is in row 3 else if (Y < boxHeightRow3) { Serial.println("7"); tft.println("7"); delay(150); } //Check if element clicked is in row 4 else if (Y < boxHeightRow4) { Serial.println("Enter"); tft.println(" "); delay(150); checkPassword(); } } //Check if element clicked is in mid column } else if (X > midColPositionX && X < (midColPositionX + BOXSIZE)) { //Check if element clicked is in row 1 if (Y > verticalAlign) { if (Y < boxHeightRow1) { Serial.println("2"); tft.println("2"); password.append('2');//add 2 to the guessed password; delay(150); } //Check if element clicked is in row 2 else if (Y < boxHeightRow2) { Serial.println("5"); tft.println("5"); delay(150); } //Check if element clicked is in row 3 else if (Y < boxHeightRow3) { Serial.println("8"); tft.println("8"); delay(150); } //Check if element clicked is in row 4 else if (Y < boxHeightRow4) { Serial.println("Enter"); tft.println(" "); delay(150); checkPassword(); } } //Check if element clicked is in third column } else if (X > rightColPositionX && X < (rightColPositionX + BOXSIZE)) { if (Y > verticalAlign) { //Check if element clicked is in row 1 if (Y < boxHeightRow1) { Serial.println("3"); tft.println("3"); password.append('3');//add 3 to the guessed password; delay(150); } //Check if element clicked is in row 2 else if (Y < boxHeightRow2) { Serial.println("6"); tft.println("6"); delay(150); } //Check if element clicked is in row 3 else if (Y < boxHeightRow3) { Serial.println("9"); tft.println("9"); delay(150); } //Check if element clicked is in row 3 else if (Y < boxHeightRow4) { Serial.println("0"); tft.println("0"); delay(150); } } } // good for debuggin, prints out the x,y cordinates of the press // tft.setTextSize(3); // tft.print("X = "); tft.println(X); // tft.print("Y = "); tft.println(Y); } } void retrieveTouch() { digitalWrite(13, HIGH); TSPoint p = ts.getPoint(); digitalWrite(13, LOW); //If sharing pins, you'll need to fix the directions of the touchscreen pins, pinmode required for library pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); //Scale from 0->1023 to tft.width //X = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //Y = map(p.y, TS_TOP, TS_BOT, 0, tft.height()); // on my tft the numbers are reversed so this is used instead of the above X = tft.width() - map(p.x, TS_RT, TS_LEFT, 0, tft.width()); Y = map(p.y, TS_TOP, TS_BOT, 0, tft.height()); Z = p.z; } void createButtons() { //(initial x,initial y,width,height,color) double secondRowVertialAlign = secondRow + verticalAlign; double thirdRowVertialAlign = thirdRow + verticalAlign; double fourthRowVertialAlign = fourthRow + verticalAlign; /***Draw filled squares with specified dimensions and position***/ //First Row tft.fillRect(leftColPositionX, verticalAlign, BOXSIZE, BOXSIZE, DARKGREEN); tft.fillRect(midColPositionX, verticalAlign, BOXSIZE, BOXSIZE, DARKGREEN); tft.fillRect(rightColPositionX, verticalAlign, BOXSIZE, BOXSIZE, DARKGREEN); //Second Row tft.fillRect(leftColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, DARKGREEN); tft.fillRect(midColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, DARKGREEN); tft.fillRect(rightColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, DARKGREEN); //Third Row tft.fillRect(leftColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, DARKGREEN); tft.fillRect(midColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, DARKGREEN); tft.fillRect(rightColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, DARKGREEN); //Fourth Row tft.fillRect(leftColPositionX, fourthRowVertialAlign, (BOXSIZE * 2) + padding, BOXSIZE, DARKGREEN); tft.fillRect(rightColPositionX, fourthRowVertialAlign, BOXSIZE, BOXSIZE, DARKGREEN); /***Draw Borders around squares***/ //First Row tft.drawRect(leftColPositionX, verticalAlign, BOXSIZE, BOXSIZE, BLACK); tft.drawRect(midColPositionX, verticalAlign, BOXSIZE, BOXSIZE, BLACK); tft.drawRect(rightColPositionX, verticalAlign, BOXSIZE, BOXSIZE, BLACK); //Second Row tft.drawRect(leftColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, BLACK); tft.drawRect(midColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, BLACK); tft.drawRect(rightColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, BLACK); //Third Row tft.drawRect(leftColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, BLACK); tft.drawRect(midColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, BLACK); tft.drawRect(rightColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, BLACK); //Fourth Row tft.drawRect(leftColPositionX, fourthRowVertialAlign, (BOXSIZE * 2) + padding, BOXSIZE, BLACK); tft.drawRect(rightColPositionX, fourthRowVertialAlign, BOXSIZE, BOXSIZE, BLACK); } void insertNumbers() { //Centers text horizontally on all three columns double leftColCursorX = leftColPositionX + (BOXSIZE / 3); double midColCursorX = midColPositionX + (BOXSIZE / 3); double rightColCursorX = rightColPositionX + (BOXSIZE / 3); //Centers text horizontally on all four rows double firstRowCursorY = verticalAlign + (BOXSIZE / 3); double secondRowCursorY = secondRow + firstRowCursorY; double thirdRowCursorY = thirdRow + firstRowCursorY; double fourthRowCursorY = fourthRow + firstRowCursorY; tft.setTextSize(4); tft.setTextColor(BLACK); //Insert Number 1 tft.setCursor(leftColCursorX, firstRowCursorY); tft.println("1"); //Insert Number 2 tft.setCursor(midColCursorX, firstRowCursorY); tft.println("2"); //Insert Number 3 tft.setCursor(rightColCursorX, firstRowCursorY); tft.println("3"); //Insert Number 4 tft.setCursor(leftColCursorX, secondRowCursorY); tft.println("4"); //Insert Number 5 tft.setCursor(midColCursorX, secondRowCursorY); tft.println("5"); //Insert Number 6 tft.setCursor(rightColCursorX, secondRowCursorY); tft.println("6"); //Insert Number 7 tft.setCursor(leftColCursorX, thirdRowCursorY); tft.println("7"); //Insert Number 8 tft.setCursor(midColCursorX, thirdRowCursorY); tft.println("8"); //Insert Number 9 tft.setCursor(rightColCursorX, thirdRowCursorY); tft.println("9"); //Insert Enter txt tft.setCursor(leftColCursorX, fourthRowCursorY); tft.println("Enter"); //Insert 0 Number tft.setCursor(rightColCursorX, fourthRowCursorY); tft.println("0"); } void checkPassword() { if (password.evaluate()){ Serial.println("Access Granted"); Serial.println("Relay ON"); tft.fillScreen(BLACK); tft.setCursor(25, 100); tft.setTextSize(5); tft.setTextColor(BLUE); tft.println("Access Granted"); digitalWrite(relayPin, HIGH);// Turn the relay switch ON to open door delay(5000); //digitalWrite(relayPin, LOW);// set relay pin to LOW //Serial.println("Relay OFF"); } else { Serial.println("Access Denied"); tft.fillScreen(BLACK); tft.setCursor(30, 100); tft.setTextSize(5); tft.setTextColor(RED); tft.println("Access Denied"); delay(2000); //digitalWrite(relayPin, LOW);// set relay pin to LOW } resetPassword(); } void resetPassword() { password.reset(); //delay(2000); setup(); }