Optimizing code for storing values and comparing those with new sensor readings

The first thing you need is a function that determines if a value is in a range. You perform that test 9 times, replicating code, instead of using a function.