Thanks for that Robert.
Well, as I admitted to above, this was going to be a “dirty fix” to a problem. I didn’t think it was my code but a random issue with a connected device (electronic scales).
After a good sleep, a bit of thought and carefully looking at my code, I realised it was a code issue of my own making.
I’m reading data coming in from the serial port connected to electronic scales. The example was placing a 1000gm weight on the scales which I get an average of over 0.5 seconds for calibration and general readability.
The problem was that I erroneously thought the “Trim” function stripped a string left/right to the delimiter, EndOfLine.Windows in my case.
[code] //read all data from serial port buffer
tempBuffer = Serial1.ReadAll(encodings.ASCII)
//split the data
calibrationArray = tempBuffer.Trim.Split(EndOfLine.Windows)
Dim x, total as Integer
For i as integer = 0 to (calibrationArray.Ubound)
x = val(calibrationArray(i))
total = total + x
I knew I should be getting from the device (+/- 3gm) something like this:
But in reality I was getting something like this:
Where ReadAll was reading what was in the serial buffer (as it should) but sometimes not the whole word. I was using the total and dividing by the number of elements in the array to determine the average, but the total was not always correct, hence the occasional glitch.
I changed the code to:
For i as integer = 1 to (calibrationArray.Ubound-1)
And therefore eliminate the first and last readings then divide by 2 less than the total number of elements. No more glitches.
It still does beg the question which method is best to get the average. Total / Number of Samples OR the Most Common Number in the set.