Hello there!
First of all, thanks for having clicked on my topic.
I would really appreciate some help as I am stuck since a few days with a project for a diorama. I believe it might be a relatively simple issue to solve for somebody less "newbie" than me.
I am using a Mega2560 R3 to which I attached a LED, a DFPlayer (SD-card reader) and a speaker. Basically, I want the LED to blink in the same rythm as the gunfire (latter is the audiofile on the SD-card) when pressing a button.
Both the codes for the LED and the audio file work perfectly in a seperate way, but I can't seem to combine them in a proper manner. The best I was able to do is the following code, which upon uploading plays the audio file, but when I press the button, only the LED blinks.
Also, it is intended that the LED-blinking sequence and the audio only run for one time after the button was pressed, which so far I was able to achieve.
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
int Schalterzustand;
int InputPin = 2;
int LEDPin = 13;
void setup()
{
Serial.begin(9600);
pinMode(InputPin, INPUT);
pinMode(LEDPin, OUTPUT);
mySoftwareSerial.begin(9600);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
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); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(5); //Set volume value. From 0 to 30
myDFPlayer.play(1); //Play the first mp3
}
void loop() {
Schalterzustand = digitalRead(InputPin);
Serial.println(Schalterzustand, DEC);
if (Schalterzustand == 1)
{
static unsigned long timer = millis();
if (millis() - timer > 3000) {
timer = millis();
// myDFPlayer.next(); //Play next mp3 eve
}
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
delay(800);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
delay(1000);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
digitalWrite(LEDPin, HIGH);
delay(20);
digitalWrite(LEDPin, LOW);
delay(180);
delay(3000);
}
if (Schalterzustand == 0)
{
digitalWrite(LEDPin, LOW);
}
}
I am aware of that there are probably options to simplify the long LEDPin sequence, but that is, for now, not my priority.
The diorama is, btw for an exposition regarding the Spanish Civil War in Barcelona next Thursday. If anyone of you guys is around and up to join, please feel free to send me a message.
Thanks in advance, any kind of help is highly appreciated!!
Greetings,
Ole