Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang digunakan untuk mendeteksi jarak benda menggunakan Arduino dan output led beserta alarm buzzer, jadi alat ini sebenarnya digunakan untuk mobil parkir yang mana saat dia mundur pasti akan membutuhkan sebuah indikator pada saat jarak lebih dari 3 meter, jarak diantara 3 meter sampai 1,5 meter dan jarak kurang dari 1,5 meter. alarm buzzer dan led akan berbunyi blink ketika jarak diantara 2,5 sampai 1,5 meter, kemudian jika jarak kurang dari 1,5 meter led dan buzzer akan menyala secara kontinyu atau terus menerus. jika jarak 3 meter atau lebih dari 2,5 meter maka led dan buzzer akan mati. sensor yang digunakan adalah sensor ultrasonic JSN SR04T. untuk lebih jelasnya berikut adalah skema dan programnya.
a. Arduino Uno
b. Sensor Jarak JSN SR04T
c. Led dan Buzzer
d. Program Arduino IDE
#include "Wire.h"
#include <LiquidCrystal.h>
const int pingPin = 7;
int inPin = 6;
const int pingPin2 = 5;
int inPin2 = 4;
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
long duration, cm;
long duration2, cm2;
int mark;
int mark2;
//lebih dari 3 meter = 190
//antara 1,5 sampai 2,5 = 80
//kurang dari 1,5 = 50
void setup() {
pinMode(pingPin, OUTPUT);
pinMode(inPin, INPUT);
pinMode(pingPin2, OUTPUT);
pinMode(inPin2, INPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
//Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
lcd.noCursor();
}
void loop(){
mark = 0;
mark2 = 0;
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(inPin, HIGH);
cm = microsecondsToCentimeters(duration);
//Serial.println(cm);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin2, LOW);
duration2 = pulseIn(inPin2, HIGH);
cm2 = microsecondsToCentimeters(duration2);
if((cm <= 60)&&(cm2 >= 60 )&&(mark == 0)&&(mark2 == 0)){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
mark = 1;
mark2 = 1;
}
if((cm2 <= 60)&&(cm >= 60 )&&(mark == 0)&&(mark2 == 0)){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
mark = 1;
mark2 = 1;
}
if((cm >= 100)&&(mark == 0 )){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
mark = 1;
delay(500);
}
if((cm >= 60)&&(cm <= 100)&&(mark == 0)){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delay(100);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
delay(100);
mark = 1;
}
if((cm <= 60)&&(mark == 0 )){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
mark = 1;
}
if((cm2 >= 100)&&(mark2 == 0)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
mark2 = 1;
delay(500);
}
if((cm2 >= 60)&&(cm2 <= 100)&&(mark2 == 0)){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delay(100);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
delay(100);
mark2 = 1;
}
if((cm2 <= 60)&&(mark2 == 0)){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
mark2 = 1;
}
lcd.setCursor(0, 0);
lcd.print(cm);
lcd.setCursor(0, 1);
lcd.print(cm2);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
#include <LiquidCrystal.h>
const int pingPin = 7;
int inPin = 6;
const int pingPin2 = 5;
int inPin2 = 4;
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
long duration, cm;
long duration2, cm2;
int mark;
int mark2;
//lebih dari 3 meter = 190
//antara 1,5 sampai 2,5 = 80
//kurang dari 1,5 = 50
void setup() {
pinMode(pingPin, OUTPUT);
pinMode(inPin, INPUT);
pinMode(pingPin2, OUTPUT);
pinMode(inPin2, INPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
//Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
lcd.noCursor();
}
void loop(){
mark = 0;
mark2 = 0;
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(inPin, HIGH);
cm = microsecondsToCentimeters(duration);
//Serial.println(cm);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin2, LOW);
duration2 = pulseIn(inPin2, HIGH);
cm2 = microsecondsToCentimeters(duration2);
if((cm <= 60)&&(cm2 >= 60 )&&(mark == 0)&&(mark2 == 0)){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
mark = 1;
mark2 = 1;
}
if((cm2 <= 60)&&(cm >= 60 )&&(mark == 0)&&(mark2 == 0)){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
mark = 1;
mark2 = 1;
}
if((cm >= 100)&&(mark == 0 )){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
mark = 1;
delay(500);
}
if((cm >= 60)&&(cm <= 100)&&(mark == 0)){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delay(100);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
delay(100);
mark = 1;
}
if((cm <= 60)&&(mark == 0 )){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
mark = 1;
}
if((cm2 >= 100)&&(mark2 == 0)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
mark2 = 1;
delay(500);
}
if((cm2 >= 60)&&(cm2 <= 100)&&(mark2 == 0)){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delay(100);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
delay(100);
mark2 = 1;
}
if((cm2 <= 60)&&(mark2 == 0)){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
mark2 = 1;
}
lcd.setCursor(0, 0);
lcd.print(cm);
lcd.setCursor(0, 1);
lcd.print(cm2);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
e. VIDEO HASILNYA
No comments:
Post a Comment