[Swlug] Further interesting calculations

Rhys Sage rhys_sage at yahoo.com
Mon May 29 15:50:16 UTC 2023


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.



More information about the Swlug mailing list