Carefully considered and thought out question.

D n T

Senior Member
I am currently calculating the average of a series of sensor readings.
Using word variables for accuracy.
Reading the sensor every 3 seconds then adding each reading together to get the sum, then working the average.
The word variable used to store the sum value is filled ( more than 65535) quickly.

I would like to know if there is a way of combining 2 word variable in to allow a 32 bit number to be used .

the maximum value of a byte is 255
the maximum value of a word is 65535
Can I combine two word variables ( 4 byte variables) to give me a maximum number of 4294967295, which I will never exceed,
( It would take 145 days with readings of 1023, every 3 seconds)

I hope I have been more clear this time.
 

BrendanP

Senior Member
Btw DNT have you had a look at STAMP DAQ to log your readings? You probabaly have but I thought it was worthwhile mentioning.

STAMP DAQ runs in Excel, you could draw up a spread sheet to do the calcs a sthey come in from the picaxe/sensors.
 

hippy

Ex-Staff (retired)
You can concatenate two or more variables to get larger than 16-bit numbers but you'll have to handle all the maths yourself. Addition is relatively simple ...

Code:
' LET w6:w5 = w6:w5 + w0

tempWord = w5 + w0
If tempWord < w5 Then ' True when addition will cause an overflow
  w5 = w5 + w0
  w6 = w6 + 1
Else
  w5 = w5 + w0
End If
That can be simplified. Other maths gets harder but is possible.

The alternative is to do rolling averages where the previous average of numbers is held and the current average is the previous average plus the current. It isn't ideal for all cases.
 

BeanieBots

Moderator
I don't know how big your raw data readings are, but if they are only 10 or 12 bit, you could sum into one word for 64 or 16 readings and then sum into another word for another 64 or 16 readings. Then divide each word by however many readings you took, sum them together and divide by 2.
I used this method (with a little help from SFR RAM) to produce a running average of 50 samples of a 12 bit input value.
 

womai

Senior Member
What hippy mentions at the end of the post is an implementation of an IIR (infinite impulse response) filter. Basically all previous values influence the newly calculated one, albeit with decreasing weighting the further ago they are. Sounds complicated, but is actually the digital equivalent of a simple R-C lowpass filter. Here is some code for that in VB6 (but may be a bit difficult to meaningfully translate the floating point math into Picaxe integer math):

Code:
AcqCount = AcqCount + 1
If AcqCount > averages Then AcqCount = averages
WeightOld = (AcqCount - 1) / AcqCount
WeightNew = 1 / AcqCount
DataBuffer = WeightOld * DataBuffer + WeightNew * DataBuffer_raw
The larger you set "averages" the more low-pass filtering. The way the code is written it will take the first value as it is, and then continuously average the previous average with the new value. The higher "averages" is the less weight the new value has, and the more weight the past average has - making for slower changes, i.e. more averaging/filtering. Very similar to how averaging works on many oscilloscopes (hint - guess where I am using this code? :) since it is very simple to implement and does not need much memory (most important, memory requirement does not increase with larger values for "average").
 
Last edited:

D n T

Senior Member
Stamp Daq

Brendan ,do you have an example of the type of programming you used to get the info into the DAQ? please

I downloaded the PLX DAQ file and am wading through it now
 
Last edited:
Top