Advertisement
RuiViana

RCB

Mar 24th, 2019
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.94 KB | None | 0 0
  1. #include "Keypad.h"  // Biblioteca para controle do teclado de matrizes.
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  5. //#include <LiquidCrystal.h>
  6. //LiquidCrystal lcd(6, 7, 5, 4, 3, 2);
  7.  
  8. #include <SPI.h>
  9. #include <MFRC522.h>
  10.  
  11. //Pinos Reset e SS modulo MFRC522
  12. #define SS_PIN 10
  13. #define RST_PIN 9
  14. MFRC522 mfrc522(SS_PIN, RST_PIN);
  15.  
  16. MFRC522::MIFARE_Key key;
  17. char compra[30];
  18. float convFloat;
  19. char credito[30];
  20. float debito;
  21. float saldo;
  22. //============================== INICIO DECLARACOES TECLADO ====================================
  23. //composi��o do teclado 4x4
  24. const byte ROWS = 4;  // Quatro linhas
  25. const byte COLS = 4;  // Quatro colunas.
  26. int cont = 0;
  27.  
  28. // Define o Keypad (mapa do circuito do teclado).
  29. char keys[ROWS][COLS] = {
  30.   {'1', '2', '3', 'A'},
  31.   {'4', '5', '6', 'B'},
  32.   {'7', '8', '9', 'C'},
  33.   {'*', '0', '#', 'D'}
  34. };
  35.  
  36. // Conecta o teclado matricial em linha 0, linha 1, linha 2, linha 3 e linha 4 dos pinos do arduino.
  37. byte rowPins[ROWS] = {7, 6, 5, 4};
  38.  
  39. // Conecta o teclado matricial em coluna 0, coluna 1, coluna 2, coluna 3 e coluna 4 do arduino.
  40. byte colPins[COLS] = {3, 2, 14, 15};
  41.  
  42. // Cria um objeto Keypad.
  43. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  44.  
  45. //Variaveis globais
  46. float tecConvFloat;
  47. float tecsaldo;
  48. byte buffer[34];
  49. long lidoTeclado = 0;
  50. long lidoCartao = 0;
  51. long acumulado = 0;
  52. //=============================== FIM DECLARACOES TECLADO =====================================
  53.  
  54. void setup()
  55. {
  56.   Serial.begin(9600);   //Inicia a serial
  57.  
  58.   //=============================== INICIO SETUP TECLADO ======================================
  59.   keypad.addEventListener(keypadEvent);
  60.   keypad.setDebounceTime(100);  //Tempo para digitar o pr�ximo digito
  61.   //================================= FIM SETUP TECLADO =======================================
  62.  
  63.   SPI.begin();      //Inicia  SPI bus
  64.   mfrc522.PCD_Init();   //Inicia MFRC522
  65.  
  66.   //Inicializa o LCD 20x4
  67.   lcd.begin(20, 4);
  68.   tecmensageminicial();
  69.  
  70.   //Prepara chave - padrao de fabrica = FFFFFFFFFFFFh
  71.   for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  72. }
  73.  
  74. void loop()
  75. {
  76.   keypad.getKey();
  77. }
  78.  
  79. // ============================ FIM DO LOOP / INICIO DAS FUNCOES ============================
  80. void mensagem_inicial_cartao()
  81. {
  82.   lcd.clear();
  83.   lcd.print(" Aproxime o seu");
  84.   lcd.setCursor(0, 1);
  85.   lcd.print("cartao do leitor");
  86. }
  87.  
  88.  
  89. void modo_leitura()
  90. {
  91.   mensagem_inicial_cartao();
  92.   int cont = 0;
  93.   while ( ! mfrc522.PICC_IsNewCardPresent())//Aguarda cartao
  94.   {
  95.     lcd.setCursor(0, 2);
  96.     lcd.print("Aguardando 5 seg: ");
  97.     lcd.print(cont);
  98.     if (cont == 6) {
  99.       cont = 0;
  100.       tecmensageminicial();
  101.       break;
  102.     }
  103.     delay(1000);
  104.     cont++;
  105.   }
  106.   if ( ! mfrc522.PICC_ReadCardSerial())
  107.   {
  108.     return;
  109.   }
  110.   //Mostra UID na serial
  111.   //lcd.clear();
  112.   //lcd.print("UID do Cartao:");    //Dump UID
  113.   for (byte i = 0; i < mfrc522.uid.size; i++)
  114.   {
  115.     //lcd.setCursor(cont, 1);
  116.     //lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? "0" : " ");
  117.     //lcd.setCursor(cont, 2);
  118.     //lcd.print(mfrc522.uid.uidByte[i], HEX);
  119.     //cont =  cont + 2;
  120.   }
  121.   delay(2000);
  122.   //Obtem os dados do setor 0, bloco 1 = Compra
  123.   byte sector         = 0;
  124.   byte blockAddr      = 1;
  125.   byte trailerBlock   = 3;
  126.   byte status;
  127.   byte buffer[18];
  128.   byte size = sizeof(buffer);
  129.  
  130.   //Autenticacao usando chave A
  131.   status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  132.   if (status != MFRC522::STATUS_OK)
  133.   {
  134.     //lcd.clear();
  135.     //lcd.setCursor(0, 1);
  136.     //lcd.print("PCD_Authenticate() failed:");
  137.     //lcd.setCursor(0, 2);
  138.     //lcd.print(mfrc522.GetStatusCodeName(status));
  139.     return;
  140.   }
  141.   status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
  142.   if (status != MFRC522::STATUS_OK)
  143.   {
  144.     //lcd.clear();
  145.     //lcd.setCursor(0, 1);
  146.     //lcd.print("MIFARE_Read() failed:");
  147.     //lcd.setCursor(0, 2);
  148.     //lcd.print(mfrc522.GetStatusCodeName(status));
  149.   }
  150.   //Transfere os dados byte armazenados no BUFFER para char em COMPRA
  151.   for (byte i = 0; i < 16; i++)
  152.   {
  153.     compra[i] = buffer[i];
  154.   }
  155.   convFloat = atof (compra); //Converte os dados char em float
  156.   lidoCartao = (long)convFloat;
  157.   // Halt PICC
  158.   mfrc522.PICC_HaltA();
  159.   // Stop encryption on PCD
  160.   mfrc522.PCD_StopCrypto1();
  161.   tecmensageminicial();
  162. }
  163. //-------------------------------------------------------------
  164. void modo_gravacao()
  165. {
  166.   mensagem_inicial_cartao();
  167.   //Aguarda cartao
  168.   int cont = 0;
  169.   while ( ! mfrc522.PICC_IsNewCardPresent()) {
  170.     lcd.setCursor(0, 2);
  171.     lcd.print("Aguardando 5 seg: ");
  172.     lcd.print(cont);
  173.     if (cont == 6) {
  174.       cont = 0;
  175.       tecmensageminicial();
  176.       break;
  177.     }
  178.     delay(1000);
  179.     cont++;
  180.   }
  181.   if ( ! mfrc522.PICC_ReadCardSerial())    return;
  182.  
  183.   //Mostra UID na serial
  184.   //lcd.clear();
  185.   //lcd.print("UID do Cartao:");    //Dump UID
  186.   //int cont;
  187.   for (byte i = 0; i < mfrc522.uid.size; i++)
  188.   {
  189.     //lcd.setCursor(cont, 1);
  190.     //lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? "0" : " ");
  191.     //lcd.setCursor(cont, 2);
  192.     //lcd.print(mfrc522.uid.uidByte[i], HEX);
  193.     //cont =  cont + 2;
  194.   }
  195.   //Mostra o tipo do cartao
  196.   byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  197.   //lcd.setCursor(0, 3);
  198.   //lcd.print(mfrc522.PICC_GetTypeName(piccType));
  199.   //delay(2000);
  200.   byte block;
  201.   byte status, len;
  202.  
  203.   block = 1;
  204.   //Autenticacao usando chave A
  205.   status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  206.   if (status != MFRC522::STATUS_OK) {
  207.     //lcd.clear();
  208.     //lcd.setCursor(0, 0);
  209.     //lcd.print("PCD_Authent.()failed");
  210.     //lcd.setCursor(0, 1);
  211.     //lcd.print(mfrc522.GetStatusCodeName(status));
  212.     return;
  213.   }
  214.  
  215.   //Grava no bloco 1
  216.   status = mfrc522.MIFARE_Write(block, buffer, 16);
  217.   if (status != MFRC522::STATUS_OK) {
  218.     //lcd.clear();
  219.     //lcd.setCursor(0, 0);
  220.     //lcd.print("MIFARE_Write()failed");
  221.     //lcd.setCursor(0, 1);
  222.     //lcd.println(mfrc522.GetStatusCodeName(status));
  223.     return;
  224.   }
  225.   else
  226.   {
  227.     lcd.clear();
  228.     lcd.setCursor(0, 1);
  229.     lcd.print("Gravado com Sucesso!");
  230.   }
  231.  
  232.   mfrc522.PICC_HaltA(); // Halt PICC
  233.   mfrc522.PCD_StopCrypto1();  // Stop encryption on PCD
  234.   delay(1000);
  235.   tecmensageminicial();
  236. }
  237.  
  238. //====================== INICIO FUNCAO MENSAGEM INICIAL TECLADO ===============================
  239. void tecmensageminicial() {
  240.   lcd.clear();                  //Limpa do buffer do LCD
  241.   lcd.setCursor(0, 0);
  242.   lcd.print("Digite    A-Apagar");
  243.   lcd.setCursor(0, 1);
  244.   lcd.print("valor     B-Banco");
  245.   lcd.setCursor(0, 2);
  246.   lcd.print(" e/ou     C-Credito");
  247.   lcd.setCursor(0, 3);
  248.   lcd.print("opcao     D-Debito");
  249. }//========================== FIM FUNCAO MENSAGEM INICIAL TECLADO =============================
  250.  
  251. //================================ INICIO FUNCAO TECLADO ======================================
  252. void keypadEvent(KeypadEvent eKey)
  253. {
  254.   switch (keypad.getState())
  255.   { // Condicao switch...
  256.     case PRESSED://Se precionado algum botao... Aparecera no Serial Monitor o valor Digitado
  257.       if (isDigit(eKey)) // testa se o valor E um DIGITO/NUMERO
  258.       {
  259.         if (cont == 0) {
  260.           lcd.clear();
  261.           lcd.setCursor(0, 0);
  262.           lcd.print("Digitando valor");
  263.           lcd.setCursor(0, 1);
  264.           lcd.print("R$: ");
  265.         }
  266.         Serial.print (eKey);  //Armazena numero digitado na variavel eKey.
  267.         lcd.setCursor(4 + cont, 1);
  268.         lcd.print(eKey);
  269.         Serial.println(eKey);
  270.         isDigit(eKey);
  271.         buffer[cont] = eKey;
  272.         credito[cont] = eKey;
  273.         tecConvFloat = atof (credito);
  274.         cont = cont + 1;
  275.         Serial.print("Testando a conversao teste: ");
  276.         Serial.println(tecConvFloat);
  277.         lidoTeclado = (long)tecConvFloat;
  278.  
  279.       }
  280.       else
  281.       {
  282.         switch (eKey)
  283.         {
  284.             tecConvFloat = 0;
  285.           case 'A':
  286.             lcd.clear();
  287.             lcd.setCursor(0, 1);
  288.             lcd.print("  LIMPANDO TELA...");
  289.             delay(2000);
  290.             eKey = "";
  291.             tecmensageminicial();
  292.             cont = 0;
  293.             break;
  294.  
  295.           case 'B':
  296.             modo_leitura();
  297.             saldoCartao();
  298.             Serial.print("\nsaldo anterior no cartao: ");
  299.             Serial.println(saldo);
  300.             delay(2000);
  301.             tecmensageminicial();
  302.             cont = 0;
  303.             break;
  304.  
  305.           case 'C':
  306.             /*DESENVOLVER FORMULA QUE VERIFICA SALDO E ADICIONA CREDITO
  307.               =>Verificar saldo existente no cartao
  308.               =>SOMAR com o valor digitado
  309.               =>Gravar o novo valor/saldo*/
  310.  
  311.             modo_leitura();
  312.             Serial.print("\nValor no BUFFER NA LEITURA do catao: ");
  313.             Serial.println(compra);//buffer[34]
  314.  
  315.  
  316.             saldoCartao();
  317.             Serial.print("saldo anterior no cartao: ");
  318.             Serial.println(saldo);
  319.  
  320.  
  321.             acumulado = lidoCartao + lidoTeclado;
  322.             Serial.print("Saldo Final = ");
  323.             Serial.println(acumulado);
  324.  
  325.  
  326.             Serial.print("Valor no BUFFER para gravar no catao: ");
  327.             Serial.println(acumulado);
  328.             ltoa(acumulado, buffer, 10);
  329.             for (int i = 0; i < 4 ; i++)
  330.             {
  331.               Serial.println(buffer[i], HEX);
  332.             }
  333.             modo_gravacao();
  334.  
  335.             acumulado = 0;
  336.             lidoCartao = 0;
  337.             lidoTeclado = 0;
  338.             convFloat = 0;
  339.             tecConvFloat = 0;
  340.             saldo = 0;
  341.             cont = 0;
  342.             break;
  343.  
  344.           case 'D':
  345.             //DESENVOLVER FORMULA QUE VERIFICA SE EXISTE SALDO SUFICIENTE
  346.             //=>Verificar saldo existente no cartao
  347.             //=>SUBTRAIR pelo valor digitado
  348.             //=>Gravar o novo saldo
  349.  
  350.             if (tecsaldo < tecConvFloat) {
  351.               lcd.clear();
  352.               lcd.setCursor(0, 0);
  353.               lcd.print("SLD.INSUFICIENTE");
  354.               lcd.setCursor(0, 1);
  355.               lcd.print("TRANS. ABORTADA");
  356.               delay(2000);
  357.               lcd.clear();
  358.               lcd.setCursor(0, 0);
  359.               lcd.print("  SALDO ATUAL");
  360.               lcd.print("  R$: ");
  361.               lcd.print(tecsaldo);
  362.             }
  363.             else
  364.             {
  365.               //DESENVOLVER FORMULA DO CREDITO RESTANTE
  366.             }
  367.             modo_leitura();
  368.             saldoCartao();
  369.             delay(2000);
  370.             tecmensageminicial();
  371.             break;
  372.         }
  373.       }
  374.   }
  375. }
  376. //===================================== FIM FUNCAO TECLADO ===================================
  377. void limpaBuffer()
  378. {
  379.   for (byte i = 0; i < 34; i++) buffer[i] = ' ';
  380. }
  381. //-------------------------------------------------------------------
  382. void saldoCartao() {
  383.   saldo = convFloat;
  384.   lcd.clear();
  385.   lcd.setCursor(0, 0);
  386.   lcd.print(" CONSULTA DE SALDO");
  387.   lcd.setCursor(1, 2);
  388.   lcd.print("Saldo R$: ");
  389.   lcd.print(saldo);
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement