GithubHelp home page GithubHelp logo

hwhardsoft / arduitouch-codelock-esp32 Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 1.0 42 KB

Simple codelock / keypad example for ArduiTouch ESP with ESP32

Home Page: https://www.hwhardsoft.de/english/projects/arduitouch-esp/

License: GNU General Public License v3.0

C++ 8.38% C 91.62%
ili9341 esp32-arduino touchscreen tft-display codelock keypad numpad arduitouch esp32

arduitouch-codelock-esp32's Introduction

ArduiTouch-Codelock-ESP32

Different people asked me for a very simple code example for our ArduiTouch ESP set without too much additional functions. This is a very simple codelock / keypad example for ArduiTouch ESP with ESP32 to demonstate the basic graphic and audio features of the ArduiTouch kit.

My image

Usage

Install the following libraries through Arduino Library Manager

Adafruit GFX Library https://github.com/adafruit/Adafruit-GFX-Library/archive/master.zip Adafruit ILI9341 Library https://github.com/adafruit/Adafruit_ILI9341 XPT2046_Touchscreen by Paul Stoffregen https://github.com/PaulStoffregen/XPT2046_Touchscreen/blob/master/XPT2046_Touchscreen.h

You can also download the library also directly as ZIP file and uncompress the folder under yourarduinosketchfolder/libraries/

After installing the Adafruit libraries, restart the Arduino IDE.

You can find the latest version of the code and the description of the hardware at https://www.hwhardsoft.de/english/projects/arduitouch-esp/

License

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

arduitouch-codelock-esp32's People

Contributors

hwhardsoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

janlund

arduitouch-codelock-esp32's Issues

code adaptation

Hi, I'm trying to redo your code for ILI9488 and the TFT_eSPI library, but I don't understand why it won't start, tell me what the problem is

/*______Import Libraries_______*/
#include <Arduino.h>
#include <SPI.h>
#include "TFT_eSPI.h"
//#include "usergraphics.h"
/*______End of Libraries_______*/

/*____Calibrate Touchscreen_____*/
#define MINPRESSURE 10      // minimum required force for touch event
#define TS_MINX 370
#define TS_MINY 470
#define TS_MAXX 3700
#define TS_MAXY 3600
/*______End of Calibration______*/


/*___Keylock spezific definitions___*/
#define codenum 42    // 42 is the answer for everything, but you can change this to any number between 0 and 999999

/*___End of Keylock spezific definitions___*/

TFT_eSPI tft = TFT_eSPI(SCREEN_HEIGHT, SCREEN_WIDTH);

String symbol[4][4] = {
  { "7", "8", "9" },
  { "4", "5", "6" },
  { "1", "2", "3" },
  { "C", "0", "OK" }
};
long Num1,Num2,Number;
char action;
boolean result = false;
bool pressed = false;
uint16_t t_x = 0, t_y = 0, t_z = 0;

bool Touch_Event();
void DetectButtons();
void DisplayResult();
void IntroScreen();
void draw_Result_Box();
void draw_BoxNButtons();
void Button_ACK_Tone();

void setup() {
  Serial.begin(115200); //Use serial monitor for debugging

  pinMode(TFT_BL, OUTPUT); // define as output for backlight control

  Serial.println("Init TFT and Touch...");
  tft.begin();
  Serial.print("tftx = "); Serial.print(tft.width()); Serial.print(" tfty = "); Serial.println(tft.height());
  tft.fillScreen(TFT_BLACK);
  
  IntroScreen();
  digitalWrite(TFT_BL, LOW);    // LOW to turn backlight on; 

  delay(1500);

  digitalWrite(TFT_BL, HIGH);    // HIGH to turn backlight off - will hide the display during drawing
  draw_BoxNButtons(); 
  digitalWrite(TFT_BL, LOW);    // LOW to turn backlight on; 

  //sound configuration
  ledcSetup(0, 5000, 8);
  ledcAttachPin(TFT_BL, 0);

}

