#define ECHO_PIN 3
#define TRIG_PIN 4
#define vibrator 2
#define buzzer 5
int a = 0;
int tombol = A5;
int reset ;
#include <Servo.h>
Servo myservo;
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(vibrator, OUTPUT);
pinMode(tombol, INPUT_PULLUP);
myservo.attach(11);
myservo.write(0);
}
void loop() {
bacaSensor();
baca_tombol();
}
void bacaSensor(){
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.034 / 2;
// Kondisi Pakan Penuh
if (a == LOW){
if (distance >= 20 && distance <= 100){
myservo.write(90);
delay(15);
digitalWrite (vibrator, HIGH);
}
if (distance > 100 && distance <= 130){
myservo.write(30);
delay(15);
digitalWrite (vibrator, HIGH);
}
if (distance >130){
digitalWrite(vibrator, LOW);
myservo.write(0);
delay(15);
tone (buzzer, 120);
delay(500);
noTone(buzzer);
}
delay(100);
}
if (a == HIGH){
noTone(buzzer);
}
}
void baca_tombol(){
reset = digitalRead(tombol);
if (reset == LOW) {
}
if (reset == HIGH){
digitalWrite(vibrator, LOW);
myservo.write(0);
delay(15);
a = 1;
}
}