Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah tempat sampah otomatis yang dapat memisahkan antara sampah basah dan kering dengan menggunakan Arduino dan sensor air beserta sensor jarak. alat ini menggunakan servo sebagai penggeraknya. untuk lebih jelasnya berikut adalah komponen dan programnya.
a. Arduino Uno
b. Sensor Jarak HC-SRF04
c. Sensor Air / Hujan
d. Motor Servo SG90
e. Program Arduino IDE
#include <Servo.h>
#include <Wire.h>
#define trigPin 2
#define echoPin 3
#define trigPinx 4
#define echoPinx 5
long duration, distance;
long durationx, distancex;
int dataadc1, dataadc2;
Servo myservo1;
Servo myservo2;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPinx, OUTPUT);
pinMode(echoPinx, INPUT);
myservo1.attach(9);
myservo2.attach(10);
myservo1.write(100);
myservo2.write(100);
delay(2000);
}
void loop() {
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;
digitalWrite(trigPinx, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPinx, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPinx, LOW);
durationx = pulseIn(echoPinx, HIGH);
distancex = (durationx/2) / 29.1;
dataadc1 = analogRead(A0);
dataadc2 = analogRead(A1);
Serial.print("Jarak1 = ");
Serial.print(distance);
Serial.print(" cm | Jarak2 = ");
Serial.print(distancex);
Serial.print(" cm | ");
Serial.print("adc1 = ");
Serial.print(dataadc1);
Serial.print(" | ");
Serial.print("adc2 = ");
Serial.print(dataadc2);
Serial.println(" ");
//buka
if(distance < 50){
myservo1.write(150);
delay(5000);
mulai();
}
//tutup
if(distance >= 50){
myservo1.write(100);
}
delay(200);
}
void mulai(){
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;
digitalWrite(trigPinx, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPinx, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPinx, LOW);
durationx = pulseIn(echoPinx, HIGH);
distancex = (durationx/2) / 29.1;
dataadc1 = analogRead(A0);
dataadc2 = analogRead(A1);
Serial.print("Jarak1 = ");
Serial.print(distance);
Serial.print(" cm | Jarak2 = ");
Serial.print(distancex);
Serial.print(" cm | ");
Serial.print("adc1 = ");
Serial.print(dataadc1);
Serial.print(" | ");
Serial.print("adc2 = ");
Serial.print(dataadc2);
Serial.println(" ");
//basah ke kanan
if((distancex <= 20) && (dataadc1 < 1000)){
myservo2.write(50);
delay(5000);
myservo2.write(100);
delay(5000);
return;
}
//basah ke kanan
if((distancex <= 20) && (dataadc2 < 1000)){
myservo2.write(50);
delay(5000);
myservo2.write(100);
delay(5000);
return;
}
//basah ke kanan
if((distancex <= 20) && (dataadc1 < 1000) && (dataadc2 < 1000)){
myservo2.write(50);
delay(5000);
myservo2.write(100);
delay(5000);
return;
}
//kering ke kiri
if((distancex <= 20) && (dataadc1 > 1000) && (dataadc2 > 1000)){
myservo2.write(150);
delay(5000);
myservo2.write(100);
delay(5000);
return;
}
delay(200);
mulai();
}
#include <Wire.h>
#define trigPin 2
#define echoPin 3
#define trigPinx 4
#define echoPinx 5
long duration, distance;
long durationx, distancex;
int dataadc1, dataadc2;
Servo myservo1;
Servo myservo2;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPinx, OUTPUT);
pinMode(echoPinx, INPUT);
myservo1.attach(9);
myservo2.attach(10);
myservo1.write(100);
myservo2.write(100);
delay(2000);
}
void loop() {
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;
digitalWrite(trigPinx, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPinx, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPinx, LOW);
durationx = pulseIn(echoPinx, HIGH);
distancex = (durationx/2) / 29.1;
dataadc1 = analogRead(A0);
dataadc2 = analogRead(A1);
Serial.print("Jarak1 = ");
Serial.print(distance);
Serial.print(" cm | Jarak2 = ");
Serial.print(distancex);
Serial.print(" cm | ");
Serial.print("adc1 = ");
Serial.print(dataadc1);
Serial.print(" | ");
Serial.print("adc2 = ");
Serial.print(dataadc2);
Serial.println(" ");
//buka
if(distance < 50){
myservo1.write(150);
delay(5000);
mulai();
}
//tutup
if(distance >= 50){
myservo1.write(100);
}
delay(200);
}
void mulai(){
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;
digitalWrite(trigPinx, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPinx, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPinx, LOW);
durationx = pulseIn(echoPinx, HIGH);
distancex = (durationx/2) / 29.1;
dataadc1 = analogRead(A0);
dataadc2 = analogRead(A1);
Serial.print("Jarak1 = ");
Serial.print(distance);
Serial.print(" cm | Jarak2 = ");
Serial.print(distancex);
Serial.print(" cm | ");
Serial.print("adc1 = ");
Serial.print(dataadc1);
Serial.print(" | ");
Serial.print("adc2 = ");
Serial.print(dataadc2);
Serial.println(" ");
//basah ke kanan
if((distancex <= 20) && (dataadc1 < 1000)){
myservo2.write(50);
delay(5000);
myservo2.write(100);
delay(5000);
return;
}
//basah ke kanan
if((distancex <= 20) && (dataadc2 < 1000)){
myservo2.write(50);
delay(5000);
myservo2.write(100);
delay(5000);
return;
}
//basah ke kanan
if((distancex <= 20) && (dataadc1 < 1000) && (dataadc2 < 1000)){
myservo2.write(50);
delay(5000);
myservo2.write(100);
delay(5000);
return;
}
//kering ke kiri
if((distancex <= 20) && (dataadc1 > 1000) && (dataadc2 > 1000)){
myservo2.write(150);
delay(5000);
myservo2.write(100);
delay(5000);
return;
}
delay(200);
mulai();
}
f. VIDEO HASILNYA
No comments:
Post a Comment