Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <DFPlayer_Mini_Mp3.h>
- SoftwareSerial mySerial(2, 3); // RX, TX
- #include <Servo.h>
- #include <NewPing.h>
- //
- //
- #define trigPin 11
- #define echoPin 12
- #define trigPin2 7
- #define echoPin2 6
- #define led 11
- #define led2 10
- Servo myservo;
- NewPing ultrasonic1(trigPin, echoPin );
- NewPing ultrasonic2(trigPin2, echoPin2) ;
- int count = 0;
- void setup() {
- Serial.begin (9600);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- mySerial.begin (9600);
- mp3_set_serial (mySerial); //set softwareSerial for DFPlayer-mini mp3 module
- delay(1); //wait 1ms for mp3 module to set volume
- mp3_set_volume (50);
- // mp3_play (1);
- //Serial.begin(115200);
- myservo.attach(9); //Pin Servo
- myservo.write(0); //Derajat servo di set ke 0
- myservo.detach();
- }
- void loop() {
- /* long duration, distance;
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance = (duration/2) / 29.1;
- Serial.print(distance);
- Serial.println(" cm");
- */
- int US1 = ultrasonic1.ping_cm();
- int US2 = ultrasonic2.ping_cm();
- Serial.print("Hasil Sensor 1 :");
- Serial.print(US1);
- Serial.println("cm");
- Serial.print("Hasil Sensor 2 :");
- Serial.print(US2);
- Serial.println("cm");
- if(0 < US1 <= 10)
- {
- myservo.attach(9);
- myservo.write(90); //Posisi servo pada 90 derajat
- delay(500);
- myservo.detach();
- count++;
- if(count == 1){
- mp3_play (1);
- }
- goto tunda;
- tunda:
- {if (0 < US2 < 15)
- {
- myservo.attach(9);
- myservo.write(0); //Posisi servo pada 0 derajat
- delay(500);
- myservo.detach();
- }
- }
- }
- //mp3_stop();
- // delay(6000);
- mp3_play (2);
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement