I'm using arduino nano board, and I'm trying to play two different music simultaneously. One dfplayer has to be controlled by a touch sensor, and another dfplayer will be controlled by the distance sensor. But I don't know how to connect 2 dfplayers with the nano board. Now I connected one dfplayer.
//Very much inspired by https://www.dfrobot.com/blog-1462.html by DFRobot Feb 26 2020
//Additions made by Just Baselmans https://www.youtube.com/justbaselmansYT Jan 23 2023
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
// Initialize software serial on pins 10 and 11
SoftwareSerial mySoftwareSerial1(10, 11);
SoftwareSerial mySoftwareSerial2(7, 8);
DFRobotDFPlayerMini myDFPlayer;
String line;
char command;
int dis1 = A1;
int touch = A2;
int track = 1;
void setup() {
// Serial communication with the module
mySoftwareSerial1.begin(9600);
mySoftwareSerial2.begin(9600);
// Initialize Arduino serial
Serial.begin(115200);
analogReference(DEFAULT);
pinMode (dis1, INPUT);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini"));
Serial.println(F("Initializing DFPlayer module ... Wait!"));
if (!myDFPlayer.begin(mySoftwareSerial1)) {
Serial.println(F("Not initialized:"));
Serial.println(F("1. Check the DFPlayer Mini connections"));
Serial.println(F("2. Insert an SD card"));
while (true)
;
}
Serial.println();
Serial.println(F("DFPlayer Mini module initialized!"));
//myDFPlayer.setTimeOut(500);
myDFPlayer.volume(15);
myDFPlayer.EQ(0);
}
void loop() {
command = Serial.peek();
line = Serial.readStringUntil('\n');
int raw1=analogRead(dis1);
int touchval = analogRead(touch);
//Serial.println(raw1);
//Serial.println(touchval);
delay(100);
int range = map(raw1, 220, 700, 0, 7);
switch (range) {
case 0:
myDFPlayer.play(1);
Serial.println("Range 8");
break;
case 1:
myDFPlayer.play(2);
Serial.println("Range 7");
break;
case 2:
myDFPlayer.play(3);
Serial.println("Range 6");
break;
case 3:
myDFPlayer.play(4);
Serial.println("Range 5");
break;
case 4:
myDFPlayer.play(5);
Serial.println("Range 4");
break;
case 5:
myDFPlayer.play(6);
Serial.println("Range 3");
break;
case 6:
myDFPlayer.play(7);
Serial.println("Range 2");
break;
case 7:
myDFPlayer.play(8);
Serial.println("Range 1");
break;
}
}