Problem with multiple SoftwareSerial

Hello everyone,
my civillian name is Adrie and I am from the Netherlands.
I am new here because I have a (probably) simple problem that I can't seem to fix.
I am trying to control a dfRobotMp3 through a HC-06 bluetooth and my phone.
I think I have already searched the entire internet but I still havn't found out what I am doing wrong. As you can see I've a very tiny sketch so far just for testing the Serial communication. In this case turning on and of a LED. Therefore I use a SoftwareSerial named btcom. That works fine. But when I add an extra SoftwareSerial, in this case named fake just for testing nothing happens. I read in many places on the internet that it is possible to use multiple SoftwareSerials. Could some of you give me a hint?


//  Arduino D3 to BT RX through a voltage divider
//  Arduino D2 BT TX (no need voltage divider)
//Testsketch twee SoftwareSerial lijnen

#include<SoftwareSerial.h>
SoftwareSerial btcom(2,3);  //Bluetooth SoftwareSerial
SoftwareSerial fake(10,11); // Fake SoftwareSerial
char command;
bool ledje = true;
int ledPin = 12;


void setup() {
  btcom.begin(9600); // set the baudrate for the bluetooth connection
  fake.begin(9600);  //set the baudrate for the "fake" connection
  Serial.begin(9600);//set the baudrate for the srialmonitor connection
  pinMode(ledPin,OUTPUT);

}

void loop() {
  if(btcom.available() > 0){
  command = btcom.read(); // command becomes wht I sent from my phone
  Serial.println(command); // for debugging

  if (command == 'a'){
    digitalWrite(12,ledje); //switch led on and of
    ledje =!ledje;          //
  }
}
}

With the softwareSerial commented I get the 'command' on my serial monitor an the LED works like is should but otherwise nothing appears on my serialmonitor and no working LED.
The compiler reports no bugs or mistakes.
I hope I have made myself a bit clear and did not make any mistakes in my first post on this forum. Otherwise I apologize for that.

Check the Arduino SoftwareSerial Reference, it provides details on how to use multiple SoftwareSerials

SoftwareSerial

You can only listen to one software serial port at a time. Since you start the fake port last it is the active port. If you want to listen to the btcom port you have to set it to be the one listened to.
See the two port receive example from the SoftwareSerial library
.

But realise that use of multiple SoftwareSerial instances is fraught with difficulty due to the fact that you can use only one instance at a time, have to explicitly switch between them and that if data is received on an inactive instance it will be discarded

Which Arduino board are you using ?

Hello
If do you have the functional requirement of two serial interfaces than you shall use an Arduino Mega.
The Arduino Mega has three additional serial ports: Serial1 on pins 19 (RX) and 18 (TX), Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX).
Alsjeblieft :grinning:

I am now using an Arduino Uno just to test things but eventually I will use an Arduino pro mini for my project.

I am with @ paulpaulson. If you are going to use more than 2 serial ports a Mega, with its 3 extra hardware serial ports) would be a big improvement over the Uno. Besides the convenience, using hardware serial ports will allow much faster baud rates. If the size of the Mega is a problem, there is the Mega Pro that is much smaller.

Thanks for your advice, but A, I still have a couple of pro mini's laying around here and I am Dutch so I hate to spend money :smiley:. B, I am rather stubborn and C, it should be possible and I'got the time to figure it out. I will let you know....

Do the separate serial communications, particularly messages to be received by the Arduino, occur asynchronously ?

If so then you can forget using multiple instances of SoftwareSerial unless you don't mind missing some data

A typical dutch man - Groningen or Zeeland?
Well you can build a multi pro mini system coupled via I²C and each of them has one serial interface to be handled.

There are sooooooo many MASSIVELY better boards than the ProMini to work with these days, for nearly the same money, and many/most can be programmed using Arduino tools and libraries. Many of them will easily support multiple serial ports. Why make your like more difficult just to save a few (and I mean REALLY only a few!) dollars/pounds/euros/kroner/whatever? Pro Minis were great 5-10 years ago, but there is so much more choice now.

My ultimate goal is to build a kind of "onboardcomputer" for my motorbike, to start and stop it via bluetooth. I also want to be able to play some soundbites through the Mp3 player with a small amplifier like a countdown from ten to zero before it starts. As well as "stand back"or "Don't touch me". I already use a I2C communicatation for the LCD that displays the battery voltage and the outside temperature.
And for paulpaulson, I live in Assen. :slightly_smiling_face:

//  Arduino D3 to BT RX through a voltage divider
//  Arduino D2 BT TX (no need voltage divider)
//  Arduino D11 to dfRobot RX 
//  Arduino D10 to dfRobot TX 
#include <Arduino.h>
#include <DFRobotDFPlayerMini.h>
#include <SoftwareSerial.h>
SoftwareSerial mySoftwareSerial(10,11); //RX, TX
SoftwareSerial btcom(2,3); //RX, TX
DFRobotDFPlayerMini  Mp3speler;
char commando;   

void setup(){  
  mySoftwareSerial.begin(9600);
  btcom.begin(9600);
  Serial.begin(9600);
  Mp3speler.volume(20);
  pinMode(7,OUTPUT);
  
}

void loop(){
  btcom.listen();
  if(btcom.available()>0){
    commando = btcom.read();    
  
  Serial.print("commando is   ");
  Serial.println(commando);
  digitalWrite(7,1);

  }
}

This is another testcode,
when I run this code I don't get messages in my serial monitor but when I comment the line digitalWrite(7,1); out the messages appear on the monitor. So for some reason the last line prevents the serial.print commando to be executed somehow.

Pin 1 is used by the Serial interface so no wonder setting it permanently HIGH stops serial communications

Thanks for all your help, it works so far. Now I can go further.
I have changed the order of some lines and tried and changed and renamed some more thing and now I can control the Mp3 player through my phone. I am getting somewhere.
I have put my code below for anyone who is interested.

//  Arduino D3 to BT RX through a voltage divider
//  Arduino D2 BT TX (no need voltage divider)
//  Arduino D11 to dfRobot RX 
//  Arduino D10 to dfRobot TX 
#include <DFRobotDFPlayerMini.h>
#include <SoftwareSerial.h>

SoftwareSerial Mp3com(10,11);    //RX, TX, communicate with Mp3 player
SoftwareSerial btcom(2,3);       //RX, TX, communicate with bluetooth
DFRobotDFPlayerMini  Mp3speler;  //name Mp3 player
char command;                    // command variable from bluetooth


void setup(){ 
  Mp3com.begin(9600);            //set Mp3 Player baudrate       
  btcom.begin(9600);             //set bluetooth baudrate
   Mp3speler.begin(Mp3com);      //Use Mp3com to communicate with mp3.
   Mp3speler.volume(30);
}


void loop(){
  btcom.listen();
  if(btcom.available() > 0){
    command = btcom.read();    
    speler();
  }
}



void speler(){                  //function to control Mp3 player
   switch(command){
    case 'a':
      Mp3speler.start();   
    break;
    
    case 'b':
      Mp3speler.stop();    
    break;
    
    case 'c':
      Mp3speler.next();
    break;
    
    case 'd':
      Mp3speler.previous();
    break;

    case 'e':
      Mp3speler.volumeDown();
    break;

    case 'f':
      Mp3speler.volumeUp();
    break;
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.