Sub MovingAVG(Magnetic As Int)
Dim x As Int
Dim MagData As List 'store mag raw data to sensor
MagData.Initialize
MagData.Add(Magnetic)
Dim maDuration As Int = 30 'length of average for calculate
Dim MagList As List
MagList.Initialize
Dim MayAvg As Int 'storage for values and average
For x = 0 To MagData.size - 1 'iterate through random data
MagList.Add( MagData.Get(x)) 'insert into list
MayAvg = 0 'reset and calculate the avg
For y = 0 To MagList.size - 1
MayAvg = MayAvg + MagList.Get(y) 'ad values
Next
MayAvg = MayAvg / MagList.size 'divide by the count
Log (MayAvg)
Return MayAvg
If MagList.size = maDuration Then
MagList.RemoveAt(MagList.size - 1) 'remove oldest value on List
End If
Next
End Sub