//-----------SMART OIL STATION-----------------
//-----PT. KARYA MULTI SOLUTION INDONESIA------

#include<SPI.h>
#include<MFRC522.h>
MFRC522 rfid(10, 9);

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

#include <Keypad.h>
const int ROW_NUM = 4;
const int COLUMN_NUM = 4;
String code = "";

char keys[COLUMN_NUM][ROW_NUM] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

byte pin_rows[ROW_NUM] = { 5, 4, 3, 2 };
byte pin_column[COLUMN_NUM] = { 9, 8, 7, 6 };
Keypad keypad = Keypad(makeKeymap(keys), pin_column, pin_rows, COLUMN_NUM, ROW_NUM);

unsigned long volume;

#define pumpOil 12
#define medium 11
#define low 10
#define full 9

volatile unsigned int temp, counter = 0; //encoder

void setup() {
  Serial.begin(9600);
  lcd.init();
  SPI.begin();
  rfid.PCD_Init();

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  attachInterrupt(0, ai0, RISING);
  attachInterrupt(1, ai1, RISING);

  pinMode(pumpOil, OUTPUT);
  pinMode(medium, OUTPUT);
  pinMode(low, OUTPUT);
  pinMode(full, OUTPUT);

  lcd.backlight();
  lcd.setCursor(0, 0); lcd.print("     Welcome to");
  lcd.setCursor(0, 1); lcd.print("Smart Oil Station v1");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Silahkan tempel");
  lcd.setCursor(3, 1);
  lcd.print("kartu anda");
  delay(2000);
  lcd.clear();
}

void loop() {
  InputKeypad();
}

void ReadEncoder(){
  // Send the value of counter
  if ( counter != temp ) {
    Serial.println (counter);
    temp = counter;
  }
}

void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if (digitalRead(3) == LOW) {
    counter++;
  } else {
    counter--;
  }
}
void ai1() {
  // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
  // Check with pin 2 to determine the direction
  if (digitalRead(2) == LOW) {
    counter--;
  } else {
    counter++;
  }
}

void InputKeypad() {
  char key = keypad.getKey();

  if (key) {
    code += key;
    lcd.setCursor(0, 1);
    lcd.print(code);
    delay(100);
  }

  if (key == 'D') {
    if (code.toInt() <= 1500) {
      volume = code.toInt();
    } else {
      lcd.clear();

      lcd.setCursor(0, 0);
      lcd.print("Set Volume:");
    }
    code = "";
  }
}

void InputRfid() {
  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
    return;
  }

  String id;
  id = String(rfid.uid.uidByte[0]) + String(rfid.uid.uidByte[1]) + String(rfid.uid.uidByte[2]) + String(rfid.uid.uidByte[3]);
  Serial.print("ID Anda: ");
  Serial.println(id);

  if (id == "") {
    
  } else if (id == "") {
    
  } else if (id == "") {
    
  }
  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module