Search code examples
cthreshold

Check if analog value is within threshold


I'm reading an analogue value from a Pin on a µC. I want to check periodically if the value has changed more than x. Here, I am using the abs() function, but I want to keep it simple. Can you help me?

int algVal= 0;
int oldVal = 0

while(1){

  algVal = getAlgVal();

  if(abs(algVal - oldVal) > x)
   {
    doStuff();
   }

  oldVal= algVal:

}

Solution

  • That sounds like a fantastic approach, and would probably be very nice in terms of runtime performance, abs() for integers is cheap. If in doubt, read the generated assembly of course.