Porting Code from Arduino Zero to Arduino Uno

I am trying to compile a code that was written for the arduino zero and would like to run the same for an arduino uno using Software serial. The device connected to is a RNBD451 BLE module and used the RNBD451 library for Arduino. When i compile the code i get the error "Serial1 was not declared in this scope"

What would be the ideal way to compile this code successfully? Any help on this would be greatly appreciated. The link to library is below: GitHub - MicrochipTech/RNBD451_BLE_ARDUINO_LIBRARY: "IoT Made Easy!" - Arduino library for using the Microchip RNBD451 BLE Plug and Play module.


#include <Arduino.h>
#include "rnbd.h"
#include "rnbd_interface.h"

#define DEFAULT_BAUDRATE 115200
#define SERIAL_BAUDRATE 115200
#define BLEserial Serial1
#define USBserial Serial
#define RST_PIN A3

bool Err;
bool initialize = false;
const char DevName[] = "RNBD451_PERIPHERAL";
uint8_t service_uuid = 0xC0;

typedef enum {
  /* TODO: Define states used by the application state machine. */
  RNBD_INIT,
  RNBD_FACTORY_RESET,
  RNBD_CMD,
  RNBD_CMD1,
  RNBD_SET_NAME,
  RNBD_SET_PROFILE,
  RNBD_REBOOT,
} STATES;

typedef struct
{
  /* The application's current state */
  STATES state;

} RNBD_STATE;

RNBD_STATE rnbd_state;
BLE BLE_RNBD;

void setup() {
  BLE_RNBD.setReset(RST_PIN);
  BLE_RNBD.initBleStream(&BLEserial);
  //Arduino UART Serial
  USBserial.begin(SERIAL_BAUDRATE);
  //RNBD UART Serial
  BLEserial.begin(DEFAULT_BAUDRATE);
  delay(1000);
  USBserial.println("RNBD451 PERIPHERAL");
  rnbd_state.state = RNBD_INIT;
  initialize = true;
}

void loop() {

  if (initialize == true) {
    RNBD_PERIPHERAL();
  } else {
    serial_transfer();
  }
}

void serial_transfer() {

  // read from RNBD451 and Print on Arduino Zero
  if (BLEserial.available()) {
    String BU_data = BLEserial.readString();
    USBserial.println(BU_data);
  }

  // read from Arduino Zero and Print on RNBD451
  if (USBserial.available()) {
    String AR_data = USBserial.readString();
    BLEserial.print(AR_data);
  }
}

void RNBD_PERIPHERAL() {

  switch (rnbd_state.state) {
    case RNBD_INIT:
      {
        Err = BLE_RNBD.RNBD_Init();
        if (Err) {
          Err = false;
          USBserial.println("RNBD451_INITIALIZING");
          rnbd_state.state = RNBD_CMD;
        }
      }
      break;
    case RNBD_CMD:
      {
        Err = BLE_RNBD.RNBD_EnterCmdMode();
        if (Err) {
          Err = false;
          USBserial.println("Entered CMD Mode");
          rnbd_state.state = RNBD_FACTORY_RESET;
        }
      }
      break;
    case RNBD_FACTORY_RESET:
      {
        Err = BLE_RNBD.RNBD_FactoryReset();
        RNBD.DelayMs(1000);
        if (Err) {
          Err = false;
          USBserial.println("Factory Reset Done");
          rnbd_state.state = RNBD_CMD1;
        }
      }
      break;
    case RNBD_CMD1:
      {
        Err = BLE_RNBD.RNBD_EnterCmdMode();
        if (Err) {
          Err = false;
          USBserial.println("Entered CMD Mode");
          rnbd_state.state = RNBD_SET_NAME;
        }
      }
      break;
    case RNBD_SET_NAME:
      {
        Err = BLE_RNBD.RNBD_SetName(DevName, strlen(DevName));
        if (Err) {
          Err = false;
          USBserial.println("Device Name Set");
          rnbd_state.state = RNBD_SET_PROFILE;
        }
      }
      break;
    case RNBD_SET_PROFILE:
      {
        Err = BLE_RNBD.RNBD_SetServiceBitmap(service_uuid);
        if (Err) {
          Err = false;
          USBserial.println("Service Bitmap Set");
          rnbd_state.state = RNBD_REBOOT;
        }
      }
      break;
    case RNBD_REBOOT:
      {
        Err = BLE_RNBD.RNBD_RebootCmd();
        RNBD.DelayMs(1500);
        if (Err) {
          Err = false;
          USBserial.println("Reboot Completed");
          initialize = false;
          serialFlush();
          USBserial.println("!!! Started Advertising - Scan and Connect using MBD App !!!");
        }
      }
      break;
  }
}

void serialFlush() {
  while (BLEserial.available() > 0) {
    char rnbd_temp = BLEserial.read();
  }
  while (USBserial.available() > 0) {
    char serial_temp = USBserial.read();
  }
}

Welcome to the forum.

Where do you include the SoftwareSerial library and where is the SoftwareSerial object declared ?

Hello Koepel,

Thank you for the reply. I have not included the library and the object in this code. This is the original example code from library. If i include the SoftwareSerial library and declare BLEserial as a myserial object i get more compilation errors. See below:

It seems that the library does not allow to use the serial ports for the Arduino Uno board, there is no "extern Uart" for the Arduino Uno.
You have to change the library.

Normally, a pointer to the Serial object is used:

myClass(HardwareSerial *serial);

But I don't know if that works for SoftwareSerial.