Arduino Locks Up When Serial.Available() Has Data

I am trying to diagnose an issue where inside loop() my Arduino Mega locks up on the following code when Serial1.available() has data:

  Serial.println("Before");
  if(Serial1.available() > 0)
  {         
    Serial.println("After");
  }

It's important that I note that this only locks up where there IS data and otherwise its fine. When there is data to read I see the "Before" message but never see "After" and loop() never iterates again. Does anyone have suggestions on what would do this or what I should check? I wouldn't think its a memory issue since I believe this call is just returning a count of the buffer bytes unless it's doing something additional?

Please post all your code.

Hi Mark, I attached my code (it was too large to include in the post). You will see in the code that I am using the Adafruit Music Maker shield. This issue only happens while it’s playing music so it is related. I am diagnosing this from the “Shield” side with them but I am posting here to diagnose it from the Arduino side since it’s happening on the call to Serial1.available() in hopes that someone may know if this call does something that could cause a lock/crash.

Arduino_Code.txt (15.3 KB)

  Serial2.flush();     // Flush the receive buffer

Nonsense. That is NOT what that does.

  SetVolume("18");

It is stupid for a function like this to take a string to set the volume, unless it accepts "Quiet", "Moderate", "Loud", and "RockTheHouse".

As for the actual problem, you have a boat-load of stuff going on when there is data in the Serial1 incoming buffer with NO debug stuff to show where the code dies. Add some, and show us the modified code AND Serial output, if the actual problem doesn't become obvious.

Arduino Locks Up When Serial.Available() Has Data

Your code in loop basically does nothing until you have serial data available. So what you are basically saying is you have some sort of bug, triggered by receiving data.

The Arduino doesn't "lock up", per se, when it receives data. One possible problem is your WaitForReady function.

You printDirectory function is recursing on the directory structure holding a File object open at each
level. This might be using up all your RAM - File objects take a lot of space and its best to have just
one in use at a time.

void WaitForReady()
{
  byte data;
  do
  { 
     data = Serial2.read();

    if(data != 255)
    {
        Serial.println(data);
    }
  }
  while (data != ':');   // Wait here until the Emic 2 responds with a ":" indicating it's ready to accept the next command
  delay(10);
}

You don't call Serial2.available() before calling Serial2.read(). You have a while loop that is not
guaranteed to terminate.

DeviantSpark:
I am trying to diagnose an issue where inside loop() my Arduino Mega locks up on the following code when Serial1.available() has data:

  Serial.println("Before");

if(Serial1.available() > 0)
  {       
    Serial.println("After");
  }




It's important that I note that this only locks up where there IS data and otherwise its fine. When there is data to read I see the "Before" message but never see "After" and loop() never iterates again. Does anyone have suggestions on what would do this or what I should check? I wouldn't think its a memory issue since I believe this call is just returning a count of the buffer bytes unless it's doing something additional?

When your code DETECTS presence of data , it prints "After" and does nothing else.
The data remains in "serial" buffer, hence the process gets "stuck" next time around ( assuming in some kind of loop or loop() ) until the buffer is cleared.
Check up on (Serial) read().

After more research I realized I goofed and was using digital pin 7 which is also used by my shield so there was a conflict. I think reason I wasn't seeing the Serial.print() messages was because it was breaking before it had a chance to write the message to the console.