I’m attempting to come up with a routine that with each pass of checking sensors it will store an average value of the sensor readings in array element SensorName(10). In this routine, I read the sensors and place the values in a sensor array sensorName(0-9) and use sensorName(10) to hold the average. After the first 10 passes the sensor average stabilizes and the array elements for each sensorName (0-9) continue to be overwritten and sensorName(10) continues to be updated. I almost have this working except the first pass avg is not correct. This also feels complicated. Someone clever might show me an easier way to build an average on the first ten passes and then continue to maintain an average as the program real time continues to read sensors values.
This is the code I’m using:
Count and FirstTenCycles are stored properties in a method
[code]Var x,avg As Integer
If FirstTenCycles < 10 Then //get first 10 readings to build avg
ReadAllTempSensorsSub(FirstTenCycles)
FirstTenCycles = FirstTenCycles + 1
Elseif count < 10 Then //Continue after first 10 reads
ReadAllTempSensorsSub(count)
count = count + 1
End
If count > 9 Then // start over filling array
count = 0
End[/code]
ReadAllTempSensorsSub:
[code]Public Sub ReadAllTempSensorsSub(count as integer)
Var BK7TR As New SensorRead
StorageAreaTemp(count) = BK7TR.ReadSensor(“BK7”,2)
CaluculateAvg(StorageAreaTemp)
FloorIntakeTemp(count) = BK7TR.ReadSensor(“BK7”,3)
CaluculateAvg(FloorIntakeTemp)
CeilingTemp(count) = BK7TR.ReadSensor(“BK7”,4)
CaluculateAvg(CeilingTemp)
KitchenTemp(count) = BK7TR.ReadSensor(“BK7”,5)
CaluculateAvg(KitchenTemp)
UpstairsTemp(count) = BK7TR.ReadSensor(“BK7”,6)
CaluculateAvg(UpstairsTemp)
BedroomTemp(count) = BK7TR.ReadSensor(“BK7”,7)
CaluculateAvg(BedroomTemp)
End Sub[/code]
Caluculate Avg:
[code]Public Sub CaluculateAvg(byref TempZone() as integer)
Var x,avg As Integer
For x = 0 To FirstTenCycles-1 // will stay 10 after first 10 reads
Avg = Avg + TempZone(x)
Next
TempZone(10) = avg/FirstTenCycles-1
End Sub
[/code]