Serial Question

Hey folks. First time posting in a good while. I've actually forgotten most of my arduino programming and now find myself stumbling on simple problems. I was hoping to see if someone could explain to me why the following is happening on the below code:

I have a DFPlayer MP3 module that I am communicating with through softwareSerial. Throughout my code I call a function that prints out to serial the status of variables and returns from functions that check on the MP3 player.

I have it print read(), but only if and when available() returns back something that is not zero (which if I remember correctly, should automatically be determined as true). However, in the serial monitor, it shows available() returning "0", but then does print out the read() at "2".

On the next go around (this info function is being called inside a while loop), it shows that available() does have a byte in it, but then doesnt print it out.

What am I missing here?
Many thanks in advance.

edit:

Code and Serial Monitor output below.

void info(){
Serial.print("ReadType is: ");Serial.println(theTurret.readType());
Serial.print("available # of bytes is:");Serial.println(theTurret.available());
if(theTurret.available()){Serial.print("Read is:");Serial.println(theTurret.read());}
Serial.print("ReadState is:");Serial.println(theTurret.readState());
Serial.print("Time is:");Serial.println(millis());Serial.println();
}

Turret opening...
ReadType is: 4
available # of bytes is:0
Read is:2
ReadState is:2
Time is:6392

Turret opening...
ReadType is: 5
available # of bytes is:1
ReadState is:512
Time is:7445

Don't post pictures of code. Copy and paste the code where we can see it and even copy and paste and test if we want to. Seriously...

You say info() is called in a while loop. Do you mean you have an infinite while() loop in the function loop()? I think that loop() has to return regularly for Serial to work.

The while loop in question is not infinite.

Although, at the end of the loop(), there is a while loop to stop the program from looping.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool. I recommend you to use the standard IDE instead.

Put each statement on a single line. There is no benefit in mashing a bunch of stuff onto one line. It just makes it harder to read.

You have two calls to theTurret.available(). There's no guarantee that available() will return the same value on the second call. Try this instead:

void info() {
  Serial.print("ReadType is: ");
  Serial.println(theTurret.readType());

  if (const byte availableBytes = theTurret.available()) {
    Serial.print("available # of bytes is:");
    Serial.println(availableBytes);

    Serial.print("Read is:");
    Serial.println(theTurret.read());
  }

  Serial.print("ReadState is:");
  Serial.println(theTurret.readState());

  Serial.print("Time is:");
  Serial.println(millis());
  Serial.println();
}

Do you see how much easier to read that is?

papaFred:
I think that loop() has to return regularly for Serial to work.

No, that's not true. You only need to return from loop() if you want serialEvent() to work and that stupid feature is almost never used.

Post complete code. The problem might not be in that one function you posted.