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