Translate

Membuat Alat Penghitung Benda / Barang (Counter Machine) Menggunakan Arduino dan sensor jarak ultrasonic HC-SRF04

Membuat Alat Penghitung Benda / Barang (Counter Machine) Menggunakan Arduino dan sensor jarak ultrasonic HC-SRF04


            Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang fungsinya untuk menghitung benda atau barang secara otomatis menggunakan Arduino dan sensor jarak untrasonic HC-SRF04. jadi alat ini hanya bertugas untuk menghitung barang yang lewat dengan jarak benda ke sensor kurang dari 20 cm namun bisa diatur pada program. Hasil counter ditampilkan ke LCD agar mengetahui jumlah totalnya, terdapat 3 buah tombol untuk reset, start counter dan stop counter atau pause. untuk lebih jelasnya berikut adalah skema dan programnya.



a. Arduino Uno 




b. Sensor Ultrasonic Jarak HC-SRF04

  


c. LCD 16x2 + I2C





d. Program Arduino IDE

#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
#include <SoftwareSerial.h>
#define trigPin A2
#define echoPin A3

#include <LiquidCrystal.h>
//pin lcd rs,e,d4,d5,d6,d7
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);

//pin printer thermal TX,RX
SoftwareSerial Thermal(2, 3);

//definisi variabel
int mark = 0;
int cacah;
int sensor;
int tanda = 0;
int zero=0;
int heatTime = 80;
int heatInterval = 255;
char printDensity = 15;
char printBreakTime = 15;
long duration, distance;

//
const int btreset = A1;
int btresetx = 1;

const int btstart = 12;
int btstartx = 1;

const int btprint = 11;
int btprintx = 1;

const int btfinish = 10;
int btfinishx = 1;


//menampilkan nilai jam
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}


void setup() {
 
  //setting lcd
  lcd.begin(16,2);
  lcd.clear();
  lcd.noCursor();

  //setting mode pin arduino
  pinMode(btreset, INPUT_PULLUP);
  pinMode(btstart, INPUT_PULLUP);
  pinMode(btprint, INPUT_PULLUP);
  pinMode(btfinish, INPUT_PULLUP);
  pinMode(A0, INPUT_PULLUP);
 
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  //setting printer
  Wire.begin();
  Serial.begin(9600);
  Thermal.begin(19200); // to write to our new printer
  initPrinter();
 
   //set jam dan tanggal
   //DS3231 seconds, minutes, hours, day, date, month, year
   setDS3231time(0,10,11,2,3,8,15);
}

//setting printer
void initPrinter()
{
 //Modify the print speed and heat
 Thermal.write(27);
 Thermal.write(55);
 Thermal.write(7); //Default 64 dots = 8*('7'+1)
 Thermal.write(heatTime); //Default 80 or 800us
 Thermal.write(heatInterval); //Default 2 or 20us
 //Modify the print density and timeout
 Thermal.write(18);
 Thermal.write(35);
 int printSetting = (printDensity<<4) | printBreakTime;
 Thermal.write(printSetting); //Combination of printDensity and printBreakTime

}

//setting barcode
void printBarcode(char zz[])
{
 Thermal.write(29); //GS
 Thermal.write(107); //k
 Thermal.write(zero); //m = 0
 for (int z=0; z<12; z++)
 {
 Thermal.write(zz[z]);
 }
 Thermal.write(zero); // bar code terminator
 delay(3000); // necessary delay
 Thermal.write(10);
 Thermal.write(10);
 Thermal.write(10);
}


//setting untuk rtc
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}


//variabel rtc
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}


//untuk menampilkan waktu
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  lcd.setCursor(0,0);
  lcd.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  lcd.print(":");
  if (minute<10)
  {
    lcd.print("0");
  }
  lcd.print(minute, DEC);
  lcd.print(":");
  if (second<10)
  {
    lcd.print("0");
  }
  lcd.print(second, DEC);
  lcd.print(" ");
  lcd.print(dayOfMonth, DEC);
  lcd.print("/");
  lcd.print(month, DEC);
  lcd.print("/");
  lcd.print(year, DEC);
  Serial.print(" Hari : ");
  switch(dayOfWeek){
  case 1:
    Serial.println("MINGGU");
    break;
  case 2:
    Serial.println("SENIN");
    break;
  case 3:
    Serial.println("SELASA");
    break;
  case 4:
    Serial.println("RABU");
    break;
  case 5:
    Serial.println("KAMIS");
    break;
  case 6:
    Serial.println("JUM'AT");
    break;
  case 7:
    Serial.println("SABTU");
    break;
  }
}


//untuk print waktu = jam dan tanggal
void printTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  Thermal.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Thermal.print(":");
  if (minute<10)
  {
    Thermal.print("0");
  }
 Thermal.print(minute, DEC);
  Thermal.print(":");
  if (second<10)
  {
    Thermal.print("0");
  }
  Thermal.print(second, DEC);
  Thermal.print(" ");
  Thermal.print(dayOfMonth, DEC);
  Thermal.print("/");
  Thermal.print(month, DEC);
  Thermal.print("/");
  Thermal.print(year, DEC);
  Thermal.print(" Hari : ");
  switch(dayOfWeek){
  case 1:
    Thermal.println("MINGGU");
    break;
  case 2:
    Thermal.println("SENIN");
    break;
  case 3:
    Thermal.println("SELASA");
    break;
  case 4:
    Thermal.println("RABU");
    break;
  case 5:
    Thermal.println("KAMIS");
    break;
  case 6:
    Thermal.println("JUM'AT");
    break;
  case 7:
    Thermal.println("SABTU");
    break;
  }
}


void loop() {
 
//update waktu 
//displayTime();

  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  Serial.println(distance);
  delay(200);
 
//untuk deteksi tombol 
  btresetx = digitalRead(btreset);
  btstartx = digitalRead(btstart);
  btfinishx = digitalRead(btfinish);
  btprintx = digitalRead(btprint);

//jika sensor bernilai high atau ada tegangan 5v maka counter + 1 
  if((distance <= 20)&&(mark == 1)&&(tanda == 0)){
  delay(100); 
  cacah++;
  tanda = 1;
  }
 
  if((distance > 20)&&(mark == 1)&&(tanda == 1)){
  delay(100); 
  tanda = 0;
  } 

//untuk menampilkan nilai counter   
lcd.setCursor(0,1);
lcd.print("X= ");
lcd.print(cacah);

//untuk menampilkan mode start
if(mark == 1){
//lcd.setCursor(0,1);
//lcd.print("START        ");
}

//untuk menampilkan mode finish
if(mark == 0){
//lcd.setCursor(0,1);
//lcd.print("FINISH         ");
}
    
delay(200);

//saat tombol reset ditekan nilai counter = 0
 if (btresetx == LOW ) {
 delay(200);
 lcd.clear();
 cacah = 0;
 }

 //saat tombol start ditekan
 if (btstartx == LOW ) {
 delay(200);
 mark = 1;
 }

 //saat tombol finish ditekan
 if (btfinishx == LOW ) {
 delay(200);
 mark = 0;
 }
 
 //saat tombol print ditekan
 if (btprintx == LOW ) {    

   Serial.println("print ready");
   Serial.println("printing....");
   delay(1000);

  //UNDERLINE MODE
  Thermal.write(27);
  Thermal.write(45);
  Thermal.write(1);
  Thermal.println(" HASIL HITUNGAN BARANG ");
  Thermal.write(10);

  //TURN OFF UNDERLINE MODE
  Thermal.write(27);
  Thermal.write(45);
  Thermal.write(zero);
  delay(3000);

  //NORMAL MODE
  Thermal.println("REKAP DATA");
  Thermal.write(10);

  //print waktu
  printTime();
  Thermal.write(10);

  //print suhu
  Thermal.print("Jumlah = ");
  Thermal.print(cacah);
  Thermal.println(" pcs");
  Thermal.write(10);
  Thermal.write(10);
 
  }
   
}





e. VIDEO HASILNYA










No comments:

Post a Comment