Comparing analog signals

I can't understand why noone done the obvious thing and eliminated one of the if's - they are the same condition, with opposite sense, so you only need to calculate one of them.

  bool different = potValue <= resistorValue * 0.9  ||  potValue >= resistorValue * 1.1 ;
  digitalWrite (redLed, different) ;
  digitalWrite (greenLed, ! different) ;

Simple, no chance of introducing a bug by getting the two conditions not-quite opposite (for instance cattledog's answer still has a bug since <= is not the opposite of >=)

1 Like