The included code works ok (plays the files on the SD card) when I connect the DFRobotDFPlayer to a UNO but will not work when I connect the player to a Nano.
I am using the same pin designations on the Uno and the Nano.
Obviously I have either missed something or there is a subtle difference between the Nano and the Uno that I am not aware of.
Hope someone can help me.
The code uploads without any problems.
/*
The sound effects were recorded from YouTube videos using Audacity.
My sketch plays old time radio music, looping until a trigger event,
from a switch or motion sensor that starts the sequence.
Music continues for a while, then white and blue LED's flicker to simulate arc welding,
and the arc welding sound file plays for a random time.
When the welding stops, the red LED comes on and fades to simulate the hot metal
afterglow, no sound. This time is enough for the welder to pick up his hammer.
Next the slag chipping sound file plays, stops, and plays again.
Then the wire brush sound file plays to simulate cleaning up the slag, and stops.
After time enough for the guy to put down the wire brush and pick up the grinder,
the 2 yellow LED's start to flicker grinder sparks,
and the grinder sound file plays for a random time.
The whole sequence repeats once more, then stops, and everything goes silent.
After a short time, the old time radio music starts playing again,
and loops until the next trigger event.
Script for Full Featured Welding Simulator w/Sound:
MP3 File 5 for 41 seconds of old time radio music after trigger event
Welding (white and blue leds flashing with MP3 File 1 welding sounds)
Hot Metal Afterglow (red led fading no sound)
MP3 File 2 slag chipping 2 times
MP3 File 3 wire brushing 1 time
Grinding the weld smooth (2 yellow leds flashing with MP3 File 4 grinding sounds)
XX seconds after cycles is shutdown, the MP3 File 5 loops 41 seconds until next trigger
Interrupt driven sensor should only play on a RISING edge single.
I.E, when a light sensor is covered.
Adapted from code by Ben (thewag on Trainboard) pieced together from multiple sources,
including Toms Trains and Things, original idea by fernpoint on Model Railroad Hobbiest,
and suggestions from Marc, an actual career professional welder. Thanks to you all!
Last Modified 04/27/2020, needs comments for each line so I can understand
what is happening
*/
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
//Digital Pins
int TRIGGER = 2; // Interrupt 0 -- Sensor
int RLED = 3; // 330 Ohm / Red LED for slag glow on pin D3 PWM
int BLED = 4; // 330 Ohm / Blue LED for arc strike on pin D4
int WLED = 5; // 330 Ohm / White LED for welder flash on pin D5
int ALED = 6; // 330 Ohm / Amber LED for grinder sparks on pin D6
int YLED = 7; // 330 Ohm / Yellow LED for grinder sparks on pin D7
int PLAYER_RX = 10; // Player Receive pin D10.
int PLAYER_TX = 11; // Player Transmit pin D11.
int PLAYER_BUSY = 12; // MP3 player status pin. LOW means busy playing.
int RUN = 0; // Running status
int CYCLE_COUNT = 2; // Cycles to run the welder per trigger event
int SHUTDOWN_SOUND_TRACK_NUMBER = 5;
SoftwareSerial mySoftwareSerial(PLAYER_RX, PLAYER_TX);
DFRobotDFPlayerMini MP3;
void setup() {
attachInterrupt(digitalPinToInterrupt(TRIGGER), sensorTiggered, RISING); //May be RISING, FALLING, or CHANGE
pinMode (PLAYER_BUSY, INPUT);
pinMode (WLED, OUTPUT);
pinMode (BLED, OUTPUT);
pinMode (RLED, OUTPUT);
pinMode (ALED, OUTPUT);
pinMode (YLED, OUTPUT);
//test flash RLED
digitalWrite(RLED, HIGH);
delay(500);
digitalWrite(RLED, LOW);
mySoftwareSerial.begin(9600);
//see if Player begins
if (!MP3.begin(mySoftwareSerial, true, true)){
// flash white LED if MP3 not started
digitalWrite(WLED, HIGH);
delay(500);
digitalWrite(WLED, LOW);
delay(500);
digitalWrite(WLED, HIGH);
delay(500);
digitalWrite(WLED, LOW);
} else{ //flash blue LED if MP3 started
digitalWrite(BLED, HIGH);
delay(500);
digitalWrite(BLED, LOW);
delay(500);
digitalWrite(BLED, HIGH);
delay(500);
digitalWrite(BLED, LOW);
}
//MP3.begin(mySoftwareSerial);
MP3.setTimeOut(500);
MP3.volume(25);
MP3.EQ(DFPLAYER_EQ_BASS);
MP3.outputDevice(DFPLAYER_DEVICE_SD);
}
void loop() {
MP3.play(5);
delay(10000);
// Test condition again to start the weld procedure
if (RUN) {
for (int x = 1; x <= CYCLE_COUNT; x++) {
welder(); // White/Blue flicker and MP3 track 1
afterglow(); // Red weld afterglow 2 to 3 seconds, no sound
grinder();
}
}
RUN = 0; //After CycleCount reaches limit
shut_down(); //Sets all LED's off
}
void sensorTiggered()
{
RUN = 1;
}
void welder() {
delay(3000);
MP3.play(1); // Random White/Blue flashing with track 1 playing
int cycle = random(80, 160); // Randomly sets the cycle time of the LEDs
for (int i = 0; i < cycle; i++)
{
delay(1000); //allow sound to start same as leds
digitalWrite(WLED, HIGH);
delay(random(10));
digitalWrite(WLED, LOW);
delay(random(200));
digitalWrite(BLED, HIGH);
delay(random(10));
digitalWrite(BLED, LOW);
delay(random(100));
if (digitalRead(PLAYER_BUSY) == HIGH) { // HIGH not playing
MP3.play(1); // Restart if sound has stopped
}
}
MP3.stop();
}
void afterglow() { // Weld afterglow starts after weld stops and then dims no sound
for (int j = 80; j > 1; j--) { // Variable int=j sets afterglow intensity, j > 1 sets ending glow, j-- decreases intensity
analogWrite(RLED, j); // Sets RLED to current value of j
delay(80); // Sets delay between each RLED intensity decrease
}
}
void grinder() {
MP3.play(2);
delay(5000);
MP3.play(2);
delay(2000);
MP3.play(3);
delay(12000);
MP3.play(4);
// Randomly sets the cycle time of the LEDs
int cycle = random(100, 200);
for (int i = 0; i < cycle; i++)
{
digitalWrite(ALED, HIGH);
delay(random(20));
digitalWrite(ALED, LOW);
delay(random(200));
digitalWrite(YLED, HIGH);
delay(random(10));
digitalWrite(YLED, LOW);
delay(random(100));
if (digitalRead(PLAYER_BUSY) == HIGH) { // HIGH not playing
// Restart if sound has stopped
MP3.play(4);
}
}
MP3.stop();
}
void shut_down() {
// Turn off all LEDs
digitalWrite(WLED, LOW);
digitalWrite(BLED, LOW);
digitalWrite(RLED, LOW);
digitalWrite(ALED, LOW);
digitalWrite(YLED, LOW);
}