/********************************************************************//**
 * @brief     detects a touch event and converts touch data 
 * @param[in] None
 * @return    boolean (true = touch pressed, false = touch unpressed) 
 *********************************************************************/
bool Touch_Event() {
  pressed = tft.getTouch(&t_x, &t_y); 
  delay(1);
  #ifdef touch_yellow_header
    p.x = map(p.x, TS_MINX, TS_MAXX, 320, 0); // yellow header
  #else
    t_x = map(t_x, TS_MINX, TS_MAXX, 0, 320); // black header
  #endif
  t_y = map(t_y, TS_MINY, TS_MAXY, 0, 240);
  if (t_z > MINPRESSURE) return true;  
  return false;  
}

/********************************************************************//**
 * @brief     detecting pressed buttons with the given touchscreen values
 * @param[in] None
 * @return    None
 *********************************************************************/
void DetectButtons() {
  if (t_y>185) //Detecting Buttons on Column 1
  {
    if (t_x>265) //If cancel Button is pressed
    {Serial.println ("Button Cancel"); Number=Num1=Num2=0; result=false;}
    
     if (t_x>205 && t_x<255) //If Button 1 is pressed
    {Serial.println ("Button 1"); 
    Button_ACK_Tone();
    if (Number==0)
    Number=1;
    else
    Number = (Number*10) + 1; //Pressed twice
    }
    
     if (t_x>145 && t_x<195) //If Button 4 is pressed
    {Serial.println ("Button 4"); 
    Button_ACK_Tone();
    if (Number==0)
    Number=4;
    else
    Number = (Number*10) + 4; //Pressed twice
    }
    
     if (t_x>85 && t_x<135) //If Button 7 is pressed
    {Serial.println ("Button 7");
    Button_ACK_Tone();
    if (Number==0)
    Number=7;
    else
    Number = (Number*10) + 7; //Pressed twice
    } 
  }

  if (t_y<175 && t_y>85) //Detecting Buttons on Column 2
  {
    if (t_x>265)
    {Serial.println ("Button 0"); //Button 0 is Pressed
    Button_ACK_Tone();
    if (Number==0)
    Number=0;
    else
    Number = (Number*10) + 0; //Pressed twice
    }
    
    if (t_x>205 && t_x<255)
    {Serial.println ("Button 2"); 
    Button_ACK_Tone();
     if (Number==0)
    Number=2;
    else
    Number = (Number*10) + 2; //Pressed twice
    }
    
     if (t_x>145 && t_x<195)
    {Serial.println ("Button 5"); 
    Button_ACK_Tone();
     if (Number==0)
    Number=5;
    else
    Number = (Number*10) + 5; //Pressed twic
    }
    
     if (t_x>85 && t_x<135)
    {Serial.println ("Button 8"); 
    Button_ACK_Tone();
     if (Number==0)
    Number=8;
    else
    Number = (Number*10) + 8; //Pressed twic
    }   
  }

  if (t_y>0 && t_y<75) //Detecting Buttons on Column 3
  {
    if (t_x>265)
    {Serial.println ("Button OK"); 
    result = true;
    }
    
     if (t_x>205 && t_x<255)
    {Serial.println ("Button 3"); 
    Button_ACK_Tone();
    if (Number==0)
    Number=3;
    else
    Number = (Number*10) + 3; //Pressed twice
    }
    
     if (t_x>145 && t_x<195)
    {Serial.println ("Button 6"); 
    Button_ACK_Tone();
    if (Number==0)
    Number=6;
    else
    Number = (Number*10) + 6; //Pressed twice
    }
    
     if (t_x>85 && t_x<135)
    {Serial.println ("Button 9");
    Button_ACK_Tone();
    if (Number==0)
    Number=9;
    else
    Number = (Number*10) + 9; //Pressed twice
    }   
  }

}


/********************************************************************//**
 * @brief     shows the entered numbers (stars)
 * @param[in] None
 * @return    None
 *********************************************************************/
