SoftwareSerial.h doesn't work with Nano33BLE but called on in many examples

I have been working on the adafruit sound fx, I just wanted ask about the SoftwareSerial.h library it asks for. I tried to load the file but won't compile on the Nano33BLE.

Can you suggest how to rework the Adafruit sketch to work on a Nano33BLE, I can’t go further with the project. Even if it means writing code using Serial1 could someone explain or put some code on how to do this as I am unsure as never used this function before.

One other question, with the Nano33BLE, is it necessary and if yes, how do I calibrate the IMU? And is needed only once during initial setup or every time used?
Thanks

use hardware Serial1. it is on pins labeled RX and TX.

Serial1.begin(BAUD_RATE);

I could not find the link to your IMU so I used mine. Calibration is only required every 6 months, or after a rough handling, hard landings, or if your Drone is requiring an IMU calibration. The drone will inform you if calibration is required before flight, so there is no second guessing! For calibration follow the instructions that came with it.

Thanks for the reply on Calibration. I am using it for motion detection and angular measurement for a project I am making for my dog. I was wondering if there is coding I need to add to my sketch that will indicate when it requires calibration, how to set i in calibration mode(say by a push button) and if coding needs to be added to the Nano33BLE to enable it to calibrate.

I know with INAV for my drone that is all built into the firmware. But I am new to arduino and haven't had luck finding anything about this online yet.

I have been trying to get the Adafruit SoundFX board working via TX/RX on Nano33BLE which I have found out is Serial1. here isn't much info on this as it is different to other Arduino modules. I am having trouble with the code, it says I have not declared in the scope:

Arduino: 1.8.16 (Windows Store 1.8.51.0) (Windows 10), Board: "Arduino Nano 33 BLE"

> CyclopsV2_I2C_coding_light:52:48: error: 'Serial1' was not declared in this scope
> 
>  Adafruit_Soundboard sfx = Adafruit_Soundboard(&Serial1, NULL, SFX_RST);
> 
>                                                 ^~~~~~~
> 
> C:\Users\KevB\Documents\Arduino\CyclopsV2_I2C_coding_light\CyclopsV2_I2C_coding_light.ino:52:48: note: suggested alternative: 'serial_s'
> 
>  Adafruit_Soundboard sfx = Adafruit_Soundboard(&Serial1, NULL, SFX_RST);
> 
>                                                 ^~~~~~~
> 
>                                                 serial_s
> 
> C:\Users\KevB\Documents\Arduino\CyclopsV2_I2C_coding_light\CyclopsV2_I2C_coding_light.ino: In function 'void setup()':
> 
> CyclopsV2_I2C_coding_light:129:4: error: 'Serial1' was not declared in this scope
> 
>  {  Serial1.begin(9600);
> 
>     ^~~~~~~
> 
> C:\Users\KevB\Documents\Arduino\CyclopsV2_I2C_coding_light\CyclopsV2_I2C_coding_light.ino:129:4: note: suggested alternative: 'serial_s'
> 
>  {  Serial1.begin(9600);
> 
>     ^~~~~~~
> 
>     serial_s
> 
> C:\Users\KevB\Documents\Arduino\CyclopsV2_I2C_coding_light\CyclopsV2_I2C_coding_light.ino: At global scope:
> 
> CyclopsV2_I2C_coding_light:140:1: error: expected unqualified-id before '{' token
> 
>  {
> 
>  ^
> 
> CyclopsV2_I2C_coding_light:168:1: error: expected unqualified-id before '{' token
> 
>  {
> 
>  ^
> 
> CyclopsV2_I2C_coding_light:193:1: error: expected declaration before '}' token
> 
>  }
> 
>  ^
> 
> exit status 1
> 
> 'Serial1' was not declared in this scope

Arduino forum explanation, but no example. OK for someone experienced but not for a nubie

Can someone simply explain how to declare and setup Serial1 to write(TX) to the adafruitSFX(RX) and read SFX(RX) to (BLE)TX?

This is the example given by Adafruit:

> /* 
>   Menu driven control of a sound board over UART.
>   Commands for playing by # or by name (full 11-char name)
>   Hard reset and List files (when not playing audio)
>   Vol + and - (only when not playing audio)
>   Pause, unpause, quit playing (when playing audio)
>   Current play time, and bytes remaining & total bytes (when playing audio)
> 
>   Connect UG to ground to have the sound board boot into UART mode
> */
> 
> #include <SoftwareSerial.h>
> #include "Adafruit_Soundboard.h"
> 
> 
> // Choose any two pins that can be used with SoftwareSerial to RX & TX
> #define SFX_TX 5
> #define SFX_RX 6
> 
> // Connect to the RST pin on the Sound Board
> #define SFX_RST 4
> 
> // You can also monitor the ACT pin for when audio is playing!
> 
> // we'll be using software serial
> SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
> 
> // pass the software serial to Adafruit_soundboard, the second
> // argument is the debug port (not used really) and the third 
> // arg is the reset pin
> Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);
> // can also try hardware serial with
> // Adafruit_Soundboard sfx = Adafruit_Soundboard(&Serial1, NULL, SFX_RST);
> 
> void setup() {
>   Serial.begin(115200);
>   Serial.println("Adafruit Sound Board!");
>   
>   // softwareserial at 9600 baud
>   ss.begin(9600);
>   // can also do Serial1.begin(9600)
> 
>   if (!sfx.reset()) {
>     Serial.println("Not found");
>     while (1);
>   }
>   Serial.println("SFX board found");
> }
> 
> 
> void loop() {
>   flushInput();
>   
>   Serial.println(F("What would you like to do?"));
>   Serial.println(F("[r] - reset"));
>   Serial.println(F("[+] - Vol +"));
>   Serial.println(F("[-] - Vol -"));
>   Serial.println(F("[L] - List files"));
>   Serial.println(F("[P] - play by file name"));
>   Serial.println(F("[#] - play by file number"));
>   Serial.println(F("[=] - pause playing"));
>   Serial.println(F("[>] - unpause playing"));
>   Serial.println(F("[q] - stop playing"));
>   Serial.println(F("[t] - playtime status"));
>   Serial.println(F("> "));
>   
>   while (!Serial.available());
>   char cmd = Serial.read();
>   
>   flushInput();
>   
>   switch (cmd) {
>     case 'r': {
>       if (!sfx.reset()) {
>         Serial.println("Reset failed");
>       }
>       break; 
>     }
>     
>     case 'L': {
>       uint8_t files = sfx.listFiles();
>     
>       Serial.println("File Listing");
>       Serial.println("========================");
>       Serial.println();
>       Serial.print("Found "); Serial.print(files); Serial.println(" Files");
>       for (uint8_t f=0; f<files; f++) {
>         Serial.print(f); 
>         Serial.print("\tname: "); Serial.print(sfx.fileName(f));
>         Serial.print("\tsize: "); Serial.println(sfx.fileSize(f));
>       }
>       Serial.println("========================");
>       break; 
>     }
>     
>     case '#': {
>       Serial.print("Enter track #");
>       uint8_t n = readnumber();
> 
>       Serial.print("\nPlaying track #"); Serial.println(n);
>       if (! sfx.playTrack((uint8_t)n) ) {
>         Serial.println("Failed to play track?");
>       }
>       break;
>     }
>     
>     case 'P': {
>       Serial.print("Enter track name (full 12 character name!) >");
>       char name[20];
>       readline(name, 20);
> 
>       Serial.print("\nPlaying track \""); Serial.print(name); Serial.print("\"");
>       if (! sfx.playTrack(name) ) {
>         Serial.println("Failed to play track?");
>       }
>       break;
>    }
> 
>    case '+': {
>       Serial.println("Vol up...");
>       uint16_t v;
>       if (! (v = sfx.volUp()) ) {
>         Serial.println("Failed to adjust");
>       } else {
>         Serial.print("Volume: "); Serial.println(v);
>       }
>       break;
>    }
> 
>    case '-': {
>       Serial.println("Vol down...");
>       uint16_t v;
>       if (! (v=sfx.volDown()) ) {
>         Serial.println("Failed to adjust");
>       } else { 
>         Serial.print("Volume: "); 
>         Serial.println(v);
>       }
>       break;
>    }
>    
>    case '=': {
>       Serial.println("Pausing...");
>       if (! sfx.pause() ) Serial.println("Failed to pause");
>       break;
>    }
>    
>    case '>': {
>       Serial.println("Unpausing...");
>       if (! sfx.unpause() ) Serial.println("Failed to unpause");
>       break;
>    }
>    
>    case 'q': {
>       Serial.println("Stopping...");
>       if (! sfx.stop() ) Serial.println("Failed to stop");
>       break;
>    }  
> 
>    case 't': {
>       Serial.print("Track time: ");
>       uint32_t current, total;
>       if (! sfx.trackTime(&current, &total) ) Serial.println("Failed to query");
>       Serial.print(current); Serial.println(" seconds");
>       break;
>    }  
> 
>    case 's': {
>       Serial.print("Track size (bytes remaining/total): ");
>       uint32_t remain, total;
>       if (! sfx.trackSize(&remain, &total) ) 
>         Serial.println("Failed to query");
>       Serial.print(remain); Serial.print("/"); Serial.println(total); 
>       break;
>    }  
> 
>   }
> }
> 
> 
> 
> 
> 
> 
> /************************ MENU HELPERS ***************************/
> 
> void flushInput() {
>   // Read all available serial input to flush pending data.
>   uint16_t timeoutloop = 0;
>   while (timeoutloop++ < 40) {
>     while(ss.available()) {
>       ss.read();
>       timeoutloop = 0;  // If char was received reset the timer
>     }
>     delay(1);
>   }
> }
> 
> char readBlocking() {
>   while (!Serial.available());
>   return Serial.read();
> }
> 
> uint16_t readnumber() {
>   uint16_t x = 0;
>   char c;
>   while (! isdigit(c = readBlocking())) {
>     //Serial.print(c);
>   }
>   Serial.print(c);
>   x = c - '0';
>   while (isdigit(c = readBlocking())) {
>     Serial.print(c);
>     x *= 10;
>     x += c - '0';
>   }
>   return x;
> }
> 
> uint8_t readline(char *buff, uint8_t maxbuff) {
>   uint16_t buffidx = 0;
>   
>   while (true) {
>     if (buffidx > maxbuff) {
>       break;
>     }
> 
>     if (Serial.available()) {
>       char c =  Serial.read();
>       //Serial.print(c, HEX); Serial.print("#"); Serial.println(c);
> 
>       if (c == '\r') continue;
>       if (c == 0xA) {
>         if (buffidx == 0) {  // the first 0x0A is ignored
>           continue;
>         }
>         buff[buffidx] = 0;  // null term
>         return buffidx;
>       }
>       buff[buffidx] = c;
>       buffidx++;
>     }
>   }
>   buff[buffidx] = 0;  // null term
>   return buffidx;
> }
> /************************ MENU HELPERS ***************************/

do you have Nano BLE selected in Tools menu?

1 Like

The Adafruit Library example code compiles for me with the Serial1 modifications suggested in the comments. As @ Juraj says, you must select the Nano33 as the board.

/* 
  Menu driven control of a sound board over UART.
  Commands for playing by # or by name (full 11-char name)
  Hard reset and List files (when not playing audio)
  Vol + and - (only when not playing audio)
  Pause, unpause, quit playing (when playing audio)
  Current play time, and bytes remaining & total bytes (when playing audio)

  Connect UG to ground to have the sound board boot into UART mode
*/

//#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"


// Choose any two pins that can be used with SoftwareSerial to RX & TX
//#define SFX_TX 5
//#define SFX_RX 6

// Connect to the RST pin on the Sound Board
#define SFX_RST 4

// You can also monitor the ACT pin for when audio is playing!

// we'll be using software serial
//SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);

// pass the software serial to Adafruit_soundboard, the second
// argument is the debug port (not used really) and the third 
// arg is the reset pin
//Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);
// can also try hardware serial with
 Adafruit_Soundboard sfx = Adafruit_Soundboard(&Serial1, NULL, SFX_RST);

void setup() {
  Serial.begin(115200);
  Serial.println("Adafruit Sound Board!");
  
  //softwareserial at 9600 baud
  //ss.begin(9600);
   //can also do 
   Serial1.begin(9600);

  if (!sfx.reset()) {
    Serial.println("Not found");
    while (1);
  }
  Serial.println("SFX board found");
}


void loop() {
  flushInput();
  
  Serial.println(F("What would you like to do?"));
  Serial.println(F("[r] - reset"));
  Serial.println(F("[+] - Vol +"));
  Serial.println(F("[-] - Vol -"));
  Serial.println(F("[L] - List files"));
  Serial.println(F("[P] - play by file name"));
  Serial.println(F("[#] - play by file number"));
  Serial.println(F("[=] - pause playing"));
  Serial.println(F("[>] - unpause playing"));
  Serial.println(F("[q] - stop playing"));
  Serial.println(F("[t] - playtime status"));
  Serial.println(F("> "));
  
  while (!Serial.available());
  char cmd = Serial.read();
  
  flushInput();
  
  switch (cmd) {
    case 'r': {
      if (!sfx.reset()) {
        Serial.println("Reset failed");
      }
      break; 
    }
    
    case 'L': {
      uint8_t files = sfx.listFiles();
    
      Serial.println("File Listing");
      Serial.println("========================");
      Serial.println();
      Serial.print("Found "); Serial.print(files); Serial.println(" Files");
      for (uint8_t f=0; f<files; f++) {
        Serial.print(f); 
        Serial.print("\tname: "); Serial.print(sfx.fileName(f));
        Serial.print("\tsize: "); Serial.println(sfx.fileSize(f));
      }
      Serial.println("========================");
      break; 
    }
    
    case '#': {
      Serial.print("Enter track #");
      uint8_t n = readnumber();

      Serial.print("\nPlaying track #"); Serial.println(n);
      if (! sfx.playTrack((uint8_t)n) ) {
        Serial.println("Failed to play track?");
      }
      break;
    }
    
    case 'P': {
      Serial.print("Enter track name (full 12 character name!) >");
      char name[20];
      readline(name, 20);

      Serial.print("\nPlaying track \""); Serial.print(name); Serial.print("\"");
      if (! sfx.playTrack(name) ) {
        Serial.println("Failed to play track?");
      }
      break;
   }

   case '+': {
      Serial.println("Vol up...");
      uint16_t v;
      if (! (v = sfx.volUp()) ) {
        Serial.println("Failed to adjust");
      } else {
        Serial.print("Volume: "); Serial.println(v);
      }
      break;
   }

   case '-': {
      Serial.println("Vol down...");
      uint16_t v;
      if (! (v=sfx.volDown()) ) {
        Serial.println("Failed to adjust");
      } else { 
        Serial.print("Volume: "); 
        Serial.println(v);
      }
      break;
   }
   
   case '=': {
      Serial.println("Pausing...");
      if (! sfx.pause() ) Serial.println("Failed to pause");
      break;
   }
   
   case '>': {
      Serial.println("Unpausing...");
      if (! sfx.unpause() ) Serial.println("Failed to unpause");
      break;
   }
   
   case 'q': {
      Serial.println("Stopping...");
      if (! sfx.stop() ) Serial.println("Failed to stop");
      break;
   }  

   case 't': {
      Serial.print("Track time: ");
      uint32_t current, total;
      if (! sfx.trackTime(&current, &total) ) Serial.println("Failed to query");
      Serial.print(current); Serial.println(" seconds");
      break;
   }  

   case 's': {
      Serial.print("Track size (bytes remaining/total): ");
      uint32_t remain, total;
      if (! sfx.trackSize(&remain, &total) ) 
        Serial.println("Failed to query");
      Serial.print(remain); Serial.print("/"); Serial.println(total); 
      break;
   }  

  }
}






/************************ MENU HELPERS ***************************/

void flushInput() {
  // Read all available serial input to flush pending data.
  uint16_t timeoutloop = 0;
  while (timeoutloop++ < 40) {
    while(Serial1.available()) {
      Serial1.read();
      timeoutloop = 0;  // If char was received reset the timer
    }
    delay(1);
  }
}

char readBlocking() {
  while (!Serial.available());
  return Serial.read();
}

uint16_t readnumber() {
  uint16_t x = 0;
  char c;
  while (! isdigit(c = readBlocking())) {
    //Serial.print(c);
  }
  Serial.print(c);
  x = c - '0';
  while (isdigit(c = readBlocking())) {
    Serial.print(c);
    x *= 10;
    x += c - '0';
  }
  return x;
}

uint8_t readline(char *buff, uint8_t maxbuff) {
  uint16_t buffidx = 0;
  
  while (true) {
    if (buffidx > maxbuff) {
      break;
    }

    if (Serial.available()) {
      char c =  Serial.read();
      //Serial.print(c, HEX); Serial.print("#"); Serial.println(c);

      if (c == '\r') continue;
      if (c == 0xA) {
        if (buffidx == 0) {  // the first 0x0A is ignored
          continue;
        }
        buff[buffidx] = 0;  // null term
        return buffidx;
      }
      buff[buffidx] = c;
      buffidx++;
    }
  }
  buff[buffidx] = 0;  // null term
  return buffidx;
}
/************************ MENU HELPERS ***************************/

yes

what URL did you add in Preferences?

Are you saying that the code I posted in reply #7 does not compile without error?

Please copy/paste/post the complete error message you get. It will indicate the core and all the files the compiler is using. It will be everything that shows in the black window below the sketch.

Thanks.

Arduino: 1.8.16 (Windows Store 1.8.51.0) (Windows 10), Board: "Arduino Nano 33 BLE"

Are you using a version of the IDE from the windows store, or one downloaded directly from https://www.arduino.cc/en/software

Did you use the board manager to load the Arduino Mbed OS Nano Boards?

Code compiles Cattledog, thanks. I had put Serial1 in to replace ss myself and I must of missed something, yours works.

Thanks