[Swlug] Further interesting calculations
Dave Cridland
dave at cridland.net
Tue May 30 13:27:25 UTC 2023
You're sending output to the serial port inside your loop, and the serial
port is running at a synchronous 9600 baud. Much of your overhead and
variation is coming from this.
In practice, if you need a fixed repetition, you either want to drive it by
a clock-based interrupt, or else by using a much smaller delay/sleep call
and checking often - but the moment you introduce other activity in that
loop you're going to cause yourself pain.
On Mon, 29 May 2023 at 16:50, Rhys Sage via Swlug <swlug at mailman.lug.org.uk>
wrote:
> Those are excellent points Colin. I did modify the C++ code to produce an
> average, high, low and maximum range between the high and low. When I
> stopped the code I had got up to a range of almost 300 microseconds.
>
> As far as I know, nothing else was running on the Arduino. I certainly
> didn't have anything bar that piece of test code. There is no actual clock
> on an Arduino. What it processes is some kind of system ticker. micros()
> seems to be built into micro c++.
>
> The suggestion to use an oscilloscope is a good one. Sadly I have almost
> no contacts here in the USA where I could borrow one and buying one for one
> project seems a bit excessive. I'm also not 100% sure what I'm looking for.
> I know they do have ways of making Arduinos and Picos into oscilloscopes
> but I'm not sure where the accuracy comes in or whether perhaps their
> accuracy is extremely limited. I just looked at this DIY version (
> instructables.com/Raspberry-Pi-Pico-200Khz-Digital-Oscilloscope/) and it
> just does not look like it's really accurate to the claimed 5 microseconds
> when I have already ascertained there seems to be a variation of at least
> 100 microseconds. It could be that the microsecond clock stalls while
> processes go and and is then updated by large chunks.
>
> I tried disabling interrupts and it had little effect.
> //#include <string.h>
> unsigned long time;
> unsigned long time2;
> unsigned long time3;
> unsigned long calcu;
> unsigned long maximum;
> unsigned long minimum;
> unsigned long average;
> unsigned long range;
> unsigned long iterations;
> void setup() {
> Serial.begin(9600);
> maximum = 0;
> minimum = 10000000;
> average = 0;
> iterations = 0;
> }
>
>
> void loop() {
> noInterrupts();
> iterations++;
> Serial.println("Time:");
> time = micros(); //prints time since program started
> Serial.println(time); // wait a second so as not to send massive
> amounts of data
> calcu = (time * 2);
> time2 = micros();
> time3 = (time2 - time);
> if (time3 > maximum) maximum = time3;
> if (time3 < minimum) minimum = time3;
> average = ((minimum + maximum)/2);
> range = maximum - minimum;
> Serial.print("Minimum ");
> Serial.println(minimum);
> Serial.print("Maximum ");
> Serial.println(maximum);
> Serial.print("Average ");
> Serial.println(average);
> Serial.print("Range ");
> Serial.println(range);
> Serial.print("Iterations ");
> Serial.println(iterations);
> interrupts();
> delay(1000);
> }
>
> Actual output.
>
> 178186272
> Minimum 164
> Maximum 436
> Average 300
> Range 272
> Iterations 179
>
> It doesn't seem that disabling interrupts has had any noticeable effect.
>
> --
> Swlug mailing list
> Swlug at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/swlug
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/swlug/attachments/20230530/4d1d2e83/attachment.htm>
More information about the Swlug
mailing list