Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.17 KB | None | 0 0
  1. //For 128x128 display
  2. //#include <TFT_ILI9163C.h>
  3. //#include <Adafruit_ST7735.h>
  4.   //For drawBitmap function
  5.   //#include <Adafruit_GFX.h>
  6.  
  7. #include <TFT.h>
  8. #include <SPI.h>
  9. //Allow bitmap arrays to display from program memory instead of RAM
  10. #include <avr/pgmspace.h>
  11.  
  12. // pin definition for the Uno
  13. #define cs   10
  14. #define dc   9
  15. #define rst  8
  16.        
  17. // create an instance of the library
  18. TFT TFTscreen = TFT(cs, dc, rst);
  19.  
  20. // char array to print to the screen
  21. char sensorPrintout[6];
  22.  
  23. // TFT color definiton
  24. #define  BLACK 0x0000
  25. #define BLUE 0x001F
  26. #define RED 0xF800
  27. #define GREEN 0x07E0
  28. #define CYAN 0x07FF
  29. #define MAGENTA 0xF81F
  30. #define YELLOW 0xFFE0
  31. #define WHITE 0xFFFF
  32.  
  33.         //Bitmap image has been converted into an array to store
  34.         //pixel data in program memory of the arduino, as storing
  35.         //it in RAM takes up too much space
  36.        
  37.         //Declaring data array for icon bitmap image as external variable
  38.         //to be used in program memory
  39.         //This is the fingerprint status locked icon
  40.         static const uint8_t icon [] PROGMEM = {
  41.         0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  42.         0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  43.         0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  44.         0x0,0x0,0x0,0x3f,0xc0,0x0,0x0,0x0,
  45.         0x0,0x0,0x3,0xff,0xf8,0x0,0x0,0x0,
  46.         0x0,0x0,0x7,0xff,0xfe,0x0,0x0,0x0,
  47.         0x0,0x0,0x1f,0xc0,0x7f,0x80,0x0,0x0,
  48.         0x0,0x0,0x3e,0x0,0x7,0xc0,0x0,0x0,
  49.         0x0,0x0,0x7c,0x0,0x3,0xe0,0x0,0x0,
  50.         0x0,0x0,0xfd,0xff,0x81,0xf0,0x0,0x0,
  51.         0x0,0x0,0xff,0xff,0xe0,0xf0,0x0,0x0,
  52.         0x0,0x1,0xff,0xff,0xf8,0x78,0x0,0x0,
  53.         0x0,0x1,0xff,0x80,0x7c,0x38,0x0,0x0,
  54.         0x0,0x3,0xfc,0x0,0xe,0x3c,0x0,0x0,
  55.         0x0,0x3,0xf0,0x0,0x7,0x1c,0x0,0x0,
  56.         0x0,0x7,0xc0,0x7f,0x83,0x8e,0x0,0x0,
  57.         0x0,0x7,0x83,0xff,0xe0,0xe,0x0,0x0,
  58.         0x0,0x7,0xf,0xff,0xf8,0xf,0x0,0x0,
  59.         0x0,0x6,0x1f,0x80,0xfc,0x7,0x0,0x0,
  60.         0x0,0x4,0x7e,0x0,0x3f,0x7,0x0,0x0,
  61.         0x0,0x0,0xf8,0x0,0xf,0x7,0x0,0x0,
  62.         0x0,0x0,0xf0,0x3e,0x7,0x87,0x0,0x0,
  63.         0x0,0x1,0xe1,0xff,0x83,0x83,0x80,0x0,
  64.         0x0,0x3,0xc3,0xff,0xc3,0xc3,0x80,0x0,
  65.         0x0,0x3,0xc7,0xc3,0xe1,0xc3,0x80,0x0,
  66.         0x0,0x3,0x8f,0x0,0xf1,0xe3,0x80,0x0,
  67.         0x0,0x7,0x1e,0x0,0x78,0xe3,0x80,0x0,
  68.         0x0,0x7,0x1e,0x3c,0x38,0xe3,0x80,0x0,
  69.         0x0,0x7,0x1c,0x7e,0x38,0xe3,0x80,0x0,
  70.         0x0,0xf,0x1c,0x7f,0x38,0xe3,0x80,0x0,
  71.         0x0,0xe,0x3c,0xf7,0x38,0x71,0x80,0x0,
  72.         0x0,0xe,0x38,0xe7,0x38,0x71,0xc0,0x0,
  73.         0x0,0xe,0x38,0xe7,0x38,0x71,0xc0,0x0,
  74.         0x0,0xe,0x38,0xe7,0x38,0x73,0xc0,0x0,
  75.         0x0,0xe,0x38,0xe3,0x98,0xe3,0xc0,0x0,
  76.         0x0,0xe,0x38,0xe3,0xb8,0xe3,0x80,0x0,
  77.         0x0,0x0,0x38,0xe3,0xf8,0xe3,0x80,0x0,
  78.         0x0,0x0,0x38,0xe3,0xf8,0xe3,0x80,0x0,
  79.         0x0,0x0,0x3c,0xf1,0xf1,0xe3,0x80,0x0,
  80.         0x0,0x6,0x1c,0x70,0x1,0xc7,0x80,0x0,
  81.         0x0,0xe,0x1c,0x78,0x3,0xc7,0x80,0x0,
  82.         0x0,0xf,0x1c,0x3e,0x7,0x87,0x0,0x0,
  83.         0x0,0xf,0x1e,0x3f,0xff,0x8f,0x0,0x0,
  84.         0x0,0xf,0x1e,0x1f,0xff,0x1f,0x0,0x0,
  85.         0x0,0xf,0xf,0x7,0xfc,0x3e,0x0,0x0,
  86.         0x0,0x7,0x87,0x80,0x0,0x7c,0x0,0x0,
  87.         0x0,0x7,0x87,0xe0,0x0,0xfc,0x0,0x0,
  88.         0x0,0x3,0xc3,0xf8,0x7,0xf8,0x0,0x0,
  89.         0x0,0x3,0xe1,0xff,0xff,0xe1,0x0,0x0,
  90.         0x0,0x1,0xe0,0x7f,0xff,0x83,0x0,0x0,
  91.         0x0,0x1,0xf8,0xf,0xfe,0x7,0x0,0x0,
  92.         0x0,0x0,0xfc,0x0,0x0,0xe,0x0,0x0,
  93.         0x0,0x0,0x3f,0x0,0x0,0x3c,0x0,0x0,
  94.         0x0,0x0,0x1f,0xe0,0x1,0xf8,0x0,0x0,
  95.         0x0,0x0,0x7,0xff,0xff,0xf0,0x0,0x0,
  96.         0x0,0x0,0x1,0xff,0xff,0xc0,0x0,0x0,
  97.         0x0,0x0,0x0,0x1f,0xfc,0x0,0x0,0x0,
  98.         0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  99.         0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  100.         0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0
  101.         };
  102.  
  103. //For fingerprint sensor (model GT-511C1R)
  104. #include "FPS_GT511C3.h"
  105. #include "SoftwareSerial.h"
  106. //FPS connected to pin 4 and 5 - see previous schemas
  107. FPS_GT511C3 fps(2, 3);
  108.  
  109.        
  110. //For thermistor (temperature sensor)
  111. int tempPin = 0;
  112.  
  113. //For RGB LED
  114. #define ledRED 4
  115. #define ledGREEN 5
  116.  
  117. //For buzzer
  118. #include "pitches.h"
  119. int buzzer = A1;
  120.  
  121. //For ultrasonic sensor
  122. #include "SR04.h"
  123. #define TRIG_PIN 6
  124. #define ECHO_PIN 7
  125. SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
  126. long a;
  127.  
  128. //Defines variables for sensor calculations
  129. long duration;
  130. int distance;
  131. int safetyDistance;
  132.  
  133. void setup() {
  134.   Serial.begin(9600);
  135.   pinMode(ledRED, OUTPUT); //Red LED
  136.   pinMode(ledGREEN, OUTPUT); //Green LED
  137.   pinMode(buzzer, OUTPUT);
  138.  
  139.   //Put this line at the beginning of every sketch that uses the GLCD:
  140.   TFTscreen.begin();
  141.   TFTscreen.setRotation(2); //Put orientation of display upside down
  142.   // clear the screen with a black background
  143.   TFTscreen.background(0, 0, 0);
  144.  
  145.   //Default green LED to on so it's green before the door is opened/alarm is set off
  146.   digitalWrite(ledGREEN, HIGH); //Turn green LED on
  147.   digitalWrite(ledRED, LOW);  //Turn red LED off
  148. }
  149.  
  150. void loop() {
  151.   boolean fingerprintMatch = false; //fingerprintMatch false so ultrasonic sensor "listens" for door opening
  152.  
  153.   //Thermistor temp sensor calculations
  154.   //double to 2 decimal places
  155.   double tempReading = static_cast<double>(analogRead(tempPin));
  156.   // This is OK
  157.   double tempK = log(10000.0 * ((1024.0 / tempReading - 1.0)));
  158.   //Temperature in kelvin
  159.   tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK)) * tempK);       //  Temp Kelvin
  160.   double tempC = tempK - 273.15;            // Convert Kelvin to Celcius
  161.   //double tempF = (tempC * 9.0) / 5.0 + 32.0; // Convert Celcius to Fahrenheit
  162.    
  163.   //Temperature as float with 2 decimal places
  164.   float tempFAsFloat = static_cast<float>(tempK);
  165.   float tempCAsFloat = static_cast<float>(tempC);
  166.  
  167.  ultrasonicOnOff();
  168.    
  169.   //Read the ECHO_PIN and return the sound wave travel time in ms
  170.   duration = pulseIn(ECHO_PIN, HIGH);
  171.  
  172.    //Calculate the distance
  173.    distance= duration*0.034/2;
  174.    safetyDistance = distance;
  175.  
  176.    fingerprintMatch = false;
  177.  
  178.    if(safetyDistance <= 5) { //If distance 5cm or less
  179.     boolean alarmSetOff = true;
  180.     digitalWrite(ledGREEN, LOW); //Turn green LED off
  181.     digitalWrite(ledRED, HIGH);  //Turn red LED on
  182.     digitalWrite(buzzer, HIGH); //Turn buzzer on
  183.     TFTscreen.background(0, 0, 0);  //Clear the screen with a black background
  184.     displayLockScreen();  //Display "use fingerprint to unlock" message
  185.     //Fingerprint sensor
  186.     fps.Open(); //send serial command to initialise FPS
  187.     fps.SetLED(true); //turn on LED so finger can be read
  188.     delay(20);
  189.     fps.SetLED(false);
  190.     delay(20);
  191.     fps.SetLED(true);
  192.     delay(20);
  193.     fps.SetLED(false);
  194.     delay(20);
  195.     fps.SetLED(true);
  196.        
  197.     digitalWrite(ECHO_PIN, LOW);
  198.        
  199.     else if(safetyDistance <= 5 && alarmSetOff, true) { //If distance 5cm or less but alarm already set off, LED stays red
  200.       digitalWrite(ledGREEN, LOW); //Turn green LED off
  201.       digitalWrite(ledRED, HIGH);  //Turn red LED on
  202.     }
  203.     else{ //If distance > 5cm
  204.       digitalWrite(ledGREEN, HIGH); //Turn green LED off
  205.       digitalWrite(ledRED, LOW);  //Turn red LED on
  206.     }
  207.   }
  208. }
  209.  
  210.  
  211.  
  212.    void displayTemp(float cTemp)  //Function to display the temperature on the TFT display
  213.    {
  214.      // write the static text to the screen
  215.   // set the font color to white
  216.   TFTscreen.stroke(255, 255, 255);
  217.   // set the font size
  218.   TFTscreen.setTextSize(2);
  219.   // write the text to the top left corner of the screen
  220.   TFTscreen.text("Temperature:\n", 0, 25);
  221.   TFTscreen.text("o", 110, 55);
  222.   TFTscreen.text("C", 120, 65);
  223.   // state the font size very large for the loop
  224.   TFTscreen.setTextSize(3);
  225.    
  226.      //Force temp reading into a string so it can be put in a char array
  227.      String sensorVal = String(cTemp, 2);
  228.  
  229.      // convert the reading to a char array
  230.      String(sensorVal).toCharArray(sensorPrintout, 6);
  231.    
  232.      // set the font color
  233.      TFTscreen.stroke(255, 255, 255);
  234.      // print the sensor value
  235.      TFTscreen.text(sensorPrintout, 20, 60);
  236.      // wait for a moment
  237.      delay(250);
  238.      // erase the text you just wrote
  239.      TFTscreen.stroke(0, 0, 0);
  240.      TFTscreen.text(sensorPrintout, 20, 60);
  241.    }
  242.  
  243.      
  244. void drawBitmap(int16_t x, int16_t y, //Adafruit GFX not supported in this case so function has to be declared manually
  245.                                       //drawBitmap(x coordinates, y coordinates, name of bitmap, x resolution, x resolution, colour)
  246.  const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
  247.  
  248.   int16_t i, j, byteWidth = (w + 7) / 8;
  249.   uint8_t byte;
  250.  
  251.   for(j=0; j<h; j++) {
  252.     for(i=0; i<w; i++) {
  253.       if(i & 7) byte <<= 1;
  254.       else      byte   = pgm_read_byte(bitmap + j * byteWidth + i / 8);
  255.       if(byte & 0x80) TFTscreen.drawPixel(x+i, y+j, color);
  256.     }
  257.   }
  258.  }
  259.  
  260. void displayLockScreen() //Shows fingerprint lock screen on TFT display
  261. {
  262.   TFTscreen.drawRect(0,0,128,128,WHITE);
  263.   TFTscreen.setCursor(30,10);
  264.   TFTscreen.setTextColor(RED);
  265.   TFTscreen.setTextSize(2);
  266.   TFTscreen.print("LOCKED");
  267.  
  268.   TFTscreen.setCursor(10,100);
  269.   TFTscreen.setTextColor(WHITE);
  270.   TFTscreen.setTextSize(1);
  271.   TFTscreen.print("Waiting for valid \n    fingerprint.");
  272.   TFTscreen.drawBitmap(30,35,icon,60,60,WHITE);
  273.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement