Advertisement
21ani

df playerku

Jul 30th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.85 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.  
  25. void setup() {
  26.   Serial.begin (9600);
  27.   pinMode(trigPin, OUTPUT);
  28.   pinMode(echoPin, INPUT);
  29.   mySerial.begin (9600);
  30.   mp3_set_serial (mySerial);  //set softwareSerial for DFPlayer-mini mp3 module
  31.   delay(1);  //wait 1ms for mp3 module to set volume
  32.   mp3_set_volume (50);
  33.  // mp3_play (1);
  34.  
  35.   //Serial.begin(115200);  
  36.  
  37.   myservo.attach(9); //Pin Servo  
  38.   myservo.write(0); //Derajat servo di set ke 0
  39.  
  40.  
  41. }
  42.  
  43. void loop() {
  44.  /* long duration, distance;
  45.   digitalWrite(trigPin, LOW);
  46.   delayMicroseconds(2);
  47.   digitalWrite(trigPin, HIGH);
  48.   delayMicroseconds(10);
  49.   digitalWrite(trigPin, LOW);
  50.   duration = pulseIn(echoPin, HIGH);
  51.   distance = (duration/2) / 29.1;
  52.   Serial.print(distance);
  53.   Serial.println(" cm");
  54. */
  55.   int US1 = ultrasonic1.ping_cm();
  56.   int US2 = ultrasonic2.ping_cm();
  57.   Serial.print("Hasil Sensor 1 :");
  58.   Serial.print(US1);
  59.   Serial.print("cm");
  60.   Serial.print("Hasil Sensor 2 :");
  61.   Serial.print(US2);
  62.   Serial.println("cm");
  63.  
  64.   if(US1 < 10)
  65.   {
  66.     myservo.attach(9);
  67.     myservo.write(90); //Posisi servo pada 90 derajat
  68.     delay(500);
  69.     myservo.detach();  
  70.    mp3_play (1);
  71.   //boolean play_state = digitalRead(2);
  72.   //if(play_state == HIGH)
  73.    
  74.  
  75.     goto tunda;  
  76. tunda:
  77.   {if (US2 < 15)
  78.   {
  79.     myservo.attach(9);
  80.     myservo.write(0); //Posisi servo pada 0 derajat
  81.     delay(500);
  82.     myservo.detach();
  83.   }
  84.   }
  85.  
  86.  
  87.     }
  88. mp3_stop();
  89.   //  delay(6000);
  90.     mp3_play (2);
  91.    
  92.   delay(500);
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement