Having an analog read only print a value if value changes enough

I am trying to take a linear damper potentiometer, lets say the value reads 200, i want it to serial print that value once until the change is large enough. Lets say it wont do anything until it sees a change of 50 bits.

The end plan is to use a 12 position rotary switch but there is a growing resistance. each position is a 1k increase in resistance. What I want is for it to only print if there is a change in the switch.

I have tried counters and a status Boolean but I cant get it to work. The base code is below, is anyone able to help?

  Rotary_1 = analogRead(Rotary_1_pin);
  //Serial.println(Rotary_1);
  // save the current button state for comparison next time:

{
  if (Rotary_1 < 100)
    {
    Serial.println(1);
   }
  else if (Rotary_1 >= 100 && Rotary_1 < 500)
    {
    Serial.println(2);
    }
  else if (Rotary_1 >= 500 && Rotary_1 < 1300)
    {
    Serial.println(3);
    }
}

It looks like you are making things more complicated than they need to be

Before taking a new reading save the previous one to a different variable. After taking the new reading, subtract old from new and if the absolute value of the difference (use the abs() function) is greater than your delta value then do whatever you need

I guess another issue is that its not instantaneous change so it needs to wait until it sees that large of a move.

Note taken on the capital letters, I'm not a well versed coder so thats good to know.

if(abs((storedRotary - Rotary_1)) >= 50){

The new value could be greater.

Rotary_1 = analogRead(Rotary_1_pin);
if (abs(oldRotary - Rotary_1) >= 50){
  oldRotary = Rotary_1;
    //print the new value
}

It does not matter whether or where you use capital (upper case) letters in variable names.

The choice is merely a matter of style, but you must be consistent as the C/C++ language is case sensitive.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.