Dim ThermostatDegrees As Float = 1.5
Dim MaxErrorPercent As Float = 1.33
Dim MaxErrorDegrees As Float = Abs(ThermostatDegrees * MaxErrorPercent / 100)
Dim CurrentTemperatureDegrees As Float = 2.24568
If Abs(CurrentTemperatureDegrees - ThermostatDegrees) < MaxErrorDegrees Then
Log("within bounds") 'temperature is within 1.33% of thermostat setting
Log("(ie between " & (ThermostatDegrees - MaxErrorDegrees) & " and " & (ThermostatDegrees + MaxErrorDegrees) & " degrees)")
Else
Log("sound the klaxon")
End If
although in many situations you'd be better testing separately for too-low and too-high because usually the responses are different, eg if monitoring refrigerator temperature then:
- if it goes too high, you'd prompt the operator to check that the door is closed and power is available and connected and switched on
- if it goes too low, you'd prompt the operator to check if the compressor motor is running continuously