void DisplayResult() {
    String s1="";
    //String s2  = String(Number);
    tft.fillRect(0, 0, 240, 80, TFT_CYAN);  //clear result box
    tft.setCursor(10, 20);
    tft.setTextSize(4);
    tft.setTextColor(TFT_BLACK);
    if (Number == 0) {
      tft.println(" ");
    } else { 
       for (int i=0;i< String(Number).length();i++)
       s1 = s1 + "*";
       tft.println(s1); //update new value
    }   
}

/********************************************************************//**
 * @brief     shows the intro screen in setup procedure
 * @param[in] None
 * @return    None
 *********************************************************************/
void IntroScreen() {
  //Draw the Result Box
  tft.fillRect(0, 0, 240, 320, TFT_WHITE);
  //tft.drawXBitmap(20,80, Zihatec_Logo,200,60);
  tft.setTextSize(0);
  tft.setTextColor(TFT_BLACK);
  tft.setFreeFont(&FreeSansBold9pt7b);  

  tft.setCursor(45, 190);
  tft.println("ArduiTouch ESP");
  
  tft.setCursor(43, 215);
  tft.println("Keylock example");

}


/********************************************************************//**
 * @brief     draws a result box after code confirmation with ok button
 * @param[in] color background color of result box
 * @param[in] test  string to display in result box area
 * @param[in] xPos  X position of text output
 * @return    None
 *********************************************************************/
void draw_Result_Box(int color, char text[10], char xPos) {
   //Draw the Result Box
   tft.fillRect(0, 0, 240, 80, color);
   tft.setCursor(xPos, 26);
   tft.setTextSize(3);
   tft.setTextColor(TFT_WHITE);

   // draw text
   tft.println(text);
}

/********************************************************************//**
 * @brief     draws the keypad
 * @param[in] None
 * @return    None
 *********************************************************************/
void draw_BoxNButtons() {
   //clear screen black
  tft.fillRect(0, 0, 240, 320, TFT_BLACK);
  tft.setFreeFont(0);  
  
  //Draw the Result Box
  tft.fillRect(0, 0, 240, 80, TFT_CYAN);

  //Draw C and OK field   
  tft.fillRect  (0,260,80,60,TFT_RED);
  tft.fillRect  (160,260,80,60,TFT_GREEN); 
  
  //Draw Horizontal Lines
  for (int h=80; h<=320; h+=60)
  tft.drawFastHLine(0, h, 240, TFT_WHITE);

  //Draw Vertical Lines
  for (int v=80; v<=240; v+=80)
  tft.drawFastVLine(v, 80, 240, TFT_WHITE);

  //Display keypad lables 
  for (int j=0;j<4;j++) {
    for (int i=0;i<3;i++) {
      tft.setCursor(32 + (80*i), 100 + (60*j)); 
      if ((j==3) && (i==2)) tft.setCursor(24 + (80*i), 100 + (60*j)); //OK button
      tft.setTextSize(3);
      tft.setTextColor(TFT_WHITE);
      tft.println(symbol[j][i]);
    }
  }
}

/********************************************************************//**
 * @brief     plays ack tone (beep) after button pressing
 * @param[in] None
 * @return    None
 *********************************************************************/
void Button_ACK_Tone() {
  ledcWriteTone(0, 600);
}

void loop() {
  // check touch screen for new events
  if (Touch_Event()== true) { 
    &t_y; &t_x;
    pressed = true;
    
  } else {
    pressed = false;
  }

  // if touch is pressed detect pressed buttons
  if (pressed == true) {
    
    DetectButtons();
  
    if (result==true) {
      if (Number == codenum) {
        draw_Result_Box(TFT_GREEN,"CODE OK", 60);
        ledcWriteTone(0,1000);
        delay(800);
        ledcWriteTone(0,0);
      } else {
        draw_Result_Box(TFT_RED, "WRONG CODE", 30);
        for (int i=0;i< 3;i++) {
          ledcWriteTone(0,600);
          delay(100);
          ledcWriteTone(0,0);
          delay(50);      
        }
      }
      delay(1000);
      Number = 0; 
      result=false;
    }
    DisplayResult(); 
  }    
  delay(100);
  ledcWriteTone(0,0);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.