Need advice on measuring Velocity

Paul- thank you.

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.

All the best,
Brock

On some Arduinos, yes. The USB port has to connect. In those cases, you can use the built in serial monitor connection flag as follows:

while (!Serial); //wait for connection to be established

Is there a reason for using such a slow Baud rate as 9600?

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.

Timing depends on what "scantime" is doing, and how it is done.

Reading a port pin using digitalRead() takes around 4 microseconds.

byte x = digitalRead(pin);

Reading a port pin using direct port access as follows takes one machine instruction or 62.5 nanoseconds on an Arduino Uno.

byte x = PIND&1; //bit 0, Port D

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.