Most Bizarre Thing I Have Seen Coding

I am currently working on a robot for a large science centre as a co-op student. The programming has been taking quite a bit f time, however I have not yet reached out to the forums for aid. However, this is terribly strange.

The basic idea here is to receive 3 ints (more in future) from the serial connection. The first one is for a switchcase, the second and third are then used in the switches for whatever else, like using the next two for speeds, or going to another switchcase, then using the third value, etc...

So my little code reads in 3 ints. Essentially it will read the first thing in the serial buffer, check if it is a negative symbol, then read the next three things in the buffer and turn them into ints, and if it is not at the last value to read in, it will read one space, then proceed to the next, and this repeats for the amount of value I'm expecting.

I am using a header template to easily print to the monitor, however my issue remains even without it, using Serial.print() instead.
What is the problem you may ask? Well essentially you will notice as a way of debugging I had it print out every step of the way what it was doing because it was not working. The values of the array were completely off target. However upon printing those values, my code magically worked.

Commenting out the lines that print out one by one, I found four print statements that when removed, the values in the array would not longer match what I wrote in the monitor. I commented //NECESSARY! beside then to mark them.

For example, if I remove the top most print that says NECESSARY! commented beside it , and then type in "0001 0255 -255" my results instead of "input[0]: 1 input[1]: 255 input[2]: -255" which return with any of those statements there, will return ""input[0]: -4900 input[1]: 25 input[2]: -275.

Almost as if it is reading one memory address the left too much.

This is extremely bizarre considering those print statements do not actually effect any of the other code. More curiously it does not work without them!
You would think adding them would mess up the code, but no. This is baffling.
So any help would be greatly appreciated. My robot has been overdue for quite some time.

Thank you.

template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }

boolean printProccess = true;

void setup() 
{

  Serial.begin(9600);

}

const int inputLength = 3;
int input[inputLength];


void loop() 
{
  if (Serial.available() > 0)
  {
    if(printProccess) Serial << "Serial Available: " << Serial.available() << '\n'; //NECESSARY!
    //int num = Serial.available();
    //Serial.println(Serial.available());

    for (int i = 0; i < inputLength; i++) //Reset all input vars
    {
      input[i] = 0;
    }

    for (int i = 0; i < inputLength; i++) //For the amount of input vars we have
    {
      boolean negativeFlag = false;
      
      for (int e = 0; e < 4; e++)   //Read every one (which is 4 pieces of info, ex. -127, 0063, 0255, -255)
      {
        if (e == 0) //Check for a negative symbol here
        {
          if (Serial.read() == '-') 
          {
            //if(printProccess) Serial << "Detected negative, flagging" << '\n';
            negativeFlag = true;
          }
          else
          {
            negativeFlag = false;
            if(printProccess) Serial << "Detected no negative flag" << '\n'; //NECESSARY!
          }
        }
        else
        {
          if(printProccess) Serial << "Setting input[" << i << "] part" << e << " of 3: " << input[i] << " -> "; //NECESSARY!
          input[i] = input[i] * 10 + Serial.read() - '0'; //Turning the reads into ints
          if(printProccess) Serial << input[i] << '\n'; //NECESSARY!
        }
      }

      if (negativeFlag) 
      {
        input[i] *= -1;
        //if(printProccess) Serial << "Set input[" << i << "] to negative" << '\n';
      }

      if (Serial.available() > 0) 
      {
        //if(printProccess) Serial << "Still things ahead, reading the delimeter" << '\n';
        Serial.read(); //there will not be a delimeter on the last one so we have to make sure if we are there dont read
      }
    }
    
    //if(printProccess) Serial << "Flushing Serial" << '\n';
    Serial.flush();
    
    //Serial.print("input[0]: ");
    //Serial.print(input[0]);
    //Serial.print("  input[1]: ");
    //Serial.print(input[1]);
    //Serial.print("  input[2]: ");
    //Serial.println(input[2]);
    //Serial.println();

    Serial << "input[0]: " << input[0] << "  input[1]: " << input[1] << "  input[2]: " << input[2] << '\n';
    Serial << "" << '\n';
  }
}

You check Serial.available(), which tells you at least one character is available. You then proceed to do a whole bunch of Serial.reads, without ever checking to see if there are actually any more characters waiting. So, you are almost certainly trying to read characters which have not yet arrived, for which, IIRC, Serial.read() will return -1. You need to check Serial.read() for EVERY character you read, OR wait until Serial.available() returns a count at least as large as the number of characters you will be reading.

Regards,
Ray L.

Thank you very much. I also noticed Serial.flush was not working when I inputed values out of range, and it seems it was changed to do that function of waiting. I will change Serial.flush() to
while(Serial.Available()) Serial.read();

Thanks again.

Have a look at the examples in serial input basics. They read in all the data and then you can parse it at your leisure.

...R

This is extremely bizarre considering those print statements do not actually effect any of the other code.

Actually they do. They consume time. Your code is highly timing dependent. Consuming time by doing debug prints makes it "work" better.

What the others said, plus: Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking