Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang fungsinya seperti alat pada SPBU atau pertamini yaitu pengisi air atau BBM otomatis sesuai dengan input liter / mililiter sesuai angka yang telah diinput via keypad. yang membedakan alat ini dengan yang lainnya yaitu terdapat RFID untuk pembelian dan pembayaran air / BBMnya sehingga mirip seperti kartu E-Money yang harus diisi saldo dulu. untuk lebih jelasnya berikut adalah komponen dan programnya.
a. Arduino Mega
b. RFID Reader RC522
c. LCD 20x4 + I2C Module
d. Sensor Water Flow dan Solenoid Valve
e. Keypad 4x4
f. Relay Module
g. Program Arduino IDE
#include <SPI.h>
#include <Keypad.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h> //i2C LCD Library
LiquidCrystal_I2C lcd(0x3F, 20, 4); //library i2c lcd
#define SS_PIN 53
#define RST_PIN 49
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int buzzerm = A0;
int buzzerp = A3;
int relay = 11;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
long passwd = 0;
long isiku;
long kartu1;
long kartu2;
long kartu3;
long kartu4;
int id;
long beli;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,4,3,10}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
byte sensorInterrupt = 0; // 0 = digital pin 2
byte sensorPin = 2;
float calibrationFactor = 4.5;
volatile byte pulseCount;
unsigned int frac;
float flowRate;
unsigned int flowMilliLitres;
int totalMilliLitres;
unsigned long oldTime;
void setup()
{
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
pinMode(buzzerm,OUTPUT);
pinMode(buzzerp,OUTPUT);
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,LOW);
lcd.begin(); //set lcd i2c
lcd.noCursor(); //biar gak ada cursor di lcd
lcd.clear(); //clear lcd
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
// Serial.println("Put your card to the reader...");
// Serial.println();
delay(1000);
}
void loop()
{
lcd.setCursor(0,0);
lcd.print("Pilih Menu");
lcd.setCursor(0,1);
lcd.print("A.Isi B.Bayar");
customKey = customKeypad.getKey();
if(customKey == 'B'){
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Dekatkan Kartu");
delay(3000);
lcd.clear();
isiscan();
lcd.clear();
beli = 0;
bayar();
digitalWrite(relay, LOW);
mulaiisi();
}
if(customKey == 'C'){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Dekatkan Kartu");
delay(3000);
lcd.clear();
cek();
}
if(customKey == 'A'){
lcd.clear();
delay(1000);
passwd = 0;
isi();
lcd.setCursor(0,0);
lcd.print("Dekatkan Kartu");
delay(3000);
lcd.clear();
isiscan();
delay(1000);
isiku = 0;
scan();
}
}
void cek(){
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
// Serial.println("Put your card to the reader...");
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
//return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
//return;
}
//Show UID on serial monitor
//lcd.setCursor(0,0);
// Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
// Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
// Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
// Serial.println();
// Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "99 AD B5 55")
{
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,HIGH);
delay(200);
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,LOW);
lcd.setCursor(0,0);
lcd.print("ID=");
lcd.print(content.substring(1));
lcd.setCursor(0,1);
lcd.print("Saldo=");
lcd.print(kartu1);
lcd.print(" ");
delay(3000);
lcd.clear();
return;
}
if (content.substring(1) == "51 BB 69 1A")
{
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,HIGH);
delay(200);
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,LOW);
lcd.setCursor(0,0);
lcd.print("ID=");
lcd.print(content.substring(1));
lcd.setCursor(0,1);
lcd.print("Saldo=");
lcd.print(kartu2);
lcd.print(" ");
delay(3000);
lcd.clear();
return;
}
if (content.substring(1) == "A3 32 48 83")
{
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,HIGH);
delay(200);
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,LOW);
lcd.setCursor(0,0);
lcd.print("ID=");
lcd.print(content.substring(1));
lcd.setCursor(0,1);
lcd.print("Saldo=");
lcd.print(kartu3);
lcd.print(" ");
delay(3000);
lcd.clear();
return;
}
if (content.substring(1) == "06 5D 01 FA")
{
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,HIGH);
delay(200);
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,LOW);
lcd.setCursor(0,0);
lcd.print("ID=");
lcd.print(content.substring(1));
lcd.setCursor(0,1);
lcd.print("Saldo=");
lcd.print(kartu4);
lcd.print(" ");
delay(3000);
lcd.clear();
return;
}
cek();
}
void isi(){
lcd.setCursor(0,0);
lcd.print("Input Password");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
passwd = passwd * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(passwd);
}
if((customKey == 'D')&&(passwd == 112233)){
lcd.clear();
delay(1000);
customKey == ' ';
return;
}
if(customKey == 'C'){
lcd.clear();
delay(200);
passwd = 0;
}
isi();
}
void isiscan(){
lcd.setCursor(0,0);
lcd.print("Scan Kartu");
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
// Serial.println("Put your card to the reader...");
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
//return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
//return;
}
//Show UID on serial monitor
//lcd.setCursor(0,0);
// Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
// Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
// Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
// Serial.println();
// Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "51 BB 69 1A")
{
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,HIGH);
delay(200);
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,LOW);
lcd.setCursor(0,1);
lcd.print(content.substring(1));
delay(3000);
id = 1;
lcd.clear();
content.substring(1) = "";
return;
}
if (content.substring(1) == "99 AD B5 55")
{
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,HIGH);
delay(200);
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,LOW);
lcd.setCursor(0,1);
lcd.print(content.substring(1));
delay(3000);
id = 2;
lcd.clear();
content.substring(1) = "";
return;
}
if (content.substring(1) == "A3 32 48 83")
{
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,HIGH);
delay(200);
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,LOW);
lcd.setCursor(0,1);
lcd.print(content.substring(1));
delay(3000);
id = 3;
lcd.clear();
content.substring(1) = "";
return;
}
if (content.substring(1) == "06 5D 01 FA")
{
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,HIGH);
delay(200);
digitalWrite(buzzerm,LOW);
digitalWrite(buzzerp,LOW);
lcd.setCursor(0,1);
lcd.print(content.substring(1));
delay(3000);
id = 4;
lcd.clear();
content.substring(1) = "";
return;
}
isiscan();
}
void scan(){
lcd.setCursor(0,0);
lcd.print("Isi Berapa mL?");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
isiku = isiku * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(isiku);
}
if(customKey == 'C'){
lcd.clear();
delay(200);
isiku = 0;
}
if((customKey == 'D')&&(id == 1)){
lcd.clear();
delay(2000);
kartu1 = isiku;
return;
}
if((customKey == 'D')&&(id == 2)){
lcd.clear();
delay(2000);
kartu2 = isiku;
return;
}
if((customKey == 'D')&&(id == 3)){
lcd.clear();
delay(2000);
kartu3 = isiku;
return;
}
if((customKey == 'D')&&(id == 4)){
lcd.clear();
delay(2000);
kartu4 = isiku;
return;
}
scan();
}
void bayar(){
lcd.setCursor(0,0);
lcd.print("Beli Berapa mL ?");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
beli = beli * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(beli);
}
if(customKey == 'C'){
lcd.clear();
delay(200);
beli = 0;
}
if((customKey == 'D')&&(id == 1)){
lcd.clear();
delay(2000);
kartu1 = kartu1 - beli;
if(kartu1 >= 0){
Serial.println(id);
Serial.println(beli);
Serial.println(kartu1);
delay(1000);
lcd.setCursor(0,0);
lcd.print("BERHASIL");
lcd.setCursor(0,1);
lcd.print("SALDO=");
lcd.print(kartu1);
delay(5000);
lcd.clear();
return;
}
if(kartu1 < 0){
kartu1 = kartu1 + beli;
lcd.setCursor(0,0);
lcd.print("SALDO KURANG");
lcd.setCursor(0,1);
lcd.print("SALDO=");
lcd.print(kartu1);
delay(5000);
lcd.clear();
beli = 0;
}
}
if((customKey == 'D')&&(id == 2)){
lcd.clear();
delay(2000);
kartu2 = kartu2 - beli;
if(kartu2 >= 0){
Serial.println(id);
Serial.println(beli);
Serial.println(kartu2);
delay(1000);
lcd.setCursor(0,0);
lcd.print("BERHASIL");
lcd.setCursor(0,1);
lcd.print("SALDO=");
lcd.print(kartu2);
delay(5000);
lcd.clear();
return;
}
if(kartu2 < 0){
kartu2 = kartu2 + beli;
lcd.setCursor(0,0);
lcd.print("SALDO KURANG");
lcd.setCursor(0,1);
lcd.print("SALDO=");
lcd.print(kartu2);
delay(5000);
lcd.clear();
beli = 0;
}
}
if((customKey == 'D')&&(id == 3)){
lcd.clear();
delay(2000);
kartu3 = kartu3 - beli;
if(kartu3 >= 0){
Serial.println(id);
Serial.println(beli);
Serial.println(kartu3);
delay(1000);
lcd.setCursor(0,0);
lcd.print("BERHASIL");
lcd.setCursor(0,1);
lcd.print("SALDO=");
lcd.print(kartu3);
delay(5000);
lcd.clear();
return;
}
if(kartu3 < 0){
kartu3 = kartu3 + beli;
lcd.setCursor(0,0);
lcd.print("SALDO KURANG");
lcd.setCursor(0,1);
lcd.print("SALDO=");
lcd.print(kartu3);
delay(5000);
lcd.clear();
beli = 0;
}
}
if((customKey == 'D')&&(id == 4)){
lcd.clear();
delay(2000);
kartu4 = kartu4 - beli;
if(kartu4 >= 0){
Serial.println(id);
Serial.println(beli);
Serial.println(kartu4);
delay(1000);
lcd.setCursor(0,0);
lcd.print("BERHASIL");
lcd.setCursor(0,1);
lcd.print("SALDO=");
lcd.print(kartu4);
delay(5000);
lcd.clear();
return;
}
if(kartu4 < 0){
kartu4 = kartu4 + beli;
lcd.setCursor(0,0);
lcd.print("SALDO KURANG");
lcd.setCursor(0,1);
lcd.print("SALDO=");
lcd.print(kartu4);
delay(5000);
lcd.clear();
beli = 0;
}
}
bayar();
}
void mulaiisi(){
lcd.setCursor(0,0);
lcd.print("Beli ");
lcd.print(beli);
lcd.print(" mL ");
delay(100);
if((millis() - oldTime) > 1000) // Only process counters once per second
{
detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
// Print the flow rate for this second in litres / minute
//lcd.setCursor(0,3);
//lcd.print("Flow rate: ");
//lcd.print(int(flowRate)); // Print the integer part of the variable
//lcd.print("."); // Print the decimal point
// Determine the fractional part. The 10 multiplier gives us 1 decimal place.
frac = (flowRate - int(flowRate)) * 10;
//lcd.print(frac, DEC) ; // Print the fractional part of the variable
//lcd.print("L/min ");
// Print the number of litres flowed in this second
lcd.setCursor(0,1);
lcd.print("Spd Flow: "); // Output separator
lcd.print(flowMilliLitres);
lcd.print(" mL/Sec ");
// Print the cumulative total of litres flowed since starting
lcd.setCursor(0,2);
lcd.print("ISI: "); // Output separator
lcd.print(totalMilliLitres);
lcd.print(" mL ");
//lcd.setCursor(0,3);
//lcd.print(" ");
// Reset the pulse counter so we can start incrementing again
pulseCount = 0;
// Enable the interrupt again now that we've finished sending output
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
if(totalMilliLitres >= beli){
lcd.clear();
delay(1000);
digitalWrite(relay, HIGH);
return;
}
}
mulaiisi();
}
void pulseCounter()
{
pulseCount++;
}
h. VIDEO HASILNYA
No comments:
Post a Comment