Advertisement
safwan092

Untitled

Nov 13th, 2021
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include "Arduino.h"
  2. #include "SoftwareSerial.h"
  3. #include "DFRobotDFPlayerMini.h"
  4. #include <Servo.h>
  5.  
  6. SoftwareSerial mySoftwareSerial(4, 3); // RX, TX
  7. DFRobotDFPlayerMini myDFPlayer;
  8. void printDetail(uint8_t type, int value);
  9.  
  10. #define echoPin 11
  11. #define trigPin 12
  12. //#define buzzer 8
  13.  
  14. long duration;
  15. int distance;
  16. Servo servo;
  17.  
  18. void setup() {
  19.  
  20.   pinMode(trigPin, OUTPUT);
  21.   pinMode(echoPin, INPUT);
  22.   Serial.begin(9600);
  23.   mySoftwareSerial.begin(9600);
  24.   servo.attach(9);
  25.   if (!myDFPlayer.begin(mySoftwareSerial)) {
  26.     Serial.println(F("Unable to begin:"));
  27.     Serial.println(F("1.Please recheck the connection!"));
  28.     Serial.println(F("2.Please insert the SD card!"));
  29.     while (true) {
  30.       delay(0);
  31.     }
  32.   }
  33.   Serial.println(F("DFPlayer Mini online."));
  34.  
  35.   myDFPlayer.volume(30);  //Set volume value. From 0 to 30
  36. }
  37.  
  38. void loop() {
  39.  
  40.   digitalWrite(trigPin, LOW);
  41.   delayMicroseconds(2);
  42.   digitalWrite(trigPin, HIGH);
  43.   delayMicroseconds(10);
  44.   digitalWrite(trigPin, LOW);
  45.   duration = pulseIn(echoPin, HIGH);
  46.   distance = duration * 0.034 / 2;
  47.  
  48.   if (distance < 10) {
  49.     Serial.println("the distance is less than 10");
  50.     servo.write(90);
  51.     myDFPlayer.play(1);
  52.     delay(4000);
  53.     myDFPlayer.stop();
  54.   }
  55.   else {
  56.     servo.write(0);
  57.   }
  58.   if (distance > 60 || distance <= 0) {
  59.     Serial.println("the distance is more than 60");
  60.   }
  61.   else {
  62.     Serial.print(distance);
  63.     Serial.println(" cm");
  64.   }
  65.   delay(500);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement