Serial Print causes mulfuctions with millis()

Hi everyone, I wrote a sketch and I am using some serial prints for debugging. Each time I insert a serial print after the for loop the sketch crashes.. actually it goes to an infinite loop.

This is the whole sketch

#define pinOut 4 // Digital output for testing purposes

byte digitalSensor[] = {0, 1, 2, 3, 6, 7, 13, 14, A0, A1, A2, A5}; // define the digital sensors' pins
byte analogSensors[] = {A3, A4};     // define the analog sensors' pins

byte digitalSensorSize = sizeof(digitalSensor) / sizeof(digitalSensor[0]); // Calculate the igitalSensor size based on the digital sensor
const byte CounterSize = sizeof(digitalSensor) / sizeof(digitalSensor[0]); // Calculate the counter size based on the digital sensors
byte counter[CounterSize];                                                 // define an array of counters with the same size as the digitalSensor array

unsigned long loopMicrosStart; // Start loop timer
unsigned long loopMicrosStop;  // End loop timer
unsigned long currentMillis;
unsigned long startMillis;
unsigned long period = 1000; // 1000ms

void setup()
{

  Serial.begin(9600); // Begin communication with serial monitor

  startMillis = millis(); //Initialize startMillis

  // Set each pin attached to a digital sensor as INPUT
  for (byte i = 0; i < digitalSensorSize; i++)
  {
    pinMode(digitalSensor[i], INPUT);
  }

  pinMode(pinOut, OUTPUT);
  digitalWrite(pinOut, HIGH);
}

void loop()
{
  currentMillis = millis();

  // Loop throught the digital pins and read the output of the sensors
  // For its pulse increase the counter
  loopMicrosStart = micros(); // Initialize the timer before loop
  for (byte i = 0; i < CounterSize; i++)
  {
    bool readSensorStatus = digitalRead(digitalSensor[i]);
    if (readSensorStatus == HIGH)
    {
      counter[i]++;
    }
  }
  unsigned long loopPeriod = micros() - loopMicrosStart; // calculate the loop time in micro seconds
 
  Serial.println("TEST");
  
  //Print the counters each sec

  if (currentMillis - startMillis >= period)
  {
    Serial.println("You insert the if statement");
    for (byte i = 0; i < CounterSize; i++)
    {
      Serial.print("The loop time was: ");
      Serial.print(loopPeriod);
      Serial.println(" us");

      Serial.print("The size of counter is: ");
      Serial.println(CounterSize);
      Serial.print("Counter ");
      Serial.print(i);
      Serial.print(" = ");
      Serial.println(counter[i]);

      startMillis = currentMillis;
    }
  }
}

This is the for loop segment I mentioned before

for (byte i = 0; i < CounterSize; i++)
  {
    bool readSensorStatus = digitalRead(digitalSensor[i]);
    if (readSensorStatus == HIGH)
    {
      counter[i]++;
    }
  }
  unsigned long loopPeriod = micros() - loopMicrosStart; // calculate the loop time in micro seconds
 
  Serial.println("TEST");

If I comment on Serial.println("TEST"); the sketch is running normally,

However, when I leave it uncommented the program crashes, printing unstopped "TEST" as shown below

Basically I realized that if I insert the Serial.print anywhere inside the void loop() then the millis timer doesn't work... It works only if Serial.print is inside this segment :o

 if (currentMillis - startMillis >= period)
  {

    for (byte i = 0; i < CounterSize; i++)
    {
      Serial.print("The loop time was: ");
      Serial.print(loopPeriod);
      Serial.println(" us");

      Serial.print("The size of counter is: ");
      Serial.println(CounterSize);
      Serial.print("Counter ");
      Serial.print(i);
      Serial.print(" = ");
      Serial.println(counter[i]);

      startMillis = currentMillis;
    }
  }

If the program continues to print "test" then it is working correctly and has not crashed.

The problem is that you are trying to print that message at every iteration of loop() which is a very short interval. It is quite likely that the Serial Output Buffer gets full and then the Arduino will wait for it to empty - which it never can, because there is always another "test"

...R

Hi Robin2 and thanks for your reply.

You are right :slight_smile: I modified the sketch to serial.print everyone second and now it works fine!!