Advertisement
RuiViana

RCB2

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