You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.2 KiB
63 lines
1.2 KiB
|
|
#include "Arduino.h"
|
|
#include "SoftwareSerial.h"
|
|
#include "DFRobotDFPlayerMini.h"
|
|
|
|
SoftwareSerial mySoftwareSerial(10, 11);
|
|
DFRobotDFPlayerMini myDFPlayer;
|
|
|
|
#define LED 6
|
|
#define TRIG 4
|
|
#define ECHO 5
|
|
#define TRIG_DIST 100 // min trigger distance
|
|
#define SOUND 7
|
|
|
|
long duration, cm;
|
|
|
|
void setup() {
|
|
mySoftwareSerial.begin(9600);
|
|
Serial.begin(9600);
|
|
pinMode(TRIG, OUTPUT);
|
|
pinMode(ECHO, INPUT);
|
|
pinMode(SOUND, OUTPUT);
|
|
pinMode(LED, OUTPUT);
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
myDFPlayer.volume(20);
|
|
}
|
|
|
|
void loop() {
|
|
|
|
digitalWrite(TRIG, LOW);
|
|
delayMicroseconds(2);
|
|
digitalWrite(TRIG, HIGH);
|
|
delayMicroseconds(10);
|
|
digitalWrite(TRIG, LOW);
|
|
duration = pulseIn(ECHO, HIGH);
|
|
cm = duration / 58;
|
|
|
|
Serial.println(cm);
|
|
|
|
if(cm <= TRIG_DIST) {
|
|
myDFPlayer.play(1);
|
|
|
|
for(int i=0; i<50; i++) {
|
|
analogWrite(LED, i*5);
|
|
delay(50-i);
|
|
}
|
|
|
|
analogWrite(LED, 255);
|
|
delay(5000);
|
|
|
|
} else {
|
|
digitalWrite(LED, LOW);
|
|
}
|
|
}
|