Hello,
gbulmer, thanks a ton for the modified code. I like the addition of blinking at the end of the quit() function. A visible indicator that things are done.
Your bubblesort function will be useful to me in the future. I will keep it in a safe place. I have not tried any recursive coding with the Maple/Arduino. Does quicksort work well on the Maple?
I was surprised you set MAX_LOOPS to only 20, because in my code (see my second "quantitative" post) I saw long "blips" at rates of 0.7%-3.4%. If we expect to see long blips produced within your code at the same frequency range then between 30-145 loops need to be tested.
However, when I modified your code to do 500 loops everything checked out. I was expecting to see between 3 and 15 unusual blips, but I saw none.
Then, I decided to comment out your call to CountLoops(). I replaced it with a simple SerialUSB.println("X") which is similar to what I had in my original code.
In my LabVIEW/Maple code where I see the 21n bug I use SerialUSB.print(). When a match is found (using strcmp) the Maple performs an analog read and returns the value to LabVIEW (the system) using SerialUSB.print() and SerialUSB.println()). When I use an Arduino a three char string in the strcmp comparison works. However, when the same three character string comparison is used with a Maple I see the unpredictable "21n" pausing.
When I ran the sketch the most common time was 213 msec. However, I also saw ten (10) blips equal to 363 msec and blips at 327 msec and 232 msec. A total of twelve blips in 500 loops (12/500 = 2.4%; roughly one in forty)
When I ran the code a second time I saw a very different pattern.
Same most common time (213 msec). However, very few unusual blips. Only three were 216 msec or greater (3/500 = 0.6%)
225 msec (1 time)
217 msec (1 time)
216 msec (1 time)
215 msec (1 time)
214 msec (2 times)
The third time I ran the code the fastest time was the same (213 msec). However, there were 345 blips of 363 msec (69%).
My conclusion is that something is funky with SerialUSB.println() which I guess we already know. I am hoping this will be fixed in the release of 0.0.7.
Sorry for the "false warning" about strcmp() problems. Thanks for all the trouble and work.
Stephen from NYC
Here is the modified code I used:
#include <string.h>
#define COMM SerialUSB // for Maple
// #define COMM Serial // for Arduino#define MAXIMUM 6 // for Maple
// #define MAXIMUM 21 // for Arduino#define LIMIT 100000
// #define MAX_LOOPS (20) // original code from gbulmer
#define MAX_LOOPS (500) // if occurrence of error is 0.7% need > 140 loos to seelong currentLoop = 0;
int intervals[MAX_LOOPS]; // unusable on an Arduino - this is likely too big
int dumbMath = 0;int ledPin = 13; // LED connected to digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT);// this is not necessary, but I include it to make it easy to port my code between the Maple/Arduino.
COMM.begin();
// COMM.begin(9600);delay(5000);
}void loop()
{
char stringArray[]="0123456789";
int ledState = HIGH;for (int currentLoop=0; currentLoop < MAX_LOOPS; ++currentLoop) {
digitalWrite(ledPin, ledState); // set the LED on/off
ledState = !ledState;long start=millis();
for (int count=0; count < LIMIT; count++) {
if ((strcmp(stringArray, "0123456789") == 0) && (count == (LIMIT-1))) {
// CountLoops(); // function should force compiler to leave stuff in
SerialUSB.println("X");
}
}long stop=millis();
intervals[currentLoop] = (int)(stop-start);
}quit();
}void CountLoops() {
dumbMath++;
}void quit() {
bubblesort(intervals, MAX_LOOPS);COMM.print("Final number of loops: ");
COMM.println(MAX_LOOPS);COMM.println("Elapsed time sorted from largest to smallest");
for (int i=0; i<MAX_LOOPS /*totalBlips*/; ++i) {
COMM.print("blip ");
COMM.print(i+1);
COMM.print(" = ");
COMM.print(intervals[i]);
COMM.println(" msec");
}COMM.print("Garbage to stop compiler optimising away stuff ");
COMM.println(dumbMath);while (1) {
digitalWrite(ledPin, HIGH); // set the LED on
delay(3000);
digitalWrite(ledPin, LOW); // set the LED off
delay(1000);
}
}void bubblesort(int list[], int n)
{
int i,j;
for(i=0;i<(n-1);i++) {
for(j=0;j<(n-(i+1));j++) {
if(list[j] < list[j+1]) { // sort largest to start of array
int temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
}
}