Porting UNO code over to Beetle

Hi Arduino Friends
I am still a novice with Arduino, but I am trying to learn as much as I can, I am in my late sixties and my brain does not work as well as it used to! I have been trying to port the following UNO (which works) code over to the DFRobot Beetle (Leonardo) but just cannot fathom how to do it, everything I have tried has failed? If anyone could help, it would be much appreciated.
Tony


#include "DFRobot_DF2301Q.h"

#define Led 8

/**
   @brief DFRobot_URM13_RTU constructor
   @param serial - serial ports for communication, supporting hard and soft serial ports
   @param rx - UART The pin for receiving data
   @param tx - UART The pin for transmitting data
*/

#if (defined(ARDUINO_AVR_UNO) || defined(ESP8266))  // Use software serial
SoftwareSerial softSerial(/*rx=*/9, /*tx=*/10);     // Using D9 (RX) and D10 (TX)
DFRobot_DF2301Q_UART asr(/*softSerial =*/&softSerial);
#elif defined(ESP32)  // Use the hardware serial with remappable pin: Serial1
DFRobot_DF2301Q_UART asr(/*hardSerial =*/&Serial1, /*rx =*/D3, /*tx =*/D2);
#else                 // Use hardware serial: Serial1
DFRobot_DF2301Q_UART asr(/*hardSerial =*/&Serial1);
#endif

unsigned long previousResetTime = 0;
const unsigned long resetInterval = 120000; // 60 seconds in milliseconds
// ****************************************************************************
/////////////////////////////////////////////////
//-- Setup ------------------------------------//
/////////////////////////////////////////////////
void setup() {
  Serial.begin(115200);

  pinMode(Led, OUTPUT);    //Init LED pin to output mode 
  digitalWrite(Led, LOW);  //Set LED pin to low 
// **********************************************
  // Init the sensor
  while (!(asr.begin())) {
    Serial.println("Communication with device failed, please check connection");
    delay(3000);
  }
  Serial.println("Begin ok!");
// **********************************************
  /**
     @brief Reset module 
  */
  // asr.resetModule();

  /**
     @brief Set commands of the module
     @param setType - Set type
     @n       DF2301Q_UART_MSG_CMD_SET_VOLUME: Set volume, the set value range 1-7 
     @n       DF2301Q_UART_MSG_CMD_SET_ENTERWAKEUP: Enter wake-up state; set value 0
     @n       DF2301Q_UART_MSG_CMD_SET_MUTE Mute mode; set value 1: mute, 0: unmute
     @n       DF2301Q_UART_MSG_CMD_SET_WAKE_TIME ; Wake-up duration; the set value range 0-255s
     @param setValue - Set value, refer to the set type above for the range
  */
  asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_MUTE, 0);
  asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_VOLUME, 12);
  asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_WAKE_TIME, 5);
  //asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_ENTERWAKEUP, 0);

  /**
     @brief Play the corresponding reply audio according to the command word ID
     @param CMDID - Command word ID
  */
  asr.playByCMDID(23);
  
  previousResetTime = millis(); // Initialize the timer
}
// ****************************************************************************
/////////////////////////////////////////////////
//-- Principal Loop ---------------------------//
/////////////////////////////////////////////////
void loop() {
  // Check if 30 seconds have passed for the reset
  unsigned long currentTime = millis();
  if (currentTime - previousResetTime >= resetInterval) {
    asr.resetModule();
    previousResetTime = currentTime; // Reset the timer
    Serial.println("VR Module reset");
  }
// **********************************************
  /**
     @brief  Get the ID corresponding to the command word 
     @return Return the obtained command word ID, returning 0 means no valid ID is obtained
  */
  uint8_t CMDID = asr.getCMDID();
  switch (CMDID) {
    case 103:                                                      //If the command is "Turn on the light"
      digitalWrite(Led, HIGH);                                     //Turn on the LED
      Serial.println("received'Turn on the light',command flag'103'");  //Serial transmits "received"Turn on the light",command flag"103""
      break;

    case 104:                                                      //If the command is "Turn off the light"
      digitalWrite(Led, LOW);                                      //Turn off the LED
      Serial.println("received'Turn off the light',command flag'104'");  //The serial transmits "received"Turn off the light",command flag"104""
      break;

    default:
      if (CMDID != 0) {
        Serial.print("CMDID = ");  //Print command ID
        Serial.println(CMDID);
      }
  }
  
  delay(300); // Small delay to prevent CPU overload
}
// ****************************************************************************
// END

In what way does it fail ?

1 Like

With the Beetle (Leonardo), the Arduino serial monitor does not show the VR module's output - if I set it back to UNO then the serial monitor works and shows the words just recognised. Thanks for replying!

Given that you're no longer using an Uno, which of these 3 definitions do you think would apply? And is your device hooked up to the appropriate pins to match that definition?

1 Like

I have tried changing the settings, but I just get errors that I do not understand? - pins matched up and changed the board to Leonardo (this works with the beetle) - Beetle board is recognised? Thanks for replying!

You get errors you can't understand, and that we can't see.

As I child, I didn't pay attention in mind reading class. :slight_smile: So unless you show me, I can't know.

I rewrote the code to Leonardo (Beetle use) here is the error that I get? Thanks for any advice!

Here is the original part that works on the UNO?

/**
   @brief DFRobot_URM13_RTU constructor
   @param serial - serial ports for communication, supporting hard and soft serial ports
   @param rx - UART The pin for receiving data
   @param tx - UART The pin for transmitting data
*/
#if (defined(ARDUINO_AVR_UNO) || defined(ESP8266))  // Use software serial
SoftwareSerial softSerial(/*rx =*/4, /*tx =*/5);
DFRobot_DF2301Q_UART asr(/*softSerial =*/&softSerial);
#elif defined(ESP32)  // Use the hardware serial with remappable pin: Serial1
DFRobot_DF2301Q_UART asr(/*hardSerial =*/&Serial1, /*rx =*/D3, /*tx =*/D2);
#else                 // Use hardware serial: Serial1
DFRobot_DF2301Q_UART asr(/*hardSerial =*/&Serial1);
#endif

It will work on the Beetle/Leonardo, too, but on pins 0 & 1 (RX & TX).

Thanks for the reply, but I need 2 serial ports on the project

You have Serial (native USB) and Serial1 (USART). What more do you need?

Thats my problem - on the UNO code I have 2 serial ports (hard and soft) that works perfectly, but when I port over to leonardo (Beetle) the Serial monitor does not work?

On the Leonardo the Serial port does not use pins 0 and 1. They are used by Serial1

So, instead of using SoftwareSerial use Serial for the Serial monitor and Serial1 for the DFRobot_DF2301Q_UART

If I were you I would get rid of the #if defined tests and hard code the use of Serial1 with the DFRobot_DF2301Q_UART device connected to pins 0 and 1

Thank you all so much for helping me, as I said, I am a novice Arduino user so your advice is invaluable!

The situation is:- I need a buffer micro between the VR Module (VRM) and a master micro - the main board is small (so not much real estate) hence using a micro Arduino Beetle. There is a bad bug on the VRM where after every 15 minutes it will not recognise the custom wake-word. This makes it virtually useless for my project. After many hours of research I found that on the VRM (serial only not I2C) there is a reset < asr.resetModule(); >. I found that (on the UNO code) polling the VRM every few minutes (with the reset), then the wake-word does not get stopped solving the problem.

The beetle is programmed as if it is a Leonardo but with very few I/O see:- Beetle_SKU_DFR0282-DFRobot

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