Advertisement
RuiViana

RFID_Salva_Float.ino

Sep 29th, 2018
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.28 KB | None | 0 0
  1. //=== SOLENÓIDE E TRAVA DO CARTÃO
  2. #define solenoidPin    7
  3. #define cardLockerPin  5
  4. #include <Wire.h>
  5. #include <LiquidCrystal_I2C.h>
  6. // LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Inicializa o display no endereco 0x27
  7. LiquidCrystal_I2C lcd(0x38,  2, 1, 0, 7, 6, 5, 4, 3, POSITIVE);       // Set the LCD I2C address   Meu LCD
  8. #include <SPI.h>
  9. #include <MFRC522.h>
  10. #define SS_PIN  10
  11. #define RST_PIN 9
  12. byte    nuidPICC[4];
  13. MFRC522 rfid(SS_PIN, RST_PIN);
  14. byte  sensorInterrupt     = 0, flowMeterSensorPin  = 2;
  15. float calibrationFactor = 4.5, flowRate;
  16. volatile byte pulseCount;
  17. unsigned int  flowMilliLitres;
  18. unsigned long totalMilliLitres,  oldTime;
  19. byte  saldoLidoRFID[16];
  20. float  saldo      = 0.00;
  21. word  lastMl      = 0;
  22. byte  servindo    = 0, fechouConta = 1;
  23. unsigned int timerParado = 0;
  24.  
  25. long meuSaldo = 0;
  26.  
  27. MFRC522::MIFARE_Key key;
  28.  
  29. byte block;
  30. byte buffer1[18];
  31. byte len;
  32. MFRC522::StatusCode status;
  33.  
  34.  
  35. //======================================= VOID SETUP =======================================
  36. void setup()
  37. {
  38.   Serial.begin(9600);
  39.   lcd.begin (16, 2);
  40.   lcd.setCursor(0, 0);
  41.   lcd.setBacklight(HIGH);
  42.   lcd.print("INICIALIZANDO");
  43.   lcd.setCursor(0, 1);
  44.   lcd.print("O SISTEMA");
  45.   pinMode(solenoidPin,    OUTPUT);
  46.   pinMode(cardLockerPin,  OUTPUT);
  47.   digitalWrite(solenoidPin,   LOW);
  48.   digitalWrite(cardLockerPin, LOW);
  49.   pinMode(flowMeterSensorPin,      INPUT);  
  50.   digitalWrite(flowMeterSensorPin, HIGH);
  51.   pulseCount        = 0;
  52.   flowRate          = 0.0;
  53.   flowMilliLitres   = 0;
  54.   totalMilliLitres  = 0;
  55.   oldTime           = 0;
  56.   SPI.begin();
  57.   rfid.PCD_Init();
  58.   delay(1000);
  59.   lcd.clear();
  60.   attachInterrupt(digitalPinToInterrupt(flowMeterSensorPin), pulseCounter, FALLING);
  61. }
  62. //--------------------------------------- VOID SETUP ---------------------------------------
  63. void loop()
  64. {
  65.   if (!rfid.PICC_IsNewCardPresent())
  66.   { //                                                                         // === 1. EXIBE "INSIRA SEU CARTÃO" NO DISPLAY LCD
  67.     lcd.setCursor(0, 0);
  68.     lcd.print("SISTEMA TAPHERR ");
  69.     lcd.setCursor(0, 1);
  70.     lcd.print(" INSIRA CARTAO  ");
  71.     return;
  72.   } //                                                                        // --- 1. EXIBE "INSIRA SEU CARTÃO" NO DISPLAY LCD
  73.   if (!rfid.PICC_ReadCardSerial())
  74.   { //                                                                        // TEM CARTÃO. ESSE CARTÃO É VÁLIDO?
  75.     lcd.clear();
  76.     lcd.print("ERRO NA LEITURA ");
  77.     lcd.setCursor(0, 1);
  78.     lcd.print("RETIRE O CARTAO ");
  79.     delay(3000);
  80.     lcd.clear();
  81.     return;
  82.   } //                                                                          // NÃO é cartao valido
  83.   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  84.   if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&                             // CARTÃO É DO TIPO CORRETO?
  85.       piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  86.       piccType != MFRC522::PICC_TYPE_MIFARE_4K)
  87.   {
  88.     lcd.clear();
  89.     lcd.print(F("TIPO CARTAO INVALIDO"));
  90.     delay(3000);
  91.     lcd.clear();
  92.     return; // CARTÃO NÃO É DO TIPO CORRETO
  93.   }
  94.   for (byte i = 0; i < 4; i++)                                                    // GRAVA A UID NO ARRAY
  95.     nuidPICC[i] = rfid.uid.uidByte[i];
  96.   //---------------------------------------------------------------------------- GET FIRST NAME
  97.   lcd.clear();
  98.  
  99.   for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  100.  
  101.   rfid.PICC_DumpDetailsToSerial(&(rfid.uid)); //dump some details about the card
  102.  
  103.   block = 4;
  104.   len = 18;
  105.   status = rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(rfid.uid)); //line 834 of MFRC522.cpp file
  106.   if (status != MFRC522::STATUS_OK)
  107.   {
  108.     lcd.print("FALHA AUTENTICAC");
  109.     Serial.println(rfid.GetStatusCodeName(status));
  110.     return;
  111.   }
  112.   status = rfid.MIFARE_Read(block, buffer1, &len);
  113.   if (status != MFRC522::STATUS_OK)
  114.   {
  115.     lcd.clear();
  116.     lcd.print("ERRO LEITURA AUT");
  117.     return;
  118.   }
  119.  
  120.   for (uint8_t i = 0; i < 4; i++) {
  121.     saldoLidoRFID[i] = (buffer1[i]);
  122.   }
  123.  
  124.   for (int i = 3; i > -1; i--)
  125.   {
  126.     meuSaldo = meuSaldo << 8;
  127.     meuSaldo = meuSaldo + (long)saldoLidoRFID[i];
  128.   }
  129.   saldo = (float)meuSaldo / 100;
  130.  
  131.   //    rfid.PICC_HaltA();
  132.   //    rfid.PCD_StopCrypto1();
  133.   lcd.print("SALDO: R$ ");
  134.   lcd.print(saldo, 2);
  135.   delay(2000);
  136.   lcd.clear();
  137.  
  138.   // ---------------------------------------------------------- 3.SALDO NEGATIVO?
  139.   fechouConta = 0;                                               // ABRIU A CONTA
  140.   while (!fechouConta)
  141.   {
  142.     if ((millis() - oldTime) > 100)                                                   // Only process counters each 100ms!!!!!!!!!!!!!!!!!
  143.     {
  144.       detachInterrupt(sensorInterrupt);
  145.       flowRate = ((100.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
  146.       oldTime = millis();
  147.       flowMilliLitres = (flowRate / 60) * 100;
  148.       totalMilliLitres += flowMilliLitres;
  149.       if (lastMl != totalMilliLitres)                                                 // ===> CLIENTE ESTÁ SE SERVINDO
  150.       {
  151.         fechouConta = 0;
  152.         lastMl = totalMilliLitres;
  153.         saldo = saldo - lastMl * 0.0023;
  154.         if (saldo < 0.01) {
  155.           fimDeOperacao();
  156.         }
  157.       }
  158.       else if (!fechouConta) {
  159.         timerParado++;
  160.       }                                                               // else do if(lastMl != totalMilliLitres
  161.       if (!fechouConta)
  162.       {
  163.         lcd.setCursor(0, 0);
  164.         lcd.print("CONSUMO: ");
  165.         lcd.print(totalMilliLitres);
  166.         lcd.print("ml     ");
  167.         lcd.setCursor(0, 1);
  168.         lcd.print("SALDO: R$ ");
  169.         lcd.print(saldo, 2);
  170.         if (timerParado > 40) {
  171.           fimDeOperacao();
  172.         }
  173.       }
  174.       else
  175.       {
  176.         lcd.setCursor(0, 0);
  177.         lcd.print("SIRVA-SE.       ");
  178.         lcd.setCursor(0, 1);
  179.         lcd.print("SALDO: R$ ");
  180.         lcd.print(saldo, 2);
  181.       }
  182.       pulseCount = 0;
  183.       attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  184.     }                                                                                   // if((millis() - oldTime) > 100)
  185.   }                                                                                     // while(!fechouConta)
  186. }
  187. //--------------------------------------------------------------------------------------
  188. void fimDeOperacao()                                                                    // GRAVAR SALDO
  189. {
  190.   meuSaldo = (long)(21.69 * 100);                                 // DEBUG
  191.  
  192.   for (int i = 0; i < 4; i++)
  193.   {
  194.     buffer1[i] = (buffer1[i] << 8) + (meuSaldo & 0xFF);
  195.     meuSaldo = meuSaldo >> 8;
  196.   }
  197.  
  198.   // Serial.println(F("Authenticating using key A..."));
  199.   status = rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(rfid.uid));
  200.   if (status != MFRC522::STATUS_OK)
  201.   {
  202.     Serial.print(F("PCD_Authenticate() failed: "));
  203.     Serial.println(rfid.GetStatusCodeName(status));
  204.     return;
  205.   }
  206.   //                                                                                      // Write block
  207.   status = rfid.MIFARE_Write(block, buffer1, 16);
  208.   if (status != MFRC522::STATUS_OK)
  209.   {
  210.     Serial.print(F("MIFARE_Write() failed: "));
  211.     Serial.println(rfid.GetStatusCodeName(status));
  212.     return;
  213.   }
  214.   else
  215.     Serial.println(F("MIFARE_Write() success: "));
  216.   block = 5;
  217.   //Serial.println(F("Authenticating using key A..."));
  218.   status = rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(rfid.uid));
  219.   if (status != MFRC522::STATUS_OK)
  220.   {
  221.     Serial.print(F("PCD_Authenticate() failed: "));
  222.     Serial.println(rfid.GetStatusCodeName(status));
  223.     return;
  224.   }
  225.   //                                                                                        // Write block
  226.   status = rfid.MIFARE_Write(block, &buffer1[16], 16);
  227.   if (status != MFRC522::STATUS_OK)
  228.   {
  229.     Serial.print(F("MIFARE_Write() failed: "));
  230.     Serial.println(rfid.GetStatusCodeName(status));
  231.     return;
  232.   }
  233.   else
  234.     Serial.println(F("MIFARE_Write() success: "));
  235.   Serial.println(" ");
  236.   rfid.PICC_HaltA();                                                          // Halt PICC
  237.   rfid.PCD_StopCrypto1();                                                     // Stop encryption on PCD
  238.   //                                                                          // FIM GRAVAR SALDO
  239.   fechouConta = 1;
  240.   timerParado = 0;
  241.   totalMilliLitres = 0;
  242.   attachInterrupt(digitalPinToInterrupt(flowMeterSensorPin), pulseCounter, FALLING);
  243.   digitalWrite(solenoidPin,   HIGH);                                            // FECHA SOLENOIDE
  244.   digitalWrite(cardLockerPin, LOW);                                             // ABRE TRAVA DO CARTÃO
  245.   for (int i = 0; i < 4; i++)                                                   // PISCA SALDO
  246.   {
  247.     lcd.setCursor(0, 1);
  248.     lcd.print("                ");
  249.     delay(200);
  250.     lcd.setCursor(0, 1);
  251.     lcd.print("SALDO: R$ ");
  252.     lcd.print(saldo, 2);
  253.     delay(200);
  254.   }
  255.   saldo       = 0;
  256.   delay(500);
  257.   lcd.setCursor(0, 0);
  258.   lcd.print  ("MUITO OBRIGADO  ");
  259.   lcd.setCursor(0, 1);
  260.   lcd.print  ("RETIRE O CARTAO ");
  261.   delay(3000);
  262.   lcd.clear();
  263. }
  264. //-------------------------------------------------------------------------------------------
  265. void pulseCounter()
  266. {
  267.   pulseCount++;
  268.   fechouConta = 0;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement