Advertisement
21ani

df player2

Jul 31st, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <DFPlayer_Mini_Mp3.h>
  3.  
  4. SoftwareSerial mySerial(2, 3); // RX, TX
  5. #include <Servo.h>
  6. #include <NewPing.h>
  7.  
  8. //
  9.  
  10.  
  11. //
  12.  
  13.  
  14. #define trigPin 11
  15. #define echoPin 12
  16. #define trigPin2  7
  17. #define echoPin2 6
  18. #define led 11
  19. #define led2 10
  20. Servo myservo;
  21.  
  22. NewPing ultrasonic1(trigPin, echoPin );
  23. NewPing ultrasonic2(trigPin2, echoPin2) ;
  24. int count = 0;
  25.  
  26. void setup() {
  27.   Serial.begin (9600);
  28.   pinMode(trigPin, OUTPUT);
  29.   pinMode(echoPin, INPUT);
  30.   mySerial.begin (9600);
  31.   mp3_set_serial (mySerial);  //set softwareSerial for DFPlayer-mini mp3 module
  32.   delay(1);  //wait 1ms for mp3 module to set volume
  33.   mp3_set_volume (50);
  34.  // mp3_play (1);
  35.  
  36.   //Serial.begin(115200);  
  37.  
  38.   myservo.attach(9); //Pin Servo  
  39.   myservo.write(0); //Derajat servo di set ke 0
  40.   myservo.detach();
  41.  
  42.  
  43. }
  44.  
  45. void loop() {
  46.  /* long duration, distance;
  47.   digitalWrite(trigPin, LOW);
  48.   delayMicroseconds(2);
  49.   digitalWrite(trigPin, HIGH);
  50.   delayMicroseconds(10);
  51.   digitalWrite(trigPin, LOW);
  52.   duration = pulseIn(echoPin, HIGH);
  53.   distance = (duration/2) / 29.1;
  54.   Serial.print(distance);
  55.   Serial.println(" cm");
  56. */
  57.   int US1 = ultrasonic1.ping_cm();
  58.   int US2 = ultrasonic2.ping_cm();
  59.   Serial.print("Hasil Sensor 1 :");
  60.   Serial.print(US1);
  61.   Serial.println("cm");
  62.   Serial.print("Hasil Sensor 2 :");
  63.   Serial.print(US2);
  64.   Serial.println("cm");
  65.  
  66.   if(0 < US1 <= 10)
  67.   {
  68.     myservo.attach(9);
  69.     myservo.write(90); //Posisi servo pada 90 derajat
  70.     delay(500);
  71.     myservo.detach();
  72.     count++;
  73.     if(count == 1){
  74.    mp3_play (1);
  75.     }
  76.  
  77.    
  78.  
  79.     goto tunda;  
  80. tunda:
  81.   {if (0 < US2 < 15)
  82.   {
  83.     myservo.attach(9);
  84.     myservo.write(0); //Posisi servo pada 0 derajat
  85.     delay(500);
  86.     myservo.detach();
  87.   }
  88.   }
  89.  
  90.  
  91.     }
  92. //mp3_stop();
  93.   //  delay(6000);
  94.     mp3_play (2);
  95.    
  96.   delay(500);
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement