Translate

Membuat Alat Kendali Konveyor Deteksi Lubang dan Ketinggian Benda ARDUINO SENSOR LDR dan Jarak HC-SRF04

Membuat Alat Kendali Konveyor Deteksi Lubang dan Ketinggian Benda ARDUINO SENSOR LDR dan Jarak HC-SRF04



            Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat mendeteksi lubang dan ketinggian benda dengan menggunakan sensor LDR dan sensor HC-SRF04. alat ini dilengkapi dengan konveyor yang berfungsi untuk menjalankan benda dari posisi awal ke posisi akhir. jika terdeteksi ada lubang pada benda maka motor pada konveyor akan bernhenti dan buzzer menyala. begitupula jika terdeteksi ketinggian benda yang melebihi normal maka buzzer juga akan menyala. untuk lebih jelasnya berikut adalah program dan daftar komponenya. 




a. Arduino Mega




b. Sensor Cahaaya / LDR




c. Sensor HC-SRF04




d. Modul Driver Motor L298




e. LCD OLED






f. Program Arduino IDE

#include <EEPROM.h>
#include <Wire.h>
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);  

#define trigPin 15
#define echoPin 14

char tmp_string1[8];
char tmp_string2[8];
char tmp_string3[8];
char tmp_string4[8];
char tmp_string5[8];

int buzzer = 11;
int ledhijau = A6;
int ledmerah = A7;

int counter;

int batas1 = 50;
int batas2 = 50;

int motor1 = 2;
int motor2 = 3;
int pwmena = 9;
int ok;

int bton = 6;
int btoff = 7;
int resetku = 5;


int btonx = 0;
int btoffx = 0;
int resetkux = 0;
int cacah = 0;

int dataadc1, dataadc2;
long duration, distance;

int addr;

void draw(){

 itoa(distance, tmp_string1, 8);
 itoa(dataadc1, tmp_string2, 8);
 itoa(dataadc2, tmp_string3, 8);
 itoa(ok, tmp_string4, 8);
 itoa(counter, tmp_string5, 8);

  u8g.setFont(u8g_font_unifont);
  u8g.drawStr(0, 22, "J= "); 
  u8g.drawStr(40, 22, tmp_string1);
  u8g.drawStr(80, 22, tmp_string5);
  u8g.drawStr(0, 40, "A= "); 
  u8g.drawStr(20, 40, tmp_string2);
  u8g.drawStr(50, 40, "/");
  u8g.drawStr(60, 40, tmp_string3);
  u8g.drawStr(0, 60, "OK= "); 
  u8g.drawStr(40, 60, tmp_string4);
  
}

void setup() {
  
  Serial.begin (9600);

  pinMode(ledhijau, OUTPUT);  
  pinMode(ledmerah, OUTPUT);  

  pinMode(trigPin, OUTPUT);  
  pinMode(echoPin, INPUT);
  pinMode(buzzer, OUTPUT);

  pinMode(bton, INPUT_PULLUP);
  pinMode(btoff, INPUT_PULLUP);
  pinMode(resetku, INPUT_PULLUP);

  pinMode(9,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);

  digitalWrite(ledhijau,LOW);
  digitalWrite(ledmerah,LOW);
  digitalWrite(buzzer,LOW);

  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
  
  ok = EEPROM.read(addr);

 }

 void loop() {
  
 btonx = digitalRead(bton);  
 btoffx = digitalRead(btoff);  
 resetkux = digitalRead(resetku);  
   
 u8g.firstPage();
  do {
    draw();

  } while( u8g.nextPage() );   

   
 dataadc1 = analogRead(A0);   
 dataadc2 = analogRead(A1);   

if(btonx == 0){   
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
analogWrite(9,100);
digitalWrite(ledhijau,LOW);
digitalWrite(ledmerah,HIGH);
}

if(btoffx == 0){   
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(buzzer,LOW);
analogWrite(9,0);
}


if((dataadc1 < batas1)&&(dataadc2 < batas2)){
counter = 0;
proses();
}


if(resetkux == 0){   
ok = 0;
}

  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;

delay(200);
}

void proses(){

 EEPROM.write(addr, ok);

 btonx = digitalRead(bton);  
 btoffx = digitalRead(btoff);  
 resetkux = digitalRead(resetku);  

if(btonx == 0){   
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
analogWrite(9,100);
digitalWrite(ledhijau,LOW);
digitalWrite(ledmerah,HIGH);
}

if(btoffx == 0){   
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(buzzer,LOW);
analogWrite(9,0);
}

if(resetkux == 0){   
ok = 0;
}
   
 u8g.firstPage();
  do {
    draw();

  } while( u8g.nextPage() );   

dataadc1 = analogRead(A0);   
dataadc2 = analogRead(A1);   

counter++;

if(counter >= 5){
counter = 0;  
ok++;
}

if(dataadc1 >= batas1){
digitalWrite(ledhijau,HIGH);
digitalWrite(ledmerah,LOW);
digitalWrite(buzzer,HIGH);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
analogWrite(9,0);
berhenti();
return;
}

if(dataadc2 >= batas2){
digitalWrite(ledhijau,HIGH);
digitalWrite(ledmerah,LOW);
digitalWrite(buzzer,HIGH);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
analogWrite(9,0);
berhenti();
return;
}

  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;

if(distance < 6){
digitalWrite(ledhijau,HIGH);
digitalWrite(ledmerah,LOW);
digitalWrite(buzzer,HIGH);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
analogWrite(9,0);
berhenti();
return;
}

delay(50);
proses();
}

void berhenti(){

 btonx = digitalRead(bton);  
 btoffx = digitalRead(btoff);  
 resetkux = digitalRead(resetku);  

if(btonx == 0){   
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
analogWrite(9,100);
digitalWrite(ledhijau,LOW);
digitalWrite(ledmerah,HIGH);
return;
}

if(btoffx == 0){   
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(buzzer,LOW);
analogWrite(9,0);
}

berhenti();
}






g. VIDEO HASILNYA





   
        



No comments:

Post a Comment