Translate

ARDUINO Barcode Scanner / Barcode Reader Isi Ulang Air Otomatis (Automatic Water Filler)

Arduino Barcode Scanner / Barcode Reader Isi Ulang Air Otomatis (Automatic Water Filler)


              Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat dengan memanfaatkan barcode scanner atau barcode reader sebagai salah satu sensornya, jadi sistemnya adalah membaca kode barcode sepanjang 13 digit kemudian 4 digit pertama adalah ID dan 1 digit ke 5 adalah jumlah liter yang dipesan oleh pembeli, untuk digit sisanya tidak dipakai. terdapat sensor water flow untuk mendeteksi apakah aliran yang mengalir sudah sesuai yang dipesan ataukah tidak, kemudian ada relay modul untuk kendali solenoid valve. untuk lebih jelasnya berikut adalah program dan daftar komponennya. 




a. Arduino Mega 




b. USB Host Shield




c. Water Flow Sensor




d. Barcode Scanner / Reader




e. LCD + I2C




f. Modul Relay






g. Program Arduino IDE

#include <usbhid.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <SPI.h>
#include <LiquidCrystal_I2C.h>

int mark;
int aa;
int bb;
int cc;
int dd;
int ee;
int ff;
int gg;
int hh;
int ii;
int jj;
int kk;
int ll;
int mm;
int nn;

int relay = A8;

LiquidCrystal_I2C lcd(0x27, 16, 2);

class MyParser : public HIDReportParser {
  public: 
    MyParser();
    void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
};

MyParser::MyParser() { 
  }

USB          Usb;
USBHub       Hub(&Usb);
HIDUniversal Hid(&Usb);
MyParser     Parser;

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;
int tanda;
int tanda1;
int tanda2;

void setup() {
 
  lcd.begin();
  lcd.clear();
  lcd.noCursor();

  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
 
  pinMode(relay,OUTPUT);
  digitalWrite(relay,HIGH);
 
  Serial.begin( 115200 );
  Serial.println("Start");

  if (Usb.Init() == -1)
    Serial.println("OSC did not start.");

  delay( 200 );

  Hid.SetReportParser(0, &Parser);
}

void loop() {
  Usb.Task(); 
}

void MyParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
  // If error, return
  // I don't know why it starts on 2, I just following the example
  if (buf[2] == 1) return;

  // If empty, return
  // I check on 2 because the previous if check on 2 too
  if (buf[2] == 0) return;

  // Like above, WHY it starts on 2 ?
  // What is the purpose of bit in 0 and 1 ?
  for(uint8_t i = 2; i < 3; i++){
    Serial.print(buf[i]);
    Serial.print(" ");

if(mark == 0){
  if(buf[2] == 30){
    aa = 1;
    }
  if(buf[2] == 31){
    aa = 2;
    }
  if(buf[2] == 32){
    aa = 3;
    }
  if(buf[2] == 33){
    aa = 4;
    }
 if(buf[2] == 34){
    aa = 5;
    }
  if(buf[2] == 35){
    aa = 6;
    }
 if(buf[2] == 36){
    aa = 7;
    }
  if(buf[2] == 37){
    aa = 8;
    }
  if(buf[2] == 38){
    aa = 9;
    }
  if(buf[2] == 39){
    aa = 0;
    }
  if(buf[2] == 40){
    }
  }


if(mark == 1){
  if(buf[2] == 30){
    bb = 1;
    }
  if(buf[2] == 31){
    bb = 2;
    }
  if(buf[2] == 32){
    bb = 3;
    }
  if(buf[2] == 33){
    bb = 4;
    }
 if(buf[2] == 34){
    bb = 5;
    }
  if(buf[2] == 35){
    bb = 6;
    }
 if(buf[2] == 36){
    bb = 7;
    }
  if(buf[2] == 37){
    bb = 8;
    }
  if(buf[2] == 38){
    bb = 9;
    }
  if(buf[2] == 39){
    bb = 0;
    }
  if(buf[2] == 40){
    }
  }


if(mark == 2){
  if(buf[2] == 30){
    cc = 1;
    }
  if(buf[2] == 31){
    cc = 2;
    }
  if(buf[2] == 32){
    cc = 3;
    }
  if(buf[2] == 33){
    cc = 4;
    }
 if(buf[2] == 34){
    cc = 5;
    }
  if(buf[2] == 35){
    cc = 6;
    }
 if(buf[2] == 36){
    cc = 7;
    }
  if(buf[2] == 37){
    cc = 8;
    }
  if(buf[2] == 38){
    cc = 9;
    }
  if(buf[2] == 39){
    cc = 0;
    }
  if(buf[2] == 40){
    }
  }


if(mark == 3){
  if(buf[2] == 30){
    dd = 1;
    }
  if(buf[2] == 31){
    dd = 2;
    }
  if(buf[2] == 32){
    dd = 3;
    }
  if(buf[2] == 33){
    dd = 4;
    }
 if(buf[2] == 34){
    dd = 5;
    }
  if(buf[2] == 35){
    dd = 6;
    }
 if(buf[2] == 36){
    dd = 7;
    }
  if(buf[2] == 37){
    dd = 8;
    }
  if(buf[2] == 38){
    dd = 9;
    }
  if(buf[2] == 39){
    dd = 0;
    }
  if(buf[2] == 40){
    }
  }


if(mark == 4){
  if(buf[2] == 30){
    ee = 1;
    }
  if(buf[2] == 31){
    ee = 2;
    }
  if(buf[2] == 32){
    ee = 3;
    }
  if(buf[2] == 33){
    ee = 4;
    }
 if(buf[2] == 34){
    ee = 5;
    }
  if(buf[2] == 35){
    ee = 6;
    }
 if(buf[2] == 36){
    ee = 7;
    }
  if(buf[2] == 37){
    ee = 8;
    }
  if(buf[2] == 38){
    ee = 9;
    }
  if(buf[2] == 39){
    ee = 0;
    }
  if(buf[2] == 40){
    }
  }


if(mark == 5){
  if(buf[2] == 30){
    ff = 1;
    }
  if(buf[2] == 31){
    ff = 2;
    }
  if(buf[2] == 32){
    ff = 3;
    }
  if(buf[2] == 33){
    ff = 4;
    }
 if(buf[2] == 34){
    ff = 5;
    }
  if(buf[2] == 35){
    ff = 6;
    }
 if(buf[2] == 36){
    ff = 7;
    }
  if(buf[2] == 37){
    ff = 8;
    }
  if(buf[2] == 38){
    ff = 9;
    }
  if(buf[2] == 39){
    ff = 0;
    }
  if(buf[2] == 40){
    }
  }

if(mark == 6){
  if(buf[2] == 30){
    gg = 1;
    }
  if(buf[2] == 31){
    gg = 2;
    }
  if(buf[2] == 32){
    gg = 3;
    }
  if(buf[2] == 33){
    gg = 4;
    }
 if(buf[2] == 34){
    gg = 5;
    }
  if(buf[2] == 35){
    gg = 6;
    }
 if(buf[2] == 36){
    gg = 7;
    }
  if(buf[2] == 37){
    gg = 8;
    }
  if(buf[2] == 38){
    gg = 9;
    }
  if(buf[2] == 39){
    gg = 0;
    }
  if(buf[2] == 40){
    }
  }
 
if(mark == 7){
  if(buf[2] == 30){
    hh = 1;
    }
  if(buf[2] == 31){
    hh = 2;
    }
  if(buf[2] == 32){
    hh = 3;
    }
  if(buf[2] == 33){
    hh = 4;
    }
 if(buf[2] == 34){
    hh = 5;
    }
  if(buf[2] == 35){
    hh = 6;
    }
 if(buf[2] == 36){
    hh = 7;
    }
  if(buf[2] == 37){
    hh = 8;
    }
  if(buf[2] == 38){
    hh = 9;
    }
  if(buf[2] == 39){
    hh = 0;
    }
  if(buf[2] == 40){
    }
  }

if(mark == 8){
  if(buf[2] == 30){
    ii = 1;
    }
  if(buf[2] == 31){
    ii = 2;
    }
  if(buf[2] == 32){
    ii = 3;
    }
  if(buf[2] == 33){
    ii = 4;
    }
 if(buf[2] == 34){
    ii = 5;
    }
  if(buf[2] == 35){
    ii = 6;
    }
 if(buf[2] == 36){
    ii = 7;
    }
  if(buf[2] == 37){
    ii = 8;
    }
  if(buf[2] == 38){
    ii = 9;
    }
  if(buf[2] == 39){
    ii = 0;
    }
  if(buf[2] == 40){
    }
  }

if(mark == 9){
  if(buf[2] == 30){
    jj = 1;
    }
  if(buf[2] == 31){
    jj = 2;
    }
  if(buf[2] == 32){
    jj = 3;
    }
  if(buf[2] == 33){
    jj = 4;
    }
 if(buf[2] == 34){
    jj = 5;
    }
  if(buf[2] == 35){
    jj = 6;
    }
 if(buf[2] == 36){
    jj = 7;
    }
  if(buf[2] == 37){
    jj = 8;
    }
  if(buf[2] == 38){
    jj = 9;
    }
  if(buf[2] == 39){
    jj = 0;
    }
  if(buf[2] == 40){
    }
  }

if(mark == 10){
  if(buf[2] == 30){
    kk = 1;
    }
  if(buf[2] == 31){
    kk = 2;
    }
  if(buf[2] == 32){
    kk = 3;
    }
  if(buf[2] == 33){
    kk = 4;
    }
 if(buf[2] == 34){
    kk = 5;
    }
  if(buf[2] == 35){
    kk = 6;
    }
 if(buf[2] == 36){
    kk = 7;
    }
  if(buf[2] == 37){
    kk = 8;
    }
  if(buf[2] == 38){
    kk = 9;
    }
  if(buf[2] == 39){
    kk = 0;
    }
  if(buf[2] == 40){
    }
  }

if(mark == 11){
  if(buf[2] == 30){
    ll = 1;
    }
  if(buf[2] == 31){
    ll = 2;
    }
  if(buf[2] == 32){
    ll = 3;
    }
  if(buf[2] == 33){
    ll = 4;
    }
 if(buf[2] == 34){
    ll = 5;
    }
  if(buf[2] == 35){
    ll = 6;
    }
 if(buf[2] == 36){
    ll = 7;
    }
  if(buf[2] == 37){
    ll = 8;
    }
  if(buf[2] == 38){
    ll = 9;
    }
  if(buf[2] == 39){
    ll = 0;
    }
  if(buf[2] == 40){
    }
  }


if(mark == 12){
  if(buf[2] == 30){
    mm = 1;
    }
  if(buf[2] == 31){
    mm = 2;
    }
  if(buf[2] == 32){
    mm = 3;
    }
  if(buf[2] == 33){
    mm = 4;
    }
 if(buf[2] == 34){
    mm = 5;
    }
  if(buf[2] == 35){
    mm = 6;
    }
 if(buf[2] == 36){
    mm = 7;
    }
  if(buf[2] == 37){
    mm = 8;
    }
  if(buf[2] == 38){
    mm = 9;
    }
  if(buf[2] == 39){
    mm = 0;
    }
  if(buf[2] == 40){
    }
  }


mark++;        

if(mark > 13){

lcd.setCursor(0,0);
lcd.print(aa);
lcd.print(bb);
lcd.print(cc);
lcd.print(dd);
lcd.print(ee);
lcd.print(ff);
lcd.print(gg);
lcd.print(hh);
lcd.print(ii);
lcd.print(jj);
lcd.print(kk);
lcd.print(ll);
lcd.print(mm);

if((aa == 8)&&(bb == 9)&&(cc == 9)&&(dd == 9)&&(ee == 9)&&(tanda == 0)){
lcd.setCursor(0,1);
lcd.print("ID=");
lcd.print(aa);lcd.print(bb);lcd.print(cc);lcd.print(dd);
lcd.print(" Lt=");
lcd.print(ee);
delay(5000);
lcd.clear();
digitalWrite(relay,LOW);
mulai();
tanda = 1;
}

if((aa == 8)&&(bb == 9)&&(cc == 9)&&(dd == 8)&&(ee == 8)&&(tanda1 == 0)){
lcd.setCursor(0,1);
lcd.print("ID=");
lcd.print(aa);lcd.print(bb);lcd.print(cc);lcd.print(dd);
lcd.print(" Lt=");
lcd.print(ee);
delay(5000);
lcd.clear();
digitalWrite(relay,LOW);
mulai();
tanda1 = 1;
}

if((aa == 8)&&(bb == 9)&&(cc == 9)&&(dd == 2)&&(ee == 2)&&(tanda2 == 0)){
lcd.setCursor(0,1);
lcd.print("ID=");
lcd.print(aa);lcd.print(bb);lcd.print(cc);lcd.print(dd);
lcd.print(" Lt=");
lcd.print(ee);
delay(5000);
lcd.clear();
digitalWrite(relay,LOW);
mulai();
tanda2 = 1;
}

mark = 0;

}

}

}

void mulai(){
 
if((millis() - oldTime) > 1000)
  {

    detachInterrupt(sensorInterrupt);
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    oldTime = millis();
    flowMilliLitres = (flowRate / 60) * 1000;
    totalMilliLitres += flowMilliLitres;
   
/*    
    unsigned int frac;
  
    Serial.print("Flow rate: ");
    Serial.print(int(flowRate));  /
    Serial.print(".");           
    frac = (flowRate - int(flowRate)) * 10;
    Serial.print(frac, DEC) ;    
    Serial.print("L/min");
    Serial.print("  Current Liquid Flowing: ");  
    Serial.print(flowMilliLitres);
    Serial.print("mL/Sec");

    Serial.print("  Output Liquid Quantity: ");         
    Serial.print(totalMilliLitres);
    Serial.println("mL");
*/

    pulseCount = 0;
  
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  } 

lcd.setCursor(0,0);
lcd.print("ml= "); 
lcd.print(totalMilliLitres);
lcd.print("     "); 

lcd.setCursor(0,1);
lcd.print("ml= "); 
lcd.print(ee * 1000);
lcd.print("     ");

if(totalMilliLitres > ee * 1000){
  digitalWrite(relay,HIGH);
  lcd.clear();

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;
 
  return;
}

mulai(); 
}


void pulseCounter()
{
  pulseCount++;
}



h. VIDEO HASILNYA







No comments:

Post a Comment