Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang digunakan untuk mendeteksi jarak benda, sistemnya seperti radar yang bergerak memantau kondisi benda. jadi jika ada benda yang mendekat maka akan membunyikan buzzer dan menghidupkan led. alat ini terdiri dari sensor ultrasonik dan servo. mikroprosesor yang dipakai yaitu Arduino Mega. untuk lebih jelasnya berikut skema dan programnya.
a. Arduino Mega
b. Micro Servo
c. Sensor Ultrasonik SRF04
d. Program Arduino
#include <Servo.h>
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
Servo myservo;
int x = 50;
int y = 0;
int buzzer = 2;
int ledx = 3;
void setup() {
myservo.attach(9);
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(ledx, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
myservo.write(x);
if(y == 0){
x = x + 5;
}
if(y == 1){
x = x - 5;
}
if(x > 150){
y = 1;
}
if(x < 50){
y = 0;
}
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
if (distance < 10) { // This is where the LED On/Off happens
digitalWrite(buzzer, HIGH);
digitalWrite(ledx, LOW);
}
else {
digitalWrite(buzzer, LOW);
digitalWrite(ledx, HIGH);
}
delay(200);
}
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
Servo myservo;
int x = 50;
int y = 0;
int buzzer = 2;
int ledx = 3;
void setup() {
myservo.attach(9);
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(ledx, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
myservo.write(x);
if(y == 0){
x = x + 5;
}
if(y == 1){
x = x - 5;
}
if(x > 150){
y = 1;
}
if(x < 50){
y = 0;
}
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
if (distance < 10) { // This is where the LED On/Off happens
digitalWrite(buzzer, HIGH);
digitalWrite(ledx, LOW);
}
else {
digitalWrite(buzzer, LOW);
digitalWrite(ledx, HIGH);
}
delay(200);
}
e. VIDEO HASILNYA
No comments:
Post a Comment