Is there any relation between what you output to the Serial port on the Arduino and the time you "spend" in each runloop?
I mean, if you had this:
void loop ()
{
// a whole bunch of Serial.print() and Serial.println()
}
Would your loop slow down depending on the amount of data outputted to the serial port?
Can you measure what speed you are running at?
These questions arise from a project idea that I had where I would blink a laser at some very high frequency but the photoresistor would not always get all the "blinks" and would often miss a few. This was based on the Serial.print() output so I don't know all the actual run loops where printed out or if some were skipped. Or maybe it was some other problem entirely but the questions still remain in my head.
Would your loop slow down depending on the amount of data outputted to the serial port?
Of course.
Can you measure what speed you are running at?
Yes...
void loop( void )
{
static unsigned long Previous;
unsigned long Now;
unsigned long Delta;
Now = micros();
Delta = Now - Previous;
Previous = Now;
// Delta = time spent in the last call to loop
// Your stuff goes here
}
Or...
void loop( void )
{
static unsigned long Previous;
unsigned long Now;
unsigned long Delta;
// Your stuff goes here
Now = micros();
Delta = Now - Previous;
Previous = Now;
// Delta = time spent in this call to loop
}
When you write to the serial port, the characters you write are stored in an internal buffer. This takes a small but non-zero amount of time. Your sketch can continue on to do other things while the hardware sends the content of that buffer out in the background.
If you are writing to the serial port faster than the serial port can send the data out then eventually the internal buffer will become full. At that point any further writes to the serial port will cause your sketch to stop and wait until enough data has been send to allow your new output to be stored in the buffer. Since the serial port is extremely slow compared to the processor speed, at this point the execution speed of your sketch would slow to a crawl and your Arduino would spend lots of time waiting for data to dribble out via the serial port instead of running your code.