Hello guys,
I am currently working on my bachelor thesis and as a preperation for that I followed a simple guide on YT on how to meassure the max frequency the controller can process. That includes a for-loop that counts to 1000000 (1 million) and meassures the time it took.
The weird behavior is: If I set the loop-limit to round about 30000 everything works like intended, but going higher for like 40000 and more, the loop is not exiting anymore. Does anyone know why this is happening?
The calculations after the for-loop can be ignored so far. Those are not part of the problem. It is just the for-loop. I don't know why at some certain point, it doesn't stop looping.
The solution is to use a variable type that can hold the value required. A further suggestion would be to use an unsigned variable so that the max value could be twice that of a signed variable
const int audioInPin = 0;
int analogValue;
unsigned long newTime;
void setup() {
Serial.begin(115200);
}
void loop() {
//Serial.println("Starting...");
newTime = micros();
//Do 1 million reads and record time taken
for (float i = 0; i < 1000000.0; i++) {
analogValue = analogRead(audioInPin);
}
float conversionTime = (micros() - newTime) / 1000000.0;
Serial.print("Conversion time: ");
Serial.print(conversionTime);
Serial.println(" uS");
Serial.print("Max sampling frequency: ");
Serial.print((1.0 / conversionTime) * 1000000.0);
Serial.println(" Hz");
}
now it works. No unsigned needed in that case, maybe not even the float, guess the long would do the trick as well. Thanks so much for the quick response.