Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=== SOLENÓIDE E TRAVA DO CARTÃO
- #define solenoidPin 7
- #define cardLockerPin 5
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- // LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Inicializa o display no endereco 0x27
- LiquidCrystal_I2C lcd(0x38, 2, 1, 0, 7, 6, 5, 4, 3, POSITIVE); // Set the LCD I2C address Meu LCD
- #include <SPI.h>
- #include <MFRC522.h>
- #define SS_PIN 10
- #define RST_PIN 9
- byte nuidPICC[4];
- MFRC522 rfid(SS_PIN, RST_PIN);
- byte sensorInterrupt = 0, flowMeterSensorPin = 2;
- float calibrationFactor = 4.5, flowRate;
- volatile byte pulseCount;
- unsigned int flowMilliLitres;
- unsigned long totalMilliLitres, oldTime;
- byte saldoLidoRFID[16];
- float saldo = 0.00;
- word lastMl = 0;
- byte servindo = 0, fechouConta = 1;
- unsigned int timerParado = 0;
- long meuSaldo = 0;
- MFRC522::MIFARE_Key key;
- byte block;
- byte buffer1[18];
- byte len;
- MFRC522::StatusCode status;
- //======================================= VOID SETUP =======================================
- void setup()
- {
- Serial.begin(9600);
- lcd.begin (16, 2);
- lcd.setCursor(0, 0);
- lcd.setBacklight(HIGH);
- lcd.print("INICIALIZANDO");
- lcd.setCursor(0, 1);
- lcd.print("O SISTEMA");
- pinMode(solenoidPin, OUTPUT);
- pinMode(cardLockerPin, OUTPUT);
- digitalWrite(solenoidPin, LOW);
- digitalWrite(cardLockerPin, LOW);
- pinMode(flowMeterSensorPin, INPUT);
- digitalWrite(flowMeterSensorPin, HIGH);
- pulseCount = 0;
- flowRate = 0.0;
- flowMilliLitres = 0;
- totalMilliLitres = 0;
- oldTime = 0;
- SPI.begin();
- rfid.PCD_Init();
- delay(1000);
- lcd.clear();
- attachInterrupt(digitalPinToInterrupt(flowMeterSensorPin), pulseCounter, FALLING);
- }
- //--------------------------------------- VOID SETUP ---------------------------------------
- void loop()
- {
- if (!rfid.PICC_IsNewCardPresent())
- { // // === 1. EXIBE "INSIRA SEU CARTÃO" NO DISPLAY LCD
- lcd.setCursor(0, 0);
- lcd.print("SISTEMA TAPHERR ");
- lcd.setCursor(0, 1);
- lcd.print(" INSIRA CARTAO ");
- return;
- } // // --- 1. EXIBE "INSIRA SEU CARTÃO" NO DISPLAY LCD
- if (!rfid.PICC_ReadCardSerial())
- { // // TEM CARTÃO. ESSE CARTÃO É VÁLIDO?
- lcd.clear();
- lcd.print("ERRO NA LEITURA ");
- lcd.setCursor(0, 1);
- lcd.print("RETIRE O CARTAO ");
- delay(3000);
- lcd.clear();
- return;
- } // // NÃO é cartao valido
- MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
- if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && // CARTÃO É DO TIPO CORRETO?
- piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
- piccType != MFRC522::PICC_TYPE_MIFARE_4K)
- {
- lcd.clear();
- lcd.print(F("TIPO CARTAO INVALIDO"));
- delay(3000);
- lcd.clear();
- return; // CARTÃO NÃO É DO TIPO CORRETO
- }
- for (byte i = 0; i < 4; i++) // GRAVA A UID NO ARRAY
- nuidPICC[i] = rfid.uid.uidByte[i];
- //---------------------------------------------------------------------------- GET FIRST NAME
- lcd.clear();
- for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
- rfid.PICC_DumpDetailsToSerial(&(rfid.uid)); //dump some details about the card
- block = 4;
- len = 18;
- status = rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(rfid.uid)); //line 834 of MFRC522.cpp file
- if (status != MFRC522::STATUS_OK)
- {
- lcd.print("FALHA AUTENTICAC");
- Serial.println(rfid.GetStatusCodeName(status));
- return;
- }
- status = rfid.MIFARE_Read(block, buffer1, &len);
- if (status != MFRC522::STATUS_OK)
- {
- lcd.clear();
- lcd.print("ERRO LEITURA AUT");
- return;
- }
- for (uint8_t i = 0; i < 4; i++) {
- saldoLidoRFID[i] = (buffer1[i]);
- }
- for (int i = 3; i > -1; i--)
- {
- meuSaldo = meuSaldo << 8;
- meuSaldo = meuSaldo + (long)saldoLidoRFID[i];
- }
- saldo = (float)meuSaldo / 100;
- // rfid.PICC_HaltA();
- // rfid.PCD_StopCrypto1();
- lcd.print("SALDO: R$ ");
- lcd.print(saldo, 2);
- delay(2000);
- lcd.clear();
- // ---------------------------------------------------------- 3.SALDO NEGATIVO?
- fechouConta = 0; // ABRIU A CONTA
- while (!fechouConta)
- {
- if ((millis() - oldTime) > 100) // Only process counters each 100ms!!!!!!!!!!!!!!!!!
- {
- detachInterrupt(sensorInterrupt);
- flowRate = ((100.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
- oldTime = millis();
- flowMilliLitres = (flowRate / 60) * 100;
- totalMilliLitres += flowMilliLitres;
- if (lastMl != totalMilliLitres) // ===> CLIENTE ESTÁ SE SERVINDO
- {
- fechouConta = 0;
- lastMl = totalMilliLitres;
- saldo = saldo - lastMl * 0.0023;
- if (saldo < 0.01) {
- fimDeOperacao();
- }
- }
- else if (!fechouConta) {
- timerParado++;
- } // else do if(lastMl != totalMilliLitres
- if (!fechouConta)
- {
- lcd.setCursor(0, 0);
- lcd.print("CONSUMO: ");
- lcd.print(totalMilliLitres);
- lcd.print("ml ");
- lcd.setCursor(0, 1);
- lcd.print("SALDO: R$ ");
- lcd.print(saldo, 2);
- if (timerParado > 40) {
- fimDeOperacao();
- }
- }
- else
- {
- lcd.setCursor(0, 0);
- lcd.print("SIRVA-SE. ");
- lcd.setCursor(0, 1);
- lcd.print("SALDO: R$ ");
- lcd.print(saldo, 2);
- }
- pulseCount = 0;
- attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
- } // if((millis() - oldTime) > 100)
- } // while(!fechouConta)
- }
- //--------------------------------------------------------------------------------------
- void fimDeOperacao() // GRAVAR SALDO
- {
- meuSaldo = (long)(21.69 * 100); // DEBUG
- for (int i = 0; i < 4; i++)
- {
- buffer1[i] = (buffer1[i] << 8) + (meuSaldo & 0xFF);
- meuSaldo = meuSaldo >> 8;
- }
- // Serial.println(F("Authenticating using key A..."));
- status = rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(rfid.uid));
- if (status != MFRC522::STATUS_OK)
- {
- Serial.print(F("PCD_Authenticate() failed: "));
- Serial.println(rfid.GetStatusCodeName(status));
- return;
- }
- // // Write block
- status = rfid.MIFARE_Write(block, buffer1, 16);
- if (status != MFRC522::STATUS_OK)
- {
- Serial.print(F("MIFARE_Write() failed: "));
- Serial.println(rfid.GetStatusCodeName(status));
- return;
- }
- else
- Serial.println(F("MIFARE_Write() success: "));
- block = 5;
- //Serial.println(F("Authenticating using key A..."));
- status = rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(rfid.uid));
- if (status != MFRC522::STATUS_OK)
- {
- Serial.print(F("PCD_Authenticate() failed: "));
- Serial.println(rfid.GetStatusCodeName(status));
- return;
- }
- // // Write block
- status = rfid.MIFARE_Write(block, &buffer1[16], 16);
- if (status != MFRC522::STATUS_OK)
- {
- Serial.print(F("MIFARE_Write() failed: "));
- Serial.println(rfid.GetStatusCodeName(status));
- return;
- }
- else
- Serial.println(F("MIFARE_Write() success: "));
- Serial.println(" ");
- rfid.PICC_HaltA(); // Halt PICC
- rfid.PCD_StopCrypto1(); // Stop encryption on PCD
- // // FIM GRAVAR SALDO
- fechouConta = 1;
- timerParado = 0;
- totalMilliLitres = 0;
- attachInterrupt(digitalPinToInterrupt(flowMeterSensorPin), pulseCounter, FALLING);
- digitalWrite(solenoidPin, HIGH); // FECHA SOLENOIDE
- digitalWrite(cardLockerPin, LOW); // ABRE TRAVA DO CARTÃO
- for (int i = 0; i < 4; i++) // PISCA SALDO
- {
- lcd.setCursor(0, 1);
- lcd.print(" ");
- delay(200);
- lcd.setCursor(0, 1);
- lcd.print("SALDO: R$ ");
- lcd.print(saldo, 2);
- delay(200);
- }
- saldo = 0;
- delay(500);
- lcd.setCursor(0, 0);
- lcd.print ("MUITO OBRIGADO ");
- lcd.setCursor(0, 1);
- lcd.print ("RETIRE O CARTAO ");
- delay(3000);
- lcd.clear();
- }
- //-------------------------------------------------------------------------------------------
- void pulseCounter()
- {
- pulseCount++;
- fechouConta = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement