And here is the code. Thanks to your structure of coding, it was easy to find the correct lines where to put in the DF command lines.
As you can see I trigger one MP3 for pushbutton sound effect and another MP3 for the phaser effect.
First I was thinking about merging the soundeffect 3 times into one MP3, but then I realized that I only need one MP3 and it gets
triggered everytime the effect is triggered. So if I want 4 instead of 3 shootings, I don´t have to merge a new MP3 file for 4 shootings, it just gets
triggered 4 times then.
The DFPlayer code parts are bold. And I hope the code has no syntax mistakes, cause the “source code” function put every line of code in a seperate line and I had to delete the spare lines.
#include "FastLED.h"
#include <EEPROM.h>
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
#define PIN 7
#define buttonPin 3 // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
//=============== DFPlayer mini ===============//
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
//=============== DFPlayer mini ===============//
void setup()
{
FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
randomSeed(analogRead(0)); // for a better random
FastLED.setBrightness(20);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
//=============== DFPlayer mini setup ===============//
mySoftwareSerial.begin(9600);
Serial.begin(115200);
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
while(true);
}
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
myDFPlayer.volume(20); //Set volume value (0~30).
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
//=============== DFPlayer mini setup end ===============//
}
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
myDFPlayer.playMp3Folder(5); // plays MP3 file number 5 which is a button sound
delay(1000); // this delay needs to be at least as long as the MP3 button sound file to give it enough time to be played
// turn phaser on 3x:
startrek_phaser( 0xff, 0x90, 0, -1, 4, 3, 10, 20, 60, 40);
startrek_phaser( 0xff, 0x90, 0, -1, 4, 3, 10, 20, 60, 40);
startrek_phaser( 0xff, 0x90, 0, -1, 4, 3, 10, 20, 60, 40);
}
}
void startrek_phaser(byte PhaserRed, byte PhaserGreen, byte PhaserBlue, int TargetLED,
byte PhaserWidth, byte ShootingWidth, int msAimDelay,
int PulsateAmount, int msPulsateDelayBright, int msPulsateDelayDarker)
{
// These define how much the outer LEDs sim during Aiming, Shooting and Pulsating
#define DimmingPhaserAim 200
#define DimmingShoot 128
#define DimmingPulsing 128
int PhaserHalf = PhaserWidth/2; // division will take integer part: 3/2 = 1.5 -> 1, 4/2 = 2 -> 2
int PhaserCenterWidth = 2 - (PhaserWidth%2); // odd numbers: 1, even numbers: 2
int PhaserAim;
myDFPlayer.playMp3Folder(1); // plays MP3 file number 1 which is the phaser sound
if(TargetLED==-1)
{
PhaserAim = random(NUM_LEDS); // Pick random aim point
if(PhaserAim<PhaserHalf) // just making sure we do not go out of the range of LEDs
{
PhaserAim = PhaserWidth;
}
else if(PhaserAim+PhaserHalf>NUM_LEDS)
{
PhaserAim = NUM_LEDS-PhaserHalf;
}
}
else
{
PhaserAim = TargetLED;
}
int StepsLeftside = PhaserAim; // 0 - PhaserAim
int StepsRightside = NUM_LEDS-PhaserAim; // PhaserAim - NUM_LEDS
int maxSteps = max( StepsLeftside, StepsRightside );
int LEDPos;
// move LEDs from outside to phaser position
for(int counter=0; counter<=maxSteps; counter++)
{
setAll(0,0,0); // set all LEDs dark
// Left side towards aim point
LEDPos = PhaserAim-maxSteps+counter;
if( LEDPos >= 0 ) {
for (int PhaserBlock = 0; PhaserBlock<PhaserWidth; PhaserBlock++)
{
if(LEDPos+PhaserBlock>0)
{
leds[LEDPos+PhaserBlock] = CRGB( PhaserRed, PhaserGreen, PhaserBlue);
// only center (odd width) or center 2 LEDs (even width) should be bright, others need to fade
if ( ( (PhaserCenterWidth==1) && (PhaserBlock!=PhaserHalf) ) ||
( (PhaserCenterWidth==2) && (PhaserBlock!=PhaserHalf-1) && (PhaserBlock!=PhaserHalf) ) )
{
leds[LEDPos+PhaserBlock].fadeLightBy( DimmingPhaserAim );
}
}
}
}
// Right side towards aim point
LEDPos = PhaserAim+maxSteps-counter;
if( LEDPos < NUM_LEDS ) {
for (int PhaserBlock = 0; PhaserBlock<PhaserWidth; PhaserBlock++)
{
if(LEDPos+PhaserBlock<NUM_LEDS)
{
leds[LEDPos+PhaserBlock] = CRGB( PhaserRed, PhaserGreen, PhaserBlue);
// only center (odd width) or center 2 LEDs (even width) should be bright, others need to fade
if ( ( (PhaserCenterWidth==1) && (PhaserBlock!=PhaserHalf) ) ||
( (PhaserCenterWidth==2) && (PhaserBlock!=PhaserHalf-1) && (PhaserBlock!=PhaserHalf) ) )
{
leds[LEDPos+PhaserBlock].fadeLightBy( DimmingPhaserAim );
}
}
}
}
FastLED.show();
delay(msAimDelay);
}
// pulsing LEDs when firing phaser
LEDPos = PhaserAim;
PhaserHalf = ShootingWidth/2; // division will take integer part: 3/2 = 1.5 -> 1, 4/2 = 2 -> 2
PhaserCenterWidth = 2 - (ShootingWidth%2); // odd numbers: 1, even numbers: 2
setAll(0,0,0); // set all to black since shooting width may be different than aiming width
for(int counter=0; counter<PulsateAmount; counter++) {
// Set phaser at aim position to the usual brightness
for (int PhaserBlock = 0; PhaserBlock<ShootingWidth; PhaserBlock++)
{
leds[LEDPos+PhaserBlock] = CRGB( PhaserRed, PhaserGreen, PhaserBlue);
// only center (odd width) or center 2 LEDs (even width) should be bright, others need to fade
if ( ( (PhaserCenterWidth==1) && (PhaserBlock!=PhaserHalf) ) ||
( (PhaserCenterWidth==2) && (PhaserBlock!=PhaserHalf-1) && (PhaserBlock!=PhaserHalf) ) )
{
leds[LEDPos+PhaserBlock].fadeLightBy( DimmingShoot );
}
}
FastLED.show();
delay(msPulsateDelayBright);
// Make the outer LEDs pulsate (not the center LED or LEDs)
for (int PhaserBlock = 0; PhaserBlock<ShootingWidth; PhaserBlock++)
{
if ( ( (PhaserCenterWidth==1) && (PhaserBlock!=PhaserHalf) ) ||
( (PhaserCenterWidth==2) && (PhaserBlock!=PhaserHalf-1) && (PhaserBlock!=PhaserHalf) ) )
{
leds[LEDPos+PhaserBlock].fadeLightBy( DimmingPulsing );
}
}
FastLED.show();
delay(msPulsateDelayDarker);
}
setAll(0,0,0);
delay(500);
}
// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
leds[i] = CRGB(red, green, blue);
}
FastLED.show();
}