/*
This is an example on how to use the 2.2" and 2.4" TFT 240x320 SPI ILI9341 display using the Adafruit library.
ILI9341 TFT SPI display pins for Arduino Uno/Nano:
This is a 3.3V device so you need to put a level shifter on all 5 data pins.
An easy way to do this is to use a 2.2K resistor between the data pin on the display to the pin on arduino, and
another resistor on the side of the display to a 3.3K resistor to ground. This creates a voltage divider to
convert the 5V output of arduino into a 3V logic. Do this for all 5 data pins.
* LED = 10Ω (or more) resistor to 5V
* SCK = 13
* SDI = 11
* D/C = 9
* RESET = 8 or RST or 3.3V
* CS = 10
* GND = GND
* VCC = 5V or 3.3V (the display has it's own 3.3V regulator)
Hardware SPI Pins:
* Arduino Uno SCK=13, SDA=11
* Arduino Nano SCK=13, SDA=11
* Arduino Due SCK=76, SDA=75
* Arduino Mega SCK=52, SDA=51
SPI pin names can be confusing. These are the alternative names for the SPI pins:
MOSI = DIN = R/W = SDO = DI = SI = MTSR = SDA = D1 = SDI
CS = CE = RS = SS
DC = A0 = DO = DOUT = SO = MRST
RESET = RST
SCLK = CLK = E = SCK = SCL = D0
Libraries needed:
https://github.com/adafruit/Adafruit_ILI9341
https://github.com/adafruit/Adafruit-GFX-Library
Reference page for GFX Library: https://cdn-learn.adafruit.com/downloads/pdf/adafruit-gfx-graphics-library.pdf
Color is expressed in 16 bit with Hexadecimal value.
To select a particular color, go here and copy the "Hexadecimal 16 bit color depth value":
https://ee-programming-notepad.blogspot.com/2016/10/16-bit-color-generator-picker.html
Common colors:
* BLACK 0x0000
* BLUE 0x001F
* RED 0xF800
* GREEN 0x07E0
* CYAN 0x07FF
* MAGENTA 0xF81F
* YELLOW 0xFFE0
* WHITE 0xFFFF
List of custom fonts: https://learn.adafruit.com/adafruit-gfx-graphics-library/using-fonts
Note about custom font:
* Text background color is not supported for custom fonts. For these reason you would need to draw a filled
rectangle before drawing the text. But this would cause the text to flicker, so I don't recommend using custom fonts
for components that refresh continuously.
* Using custom fonts slows down the arduino loop, so the refresh rate is lesser than using the standard font.
Sketch made by: InterlinkKnight
Last modification: 01/29/2018
*/
#include <Adafruit_GFX.h> // Include core graphics library
#include <Adafruit_ILI9341.h> // Include Adafruit_ILI9341 library to drive the display
#include <Keypad.h>
#include <Fonts/FreeSerif24pt7b.h> // Add a custom font
// Declare pins for the display:
#define TFT_DC 9
#define TFT_RST 8 // You can also connect this to the Arduino reset in which case, set this #define pin to -1!
#define TFT_CS 10
// The rest of the pins are pre-selected as the default hardware SPI for Arduino Uno (SCK = 13 and SDA = 11)
const byte ROWS = 4;
const byte COLS = 3;
const int buttonPin = 0;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {7, 6, 5, 4};
byte colPins[COLS] = {3, 2, A2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
int Variable1; // Create a variable to have something dynamic to show on the display
int buttonState = 0;
int x=0;
void setup() // Start of setup
{
Serial.begin(9600);
// Display setup:
pinMode(12, INPUT);
tft.begin(); // Initialize display
tft.fillScreen(0x0000); // Fill screen with black
//tft.setRotation(0); // Set orientation of the display. Values are from 0 to 3. If not declared, orientation would be 0,
// which is portrait mode.
tft.setTextWrap(false); // By default, long lines of text are set to automatically “wrap” back to the leftmost column.
// To override this behavior (so text will run off the right side of the display - useful for
// scrolling marquee effects), use setTextWrap(false). The normal wrapping behavior is restored
// with setTextWrap(true).
// We are going to print on the display everything that is static on the setup, to leave the loop free for dynamic elements:
// Write to the display the text "Hello":
tft.setCursor(20,50); // Set position (x,y)
tft.setTextColor(0xFFFF); // Set color of text. First is the color of text and after is color of background
tft.setTextSize(3); // Set text size. Goes from 0 (the smallest) to 20 (very big)
tft.println("Zvolte mod: "); // Print a text or value
tft.fillRect(15, 120, 210, 61, 0xFFFF);
tft.fillRect(18, 123, 204, 55, 0x0000); // Draw rectangle (x,y,width,height,color)
tft.fillRect(15, 190, 210, 61, 0xFFFF);
tft.fillRect(18, 193, 204, 55, 0x0000);
tft.fillRect(15, 260, 210, 61, 0xFFFF);
tft.fillRect(18, 262, 204, 55, 0x0000);
tft.setTextColor(0xFFFF);
tft.setCursor(50, 140);
tft.println("Okamzite");
tft.setCursor(32, 210);
tft.println("Odpocet 5s");
tft.setCursor(23, 280);
tft.println("Odpocet 10s");
// Draw triangle:
// tft.drawTriangle(100,240, 130,190, 160,240, 0xFFE0); // Draw triangle (x0,y0,x1,y1,x2,y2,color)
// Draw filled triangle:
// tft.fillTriangle(179,240, 209,190, 239,240, 0x07FF); // Draw filled triangle (x0,y0,x1,y1,x2,y2,color)
// Draw line:
//tft.drawLine(0, 250, 239, 250, 0x07FF); // Draw line (x0,y0,x1,y1,color)
// Draw circle:
// tft.drawCircle(30, 289, 30, 0x07E0); // Draw circle (x,y,radius,color)
// Draw a filled circle:
//tft.fillCircle(110, 289, 30, 0x001F); // Draw circle (x,y,radius,color)
// Draw filled rounded rectangle:
//tft.fillRoundRect(160, 259, 80, 60, 10, 0xF81B); // Draw rounded filled rectangle (x,y,width,height,color)
} // End of setup
void loop() // Start of loop
{
char hodnota;
String spravne_heslo="4865";
int pokusy=0;
String heslo="";
int potenc = analogRead(A0);
Serial.println(potenc);
if(potenc<=341 && potenc>=0){
tft.fillRect(20, 124, 200, 53, 0xFFFF);
tft.fillRect(20, 195, 200, 51, 0x0000);
tft.fillRect(20, 264, 200, 52, 0x0000);
tft.setTextColor(0x0000);
tft.setCursor(50, 140);
tft.println("Okamzite");
tft.setTextColor(0xFFFF);
tft.setCursor(32, 210);
tft.println("Odpocet 5s");
tft.setCursor(23, 280);
tft.println("Odpocet 10s");
do{
if (digitalRead(12) == 1){
tft.fillScreen(0x0000);
tft.setTextColor(0xFFFF);
tft.setCursor(80, 40);
tft.println("Zadaj");
tft.setCursor(40, 80);
tft.println("4-miestny");
tft.setCursor(85, 120);
tft.println("PIN:");
tft.fillRect(20, 240, 40, 7, 0xFFFF);
tft.fillRect(70, 240, 40, 7, 0xFFFF);
tft.fillRect(120, 240, 40, 7, 0xFFFF);
tft.fillRect(170, 240, 40, 7, 0xFFFF);
do{
tft.fillRect(20, 180, 400, 60, 0x0000);
heslo="";
do{
tft.setTextColor(0xFFFF);
tft.fillRect(20, 240, 40, 7, 0x001F);
hodnota = customKeypad.getKey();
}
while(hodnota!='0' && hodnota!='1'&& hodnota!='2'&& hodnota!='3'&& hodnota!='4'&& hodnota!='5'
&& hodnota!='5'&& hodnota!='6'&& hodnota!='7'&& hodnota!='8'&& hodnota!='9');
tft.setCursor(30, 210);
tft.println(hodnota);
tft.fillRect(20, 240, 40, 7, 0xFFFF);
heslo.concat(hodnota);
do{
tft.fillRect(70, 240, 40, 7, 0x001F);
hodnota = customKeypad.getKey();
}
while(hodnota!='0' && hodnota!='1'&& hodnota!='2'&& hodnota!='3'&& hodnota!='4'&& hodnota!='5'
&& hodnota!='5'&& hodnota!='6'&& hodnota!='7'&& hodnota!='8'&& hodnota!='9');
tft.setCursor(80, 210);
tft.println(hodnota);
tft.fillRect(70, 240, 40, 7, 0xFFFF);
heslo.concat(hodnota);
do{
tft.fillRect(120, 240, 40, 7, 0x001F);
hodnota = customKeypad.getKey();
}
while(hodnota!='0' && hodnota!='1'&& hodnota!='2'&& hodnota!='3'&& hodnota!='4'&& hodnota!='5'
&& hodnota!='5'&& hodnota!='6'&& hodnota!='7'&& hodnota!='8'&& hodnota!='9');
tft.setCursor(130, 210);
tft.println(hodnota);
tft.fillRect(120, 240, 40, 7, 0xFFFF);
heslo.concat(hodnota);
do{
tft.fillRect(170, 240, 40, 7, 0x001F);
hodnota = customKeypad.getKey();
}
while(hodnota!='0' && hodnota!='1'&& hodnota!='2'&& hodnota!='3'&& hodnota!='4'&& hodnota!='5'
&& hodnota!='5'&& hodnota!='6'&& hodnota!='7'&& hodnota!='8'&& hodnota!='9');
tft.setCursor(180, 210);
tft.println(hodnota);
tft.fillRect(170, 240, 40, 7, 0xFFFF);
heslo.concat(hodnota);
Serial.println(heslo);
if(heslo==spravne_heslo){
Serial.println("good");}
else {
tft.fillRect(20, 180, 200, 40, 0xF800);
tft.setTextSize(3);
for(int i=0; i<5; i++){
tft.setTextColor(0x0000);
tft.setCursor(40, 190);
tft.print("Zle Heslo");
delay(300);
tft.setTextColor(0xFFFF);
tft.setCursor(40, 190);
tft.print("Zle Heslo");
delay(300);
}
pokusy++;
}
if(pokusy==1){
tft.setTextSize(4);
tft.setTextColor(0xF800);
tft.setCursor(50, 290);
tft.print("X");
}
else if(pokusy==2){
tft.setTextSize(4);
tft.setTextColor(0xF800);
tft.setCursor(110, 290);
tft.print("X");
}
else if(pokusy==3){
tft.setTextSize(4);
tft.setTextColor(0xF800);
tft.setCursor(170, 290);
tft.print("X");
tft.setTextSize(3);
tft.fillRect(20, 180, 200, 80, 0xF800);
tft.setTextColor(0x0000);
tft.setCursor(26, 190);
tft.print("Pristup bol");
tft.setCursor(26, 225);
tft.print("Zamietnuty");
delay(2000);
break;
}
}while(heslo!=spravne_heslo);
do{
}
while(analogRead(A0)>341);
}
potenc = analogRead(A0);
}
while(potenc<341);
}
else if(potenc<=682 && potenc>341){
tft.fillRect(20, 195, 200, 51, 0xFFFF);
tft.fillRect(20, 124, 200, 53, 0x0000);
tft.fillRect(20, 264, 200, 52, 0x0000);
tft.setTextColor(0x0000);
tft.setCursor(32, 210);
tft.println("Odpocet 5s");
tft.setTextColor(0xFFFF);
tft.setCursor(50, 140);
tft.println("Okamzite");
tft.setCursor(23, 280);
tft.println("Odpocet 10s");
do{
potenc = analogRead(A0);
}
while(potenc<682&& potenc>341);
}
else if(potenc<=1023 && potenc>682){
tft.fillRect(20, 195, 200, 51, 0x0000);
tft.fillRect(20, 124, 200, 53, 0x0000);
tft.fillRect(20, 264, 200, 52, 0xFFFF);
tft.setTextColor(0x0000);
tft.setCursor(23, 280);
tft.println("Odpocet 10s");
tft.setTextColor(0xFFFF);
tft.setCursor(32, 210);
tft.println("Odpocet 5s");
tft.setCursor(50, 140);
tft.println("Okamzite");
do{
potenc = analogRead(A0);
}
while(potenc>682);
Serial.println("konec");
}
// We are going to print on the display everything that is dynamic on the loop, to refresh continuously:
// Write to the display the Variable1 with left text alignment:
// tft.setCursor(21, 125); // Set position (x,y)
//tft.setTextColor(0xFFE0, 0x0000); // Set color of text. First is the color of text and after is color of background
//tft.setTextSize(4); // Set text size. Goes from 0 (the smallest) to 20 (very big)
//tft.println(hodnota); // Print a text or value
// There is a problem when we go, for example, from 100 to 99 because it doesn't automatically write a background on
// the last digit we are not longer refreshing. We need to check how many digits are and fill the space remaining.
// Write to the display the string with right text alignment:
// tft.setCursor(150, 125); // Set position (x,y)
//tft.setTextColor(0x07E0, 0x0000); // Set color of text. First is the color of text and after is color of background
//tft.setTextSize(3); // Set text size. Goes from 0 (the smallest) to 20 (very big)
//tft.println(Variable1); // Print a text or value
// We are going to write the Variable1 with a custom text, so you can see the issues:
// Draw a black square in the background to "clear" the previous text:
// This is because we are going to use a custom font, and that doesn't support brackground color.
// We are basically printing our own background. This will cause flickering, though.
// tft.fillRect(0, 198, 75, 34, 0x0000); // Draw filled rectangle (x,y,width,height,color)
// Reset to standard font, to stop using any custom font previously set
} // End of loop