1#include<Servo.h>
2#include <SoftwareSerial.h>
3#include <DFPlayer_Mini_Mp3.h>
4
5SoftwareSerial mySerial(10, 11);
6
7Servo servo;
8int const trigPin = 6;
9int const echoPin = 5;
10void setup()
11{
12 Serial.begin (9600);
13mySerial.begin (9600);
14mp3_set_serial (mySerial);
15delay(10);
16pinMode(trigPin, OUTPUT);
17pinMode(echoPin, INPUT);
18 servo.attach(3);
19}
20void loop()
21{ int duration, distance;
22digitalWrite(trigPin, HIGH);
23delay(1);
24digitalWrite(trigPin, LOW);
25
26duration = pulseIn(echoPin, HIGH);
27
28distance = (duration/2) / 29.1;
29
30if (distance <= 20 && distance >= 0) {
31 servo.write(50);
32 delay(3000);
33} else {
34
35 servo.write(160);
36 void setup () ;
37{
38mp3_set_volume (95);
39delay(10);
40mp3_play ();
41delay(10);
42mp3_play (1);
43delay(10);
44}
45}
46
47
48delay(40);
49}
50