On an Arduino 101, I have this code running:
void setup()
{
pinMode(2, INPUT);
Serial.begin(9600);
}
int counter = 0;
void loop()
{
Serial.print(millis()/1000);
Serial.print("\t");
Serial.print(counter);
Serial.print("\t");
Serial.println(digitalRead(2));
counter++;
}
A wire can be used to jump Pin2 between 3.3V and GND.
When running with the Serial Monitor on, I see the values increment at a rate where counter increases by the hundreds very quickly and steadily ticks through the thousands over time.
If I close Serial Monitor momentarily and re-open it, I notice that this counter value has increased dramatically (by the tens-of-thousands). Another sign of this is when starting the program and waiting a moment to start the Serial Monitor. As the Arduino 101 does not restart when the Serial Monitor is opened, I see a large initial value in the tens of thousands despite having just started the program.
My question is this: is the Serial library operating synchronously or asynchronously? I was led to believe that it was asynchronous; however, if that was the case, why should the Arduino execute any slower when the serial monitor is on?
While I understand that increasing baud rate will increase the speed at which counter increases, I still see the same dramatic increase in this code when the monitor is closed and then re-opened.
Another possibility I thought of was that maybe counter was being stored in a buffer and what I was seeing was not an up-to-date time or count. To check this, the program prints the digitalRead value of pin2 as I swap the jumper between 3.3v and GND. This change in value was reflected instantaneously on the Serial Monitor, and I believe this means I am not seeing past data slowly being printed out to the monitor.
One last attempt at understanding what was going on was executing this code:
void setup()
{
pinMode(2, INPUT);
Serial.begin(9600);
}
int counter = 0;
void loop()
{
counter++;
if (counter%1000==0)
{
Serial.println(counter);
}
In this example, counter can be seen ticking through quite fast, by the hundreds of thousands, which indicates that the program is capable of running very quickly until a print statement is made. The effect is notably less when baud rate is increased to something like 115200 but is that saying that baud rate is literally slowing the processing speed of the controller?
Any kind of explanation as to what exactly is going on would be very appreciated.
Thank You for your Time.
Source of Arduino being supposedly Asynchronous: https://itp.nyu.edu/physcomp/lessons/serial-communication/serial-communication-the-basics/