Arduino UNO - switching two devices using one serial port

My project is an MP3 player controlled by voice recognition. I know that I need Mega so that each module has its own HW serial port but before it arrives I'm curious whether it could work with UNO as well.

I know two modules can't communicate using SoftwareSerial at the same time but what if I don't need them to work simoultanously?

/1/ Start listenning for voice command, process it
/2/ Stop listenning
/3/ Play MP3
/4/ Stop playing MP3
/5/ Start listenning for the next voice command

Is this possible using SoftwareSerial?

Each module works fine individually but not together.

Modules:

Code:


#include "SerialMP3Player.h"
#include "VoiceRecognitionV3.h"

// MP3
#define TX 10
#define RX 11
#define CMD_PLAY_W_INDEX 0X03
#define CMD_PLAY_FOLDER_FILE 0X0f
SerialMP3Player mp3(RX, TX);

// VOICEC RECOGNITION
VR myVR(3, 2);       
uint8_t records[7];  
uint8_t buf[64];
#define meRecord (0)
#define himRecord (1)
#define undoRecord (2)
#define scoreRecord (3)
#define serveRecord (4)


void setup() {
  /* MP3 START */
  Serial.begin(9600);
  mp3.begin(9600);                    
  delay(500);                          
  mp3.sendCommand(CMD_SEL_DEV, 0, 2);  
  delay(500);                         
  /* MP3 END */

  /* VR START */
  myVR.begin(9600);
  Serial.begin(115200);
  Serial.println("Elechouse Voice Recognition V3 Module\r\nControl LED sample");

  if (myVR.clear() == 0) {
    Serial.println("Recognizer cleared.");
  } else {
    Serial.println("Not find VoiceRecognitionModule.");
    Serial.println("Please check connection and restart Arduino.");
    while (1)
      ;
  }

  if (myVR.load((uint8_t)meRecord) >= 0) {
    Serial.println("meRecord loaded");
  }
  if (myVR.load((uint8_t)himRecord) >= 0) {
    Serial.println("himRecord loaded");
  }
  if (myVR.load((uint8_t)scoreRecord) >= 0) {
    Serial.println("scoreRecord loaded");
  }
  if (myVR.load((uint8_t)serveRecord) >= 0) {
    Serial.println("serveRecord loaded");
  }
  /* VR END */  
}



void loop() {
  mp3.sendCommand(CMD_PLAY_FOLDER_FILE, 02, 0x01);
  delay(2000);
  
  int ret;
  ret = myVR.recognize(buf, 50);
  //Serial.println(String(ret));
  if (ret > 0) {
    switch (buf[1]) {
      case meRecord:
        Serial.println("ME POINT");
        //addPoint(player1, player2);
        break;
      case himRecord:
        Serial.println("HIM POINT");
        break;
      default:
        Serial.println("Record function undefined");
        break;
    }
  }  
}



Yes it is. Have a look at the Listen function in the SoftwareSerial library. It allows you to specify which SoftwareSerial port you want to listen for incoming data.

However, you may have to look at the actual libraries for your MP3 and voice recognition modules to see if they declare the SoftwareSerial port themselves or whether you can pass a SoftwareSerial object to them. If they handle a software serial port themselves, then you need to see if they have an equivalent of the listen function.

If they don't support a listen function, then you may be able to disable and enable each in turn with their respective begin and end functions.

1 Like

That's cool! Needed to tweak the library a bit (add listen() function) but it works great. Thanks!

1 Like

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