Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Arduino.h"
- #include "SoftwareSerial.h"
- #include "DFRobotDFPlayerMini.h"
- #include <Servo.h>
- SoftwareSerial mySoftwareSerial(4, 3); // RX, TX
- DFRobotDFPlayerMini myDFPlayer;
- void printDetail(uint8_t type, int value);
- #define echoPin 11
- #define trigPin 12
- //#define buzzer 8
- long duration;
- int distance;
- Servo servo;
- void setup() {
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- Serial.begin(9600);
- mySoftwareSerial.begin(9600);
- servo.attach(9);
- if (!myDFPlayer.begin(mySoftwareSerial)) {
- Serial.println(F("Unable to begin:"));
- Serial.println(F("1.Please recheck the connection!"));
- Serial.println(F("2.Please insert the SD card!"));
- while (true) {
- delay(0);
- }
- }
- Serial.println(F("DFPlayer Mini online."));
- myDFPlayer.volume(30); //Set volume value. From 0 to 30
- }
- void loop() {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance = duration * 0.034 / 2;
- if (distance < 30) {
- Serial.println("the distance is less than 10");
- servo.write(90);
- myDFPlayer.play(1);
- delay(4000);
- myDFPlayer.stop();
- }
- else {
- servo.write(0);
- }
- if (distance > 60 || distance <= 0) {
- Serial.println("the distance is more than 60");
- }
- else {
- Serial.print(distance);
- Serial.println(" cm");
- }
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement