I had a few moments this morning and took the Baud rate up and the scan time came down to about 0.3 ms.
Your comment prompted me to consider another approach to my code.
I have a step-wise program. Step 1, Step 2, Step 3... Step 6, return to Step 1.
In Step 1-5, I am looking at sensor for rising/ falling edges. In Step 6, I write my results.
Since Step 6 is the only state that requires serial communication, could I embed the serial communication in Step 6 (not in the setup). The higher scan speeds are not needed in Step 6 since we are not measuring, we are just reporting.
Step 6 would become something like:
case 6: // do this for when Step=6
Val1= //some calculated value
Val2= //some calculated value
Val3= //some calculated value
Serial.begin(9600);
Serial.println(Val1);
Serial.println(Val2);
Serial.println(Val3);
Serial.end(9600);
Step=1; // leave Step 6, go back to Step 1.
break;
If this approach works, is there any delay required between the .begin and the first print command? This code will only run once since the last argument changes the step. If a delay is needed, that is ok since all we are doing in this step is printing results.
Only because this is my first foray into Arduino and most of the examples use 9600.
The reduction of scan time from 6.24 ms to 0.3 ms was great. Ideally, I would like to go 10x faster when I am looking for my 'broken beam' measurements. While doing other things such as writing the results, I don't care as much.
Another general question for the community- with an Uno R3, what would you expect the fastest scan time possible to be? I have seen things like, "4 million to 16 million (and more) instructions per second" but I don't have sense for how many 'instructions' are in the compiled code.
Did you actually look at the documentation for "Serial"? Right after putting Val2 and Val3 in the serial buffer for transmission, you tell serial to disable before there is a chance to send anything. Strange logic.