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