I am trying to control multiple DF players independently with help of <softwareserial.h> library from 1 Arduino.
In my code even though I succeed to play and pause only the first DF player with sending 'A' and 'B' commands from Serial Monitor, when I try to send play command to second DF player both players are start to playing. So I think ...Serial.end() comment is useless. I also try to use SoftwareSerial: listen() command ,but because I transmit data, not receiving ( It says that command for receiving from selected port) I failed again. I am open for any solution. Thanks...
#include "SoftwareSerial.h"
SoftwareSerial mySerial(12, 13);
SoftwareSerial mySerial2(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
# define ACTIVATED LOW
String incomingStr = "";
void setup () {
Serial.begin(9600);
}
void loop () {
while ( Serial.available( ) ) {
incomingStr = Serial.readString();
if (incomingStr.charAt(0) == 'A')
{
mySerial.begin(9600);
play();
delay(1000);
mySerial.end();
}
if (incomingStr.charAt(0) == 'B')
{
mySerial.begin(9600);
pause();
delay(1000);
mySerial.end();
}
if (incomingStr.charAt(0) == 'C')
{
mySerial2.begin(9600);
play();
delay(1000);
mySerial2.end();
}
if (incomingStr.charAt(0) == 'D')
{
mySerial2.begin(9600);
pause();
delay(1000);
mySerial2.end();
}
}
}
void pause()
{
execute_CMD(0x0E, 0, 0);
delay(500);
}
void play()
{
execute_CMD(0x0D, 0, 1);
delay(500);
}
void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte
};
//Send the command line to the module
for (byte k = 0; k < 10; k++)
{
mySerial.write( Command_line[k]);
mySerial2.write( Command_line[k]);
}
}