Advertisement
muezza29

beli token v5

Jul 11th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.41 KB | None | 0 0
  1. // Library NFC
  2. #include <Wire.h>
  3. #include <SPI.h>
  4. #include <Adafruit_PN532.h>
  5.  
  6. #define PN532_SCK  (PA5)
  7. #define PN532_MOSI (PA7)
  8. #define PN532_SS   (PA4)
  9. #define PN532_MISO (PA6)
  10. #define PN532_IRQ   (2)
  11. #define PN532_RESET (3)
  12.  
  13.  
  14. // Library OLED
  15. #include "Arduino.h"
  16. #include "Wire.h"
  17. #include "Adafruit_GFX.h"
  18. #include "Adafruit_SSD1306.h"
  19.  
  20. #define OLED_RESET 4
  21. Adafruit_SSD1306 display(OLED_RESET);
  22.  
  23. Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
  24.  
  25. #if defined(ARDUINO_ARCH_SAMD)
  26. #define Serial SerialUSB
  27. #endif
  28.  
  29. void setup(void)
  30. {
  31. #ifndef ESP8266
  32. #endif
  33.   Serial.begin(115200);
  34.   Serial.println("Hello Arif!");
  35.  
  36.   nfc.begin();
  37.  
  38.   uint32_t versiondata = nfc.getFirmwareVersion();
  39.   if (! versiondata) {
  40.     Serial.print("Didn't find PN53x board");
  41.     while (1); // halt
  42.   }
  43.  
  44.   nfc.SAMConfig();
  45.  
  46.   Serial.println("Tempelkan kartu pelanggan");
  47.  
  48.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  49.   display.clearDisplay();
  50. }
  51.  
  52. String str1, str3;
  53. String str2 = "0x49 0xB3 0x9E 0x2B";
  54.  
  55.  
  56.  
  57. void loop(void)
  58. {
  59.   uint8_t success;
  60.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  61.   uint8_t uidLength;                          // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  62.  
  63.   success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  64.  
  65.   if (success)
  66.   {
  67.     String str22 = hexify(uid, uidLength);
  68.     results(str22);
  69.   }
  70. }
  71.  
  72. String hexify(uint8_t u[], byte len)
  73. {
  74.   String out = "";
  75.   for (byte i = 0; i < len; i++)
  76.   {
  77.     String a = String(u[i], HEX);
  78.     a.toUpperCase();
  79.     if (a.length() == 1) a = "0" + a;
  80.     out += "0x" + a + " ";
  81.   }
  82.   out.trim();
  83.   return out;
  84. }
  85.  
  86. void results(String str)
  87. {
  88.   if (str == str2)
  89.   {
  90.     display.clearDisplay();
  91.     display.setTextSize(1);
  92.     display.setTextColor(WHITE);
  93.  
  94.     display.setCursor(0, 0);
  95.     display.print("Pelanggan dikenali");
  96.  
  97.     display.setCursor(0, 8);
  98.     display.print("sebagai :");
  99.  
  100.     display.setCursor(0, 16);
  101.     display.print(" > 1303167045");
  102.     display.display();
  103.  
  104.     Serial.println("\n  1303167045");
  105.     beli();
  106.   }
  107.   else
  108.   {
  109.     display.clearDisplay();
  110.     display.setTextSize(1);
  111.     display.setTextColor(WHITE);
  112.  
  113.     display.setCursor(0, 0);
  114.     display.print("Tag NFC tidak");
  115.  
  116.     display.setCursor(0, 8);
  117.     display.print("dikenali");
  118.  
  119.     display.setCursor(0, 16);
  120.     display.print(" > Bukan pelanggan");
  121.     display.display();
  122.  
  123.     Serial.println("\n  Bukan pelanggan");
  124.   }
  125.   Serial.print("");
  126. }
  127.  
  128.  
  129.  
  130. void beli()
  131. {
  132.   delay(2000);
  133.   byte pilihan = 1;
  134.   byte jumlah_pilihan = 4;
  135.   refresh_pilihan_token(pilihan);
  136.   while (1)
  137.   {
  138.     if (check_nfc())
  139.     {
  140.       pilihan++;
  141.       if (pilihan > jumlah_pilihan)
  142.         pilihan = 1;
  143.       refresh_pilihan_token(pilihan);
  144.     }
  145.   }
  146. }
  147.  
  148. void refresh_pilihan_token(byte pilihan)
  149. {
  150.   display.clearDisplay();
  151.   display.setTextSize(1);
  152.   display.setTextColor(WHITE);
  153.  
  154.   unsigned long interval = 10000;
  155.   unsigned long previousMillis = 0;
  156.   unsigned long currentMillis = millis();
  157.  
  158.   switch (pilihan)
  159.   {
  160.     case 1:
  161.       display.setCursor(0, 0);
  162.       display.print("> 10K");
  163.       display.setCursor(0, 8);
  164.       display.print("  20K");
  165.       display.setCursor(0, 16);
  166.       display.print("  50K");
  167.       display.setCursor(0, 24);
  168.       display.print("  Batal");
  169.       if (millis() - previousMillis >=  interval)
  170.       {
  171.         printBeli(1);
  172.         previousMillis = 0;
  173.       }
  174.  
  175.       break;
  176.     case 2:
  177.       display.setCursor(0, 0);
  178.       display.print("  10K");
  179.       display.setCursor(0, 8);
  180.       display.print("> 20K");
  181.       display.setCursor(0, 16);
  182.       display.print("  50K");
  183.       display.setCursor(0, 24);
  184.       display.print("  Batal");
  185.       if (millis() - previousMillis >=  interval)
  186.       {
  187.         printBeli(2);
  188.         previousMillis = 0;
  189.       }
  190.       break;
  191.     case 3:
  192.       display.setCursor(0, 0);
  193.       display.print("  10K");
  194.       display.setCursor(0, 8);
  195.       display.print("  20K");
  196.       display.setCursor(0, 16);
  197.       display.print("> 50K");
  198.       display.setCursor(0, 24);
  199.       display.print("  Batal");
  200.       if (millis() - previousMillis >=  interval)
  201.       {
  202.         printBeli(3);
  203.         previousMillis = 0;
  204.       }
  205.       break;
  206.     case 4:
  207.       display.setCursor(0, 0);
  208.       display.print("  10K");
  209.       display.setCursor(0, 8);
  210.       display.print("  20K");
  211.       display.setCursor(0, 16);
  212.       display.print("  50K");
  213.       display.setCursor(0, 24);
  214.       display.print("> Batal");
  215.       if (millis() - previousMillis >=  interval)
  216.       {
  217.         printBeli(4);
  218.         previousMillis = 0;
  219.       }
  220.       break;
  221.   }
  222.   display.display();
  223. }
  224.  
  225. void printBeli(byte pilih) {
  226.   switch (pilih) {
  227.     case 1:
  228.       token10k();
  229.       break;
  230.  
  231.     case 2:
  232.       token20k();
  233.       break;
  234.  
  235.     case 3:
  236.       token50k();
  237.       break;
  238.  
  239.     case 4:
  240.       batal();
  241.       break;
  242.   }
  243. }
  244.  
  245. bool check_nfc()
  246. {
  247.   uint8_t success;
  248.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  249.   uint8_t uidLength;
  250.   success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  251.  
  252.   if (success)
  253.   {
  254.     String str22 = hexify(uid, uidLength);
  255.     if (str22.equals(str2)) return true;
  256.   }
  257.   return false;
  258. }
  259.  
  260. void token10k()
  261. {
  262.   display.clearDisplay();
  263.   display.setTextSize(1);
  264.   display.setTextColor(WHITE);
  265.  
  266.   display.setCursor(0, 0);
  267.   display.print("Token telah");
  268.   display.setCursor(0, 8);
  269.   display.print("ditambahkan sebesar  ");
  270.   display.setCursor(0, 16);
  271.   display.print("5,0484 kWh");
  272.   display.display();
  273. }
  274.  
  275. void token20k()
  276. {
  277.   display.clearDisplay();
  278.   display.setTextSize(1);
  279.   display.setTextColor(WHITE);
  280.  
  281.   display.setCursor(0, 0);
  282.   display.print("Token telah");
  283.   display.setCursor(0, 8);
  284.   display.print("ditambahkan sebesar  ");
  285.   display.setCursor(0, 16);
  286.   display.print("11,3588 kWh");
  287.   display.display();
  288. }
  289.  
  290. void token50k()
  291. {
  292.   display.clearDisplay();
  293.   display.setTextSize(1);
  294.   display.setTextColor(WHITE);
  295.  
  296.   display.setCursor(0, 0);
  297.   display.print("Token telah");
  298.   display.setCursor(0, 8);
  299.   display.print("ditambahkan sebesar  ");
  300.   display.setCursor(0, 16);
  301.   display.print("30,29 kWh");
  302.   display.display();
  303. }
  304.  
  305. void batal()
  306. {
  307.   display.clearDisplay();
  308.   display.setTextSize(1);
  309.   display.setTextColor(WHITE);
  310.  
  311.   display.setCursor(0, 0);
  312.   display.print("Pembelian dibatalkan");
  313.   display.display();
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement