Advertisement
muezza29

beli token v7

Jul 11th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.02 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. void loop(void)
  56. {
  57.   uint8_t success;
  58.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  59.   uint8_t uidLength;                          // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  60.  
  61.   success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  62.  
  63.   if (success)
  64.   {
  65.     String str22 = hexify(uid, uidLength);
  66.     results(str22);
  67.   }
  68. }
  69.  
  70. String hexify(uint8_t u[], byte len)
  71. {
  72.   String out = "";
  73.   for (byte i = 0; i < len; i++)
  74.   {
  75.     String a = String(u[i], HEX);
  76.     a.toUpperCase();
  77.     if (a.length() == 1) a = "0" + a;
  78.     out += "0x" + a + " ";
  79.   }
  80.   out.trim();
  81.   return out;
  82. }
  83.  
  84. void results(String str)
  85. {
  86.   if (str == str2)
  87.   {
  88.     display.clearDisplay();
  89.     display.setTextSize(1);
  90.     display.setTextColor(WHITE);
  91.  
  92.     display.setCursor(0, 0);
  93.     display.print("Pelanggan dikenali");
  94.  
  95.     display.setCursor(0, 8);
  96.     display.print("sebagai :");
  97.  
  98.     display.setCursor(0, 16);
  99.     display.print(" > 1303167045");
  100.     display.display();
  101.  
  102.     Serial.println("\n  1303167045");
  103.     beli();
  104.   }
  105.   else
  106.   {
  107.     display.clearDisplay();
  108.     display.setTextSize(1);
  109.     display.setTextColor(WHITE);
  110.  
  111.     display.setCursor(0, 0);
  112.     display.print("Tag NFC tidak");
  113.  
  114.     display.setCursor(0, 8);
  115.     display.print("dikenali");
  116.  
  117.     display.setCursor(0, 16);
  118.     display.print(" > Bukan pelanggan");
  119.     display.display();
  120.  
  121.     Serial.println("\n  Bukan pelanggan");
  122.   }
  123.   Serial.print("");
  124. }
  125.  
  126. void beli()
  127. {
  128.   delay(2000);
  129.   byte pilihan = 1;
  130.   byte jumlah_pilihan = 4;
  131.   refresh_pilihan_token(pilihan);
  132.  
  133.   uint16_t interval = 5000;
  134.   uint32_t previousMillis = millis();
  135.  
  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.       previousMillis = millis();
  145.     }
  146.  
  147.     if (millis() - previousMillis >=  interval)
  148.     {
  149.       printBeli(pilihan);
  150.       break;
  151.     }
  152.   }
  153. }
  154.  
  155. void refresh_pilihan_token(byte pilihan)
  156. {
  157.   display.clearDisplay();
  158.   display.setTextSize(1);
  159.   display.setTextColor(WHITE);
  160.  
  161.   switch (pilihan)
  162.   {
  163.     case 1:
  164.       display.setCursor(0, 0);
  165.       display.print("> 10K");
  166.       display.setCursor(0, 8);
  167.       display.print("  20K");
  168.       display.setCursor(0, 16);
  169.       display.print("  50K");
  170.       display.setCursor(0, 24);
  171.       display.print("  Batal");
  172.       break;
  173.  
  174.     case 2:
  175.       display.setCursor(0, 0);
  176.       display.print("  10K");
  177.       display.setCursor(0, 8);
  178.       display.print("> 20K");
  179.       display.setCursor(0, 16);
  180.       display.print("  50K");
  181.       display.setCursor(0, 24);
  182.       display.print("  Batal");
  183.       break;
  184.  
  185.     case 3:
  186.       display.setCursor(0, 0);
  187.       display.print("  10K");
  188.       display.setCursor(0, 8);
  189.       display.print("  20K");
  190.       display.setCursor(0, 16);
  191.       display.print("> 50K");
  192.       display.setCursor(0, 24);
  193.       display.print("  Batal");
  194.       break;
  195.  
  196.     case 4:
  197.       display.setCursor(0, 0);
  198.       display.print("  10K");
  199.       display.setCursor(0, 8);
  200.       display.print("  20K");
  201.       display.setCursor(0, 16);
  202.       display.print("  50K");
  203.       display.setCursor(0, 24);
  204.       display.print("> Batal");
  205.       break;
  206.   }
  207.   display.display();
  208. }
  209.  
  210. void printBeli(byte pilih) {
  211.   switch (pilih) {
  212.     case 1:
  213.       token10k();
  214.       break;
  215.  
  216.     case 2:
  217.       token20k();
  218.       break;
  219.  
  220.     case 3:
  221.       token50k();
  222.       break;
  223.  
  224.     case 4:
  225.       batal();
  226.       break;
  227.   }
  228. }
  229.  
  230. bool check_nfc()
  231. {
  232.   uint8_t success;
  233.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  234.   uint8_t uidLength;
  235.   success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  236.  
  237.   if (success)
  238.   {
  239.     String str22 = hexify(uid, uidLength);
  240.     if (str22.equals(str2)) return true;
  241.   }
  242.   return false;
  243. }
  244.  
  245. void token10k()
  246. {
  247.   display.clearDisplay();
  248.   display.setTextSize(1);
  249.   display.setTextColor(WHITE);
  250.  
  251.   display.setCursor(0, 0);
  252.   display.print("Token telah");
  253.   display.setCursor(0, 8);
  254.   display.print("ditambahkan sebesar  ");
  255.   display.setCursor(0, 16);
  256.   display.print("5,0484 kWh");
  257.   display.display();
  258. }
  259.  
  260. void token20k()
  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("11,3588 kWh");
  272.   display.display();
  273. }
  274.  
  275. void token50k()
  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("30,29 kWh");
  287.   display.display();
  288. }
  289.  
  290. void batal()
  291. {
  292.   display.clearDisplay();
  293.   display.setTextSize(1);
  294.   display.setTextColor(WHITE);
  295.  
  296.   display.setCursor(0, 0);
  297.   display.print("Pembelian dibatalkan");
  298.   display.display();
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement