Having issues with the Serial Monitor needing to be opened for my entire code to work

So, I've taken the wav playback example code and implemented it into my own project. what I'm using it for is a .wav sound is played when a push button is pushed (I've also got some neopixel lights that shine along with this). This all works perfectly, however my issue is, it only works when the serial monitor is open (lights don't turn on either until the monitor is open), I know why its opening and I think I know why the code is dependent on it. I just don't know how to fix it. And honestly I don't know any other way to play the sound files without the current code I have. I need this to work without being connected to my computer. This is also the first code I've ever written and forum post as well, so I'm very sorry if something is just ludicrously wrong.

I'm also using a MKRZERO

#include <Adafruit_NeoPixel.h>


#define LED_PIN2    A2
#define LED_PIN     A3
#define LED_COUNT   8
#define LED_COUNT2  8

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2(LED_COUNT2, LED_PIN2, NEO_GRB + NEO_KHZ800);

#include <SD.h>
#include <ArduinoSound.h>

const int sPin = A6;
const int bPin = 5;
int v =0;

const char filename[] = "fire.WAV";

SDWaveFile waveFile;


void setup() {
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // setup the SD card, depending on your shield of breakout board
  // you may need to pass a pin number in begin for SS
  Serial.print("Initializing SD card...");
  if (!SD.begin()) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // create a SDWaveFile
  waveFile = SDWaveFile(filename);

  // check if the WaveFile is valid
  if (!waveFile) {
    Serial.println("wave file is invalid!");
    while (1); // do nothing
  }

}
  strip.begin();
  strip2.begin();
  strip.show();
  strip2.show();

  pinMode(LED_PIN, OUTPUT);
  pinMode(LED_PIN2, OUTPUT);
  pinMode(bPin, INPUT);
  pinMode(sPin, OUTPUT);
}

void loop() {
v = digitalRead(bPin);
if (v == HIGH)
{
       AudioOutI2S.play(waveFile);
       AudioOutI2S.volume(1);
}else {
         AudioOutI2S.volume(0);

}
uint32_t green = strip.Color(0, 200, 50);
uint32_t greener = strip.Color(0, 255, 0);
{
strip2.fill(blue, 0, 8);
strip2.setBrightness(100);
strip2.show();
delay(100);
strip2.setBrightness(0);
strip2.show();
delay(100);
strip2.setBrightness(100);
strip2.show();

}

{
strip.fill(blue, 0, 5);
strip.setBrightness(100);
strip.show();
delay(50);
strip.setBrightness(0);
strip.show();
delay(50);
strip.setBrightness(100);
strip.show();

strip.fill(white, 5, 8);
strip.setBrightness(100);
strip.show();
delayMicroseconds(30000);
strip.setBrightness(0);
strip.show();
delayMicroseconds(40000);
strip.setBrightness(100);
strip.show();
}
}```

Perhaps the problem is here.
Paul

1 Like

I've not tried it but if there is no serial monitor the code will wait forever.

1 Like

if i get rid of that it still requires the serial monitor to open :confused:

i deleted the

while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

but it gives the error message;

new_simple:49:3: error: 'strip' does not name a type
strip.begin();
^~~~~
new_simple:50:3: error: 'strip2' does not name a type
strip2.begin();
^~~~~~
new_simple:51:3: error: 'strip' does not name a type
strip.show();
^~~~~
new_simple:52:3: error: 'strip2' does not name a type
strip2.show();
^~~~~~
new_simple:54:10: error: expected constructor, destructor, or type conversion before '(' token
pinMode(LED_PIN, OUTPUT);
^
new_simple:55:10: error: expected constructor, destructor, or type conversion before '(' token
pinMode(LED_PIN2, OUTPUT);
^
new_simple:56:10: error: expected constructor, destructor, or type conversion before '(' token
pinMode(bPin, INPUT);
^
new_simple:57:10: error: expected constructor, destructor, or type conversion before '(' token
pinMode(sPin, OUTPUT);
^
new_simple:58:1: error: expected declaration before '}' token
}
^
exit status 1
'strip' does not name a type

Check the syntax of the line(s) you removed. It looks like you have an extra { somewhere.

I think that the } was not removed :wink:

    while (!Serial) {
      ; // wait for serial port to connect. Needed for native USB port only
    } <<==== this one

@GrubbyDinner, if you modify code and have problems, please post the updated code.

like as a new post? or a reply to this one?

I found the solution!
take:

{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

and change it to:

{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  (!Serial); {

  }

I just got rid of the while

That's not a "proper" solution :wink:
Change

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

to

  // Open serial communications and wait for port to open:
  Serial.begin(9600);

or perhaps

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
delay(500);

Why a delay? Just curious.

To give the time for any virtual serial port to initialise. It might not be long enough but it will stop the code from freezing when not connected to a USB port.

1 Like