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.
 
 

70 lines
1.2 KiB

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11);
DFRobotDFPlayerMini myDFPlayer;
#define sensor 4
#define light 5
#define sound 2
bool lastValue = LOW;
bool sensorValue = LOW;
void setup() {
mySoftwareSerial.begin(9600);
Serial.begin(9600);
pinMode(sensor, INPUT);
pinMode(light, OUTPUT);
pinMode(sound, 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() {
sensorValue = digitalRead(sensor);
/*
Serial.print(sensorValue);
Serial.print("\t");
Serial.println(lastValue);
*/
delay(50);
if(sensorValue) {
if(!lastValue) {
myDFPlayer.play(1);
for(int i=0; i<25; i++) {
analogWrite(light, i*10);
delay(random(5, 50-i));
analogWrite(light, 0);
delay(random(5, 50-i));
}
analogWrite(light, 255);
delay(5000);
}
} else {
digitalWrite(light, LOW);
}
lastValue = sensorValue;
}