Update Value Only When it changes

Hi,

I'm wondering if someone can help me with an issue. I have a potentiometer that sends values to the serial monitor, using very simple code. What I need is code that will only print to the serial if the potentiometer value is changed and not constantly write the same value over and over again.

Your help is much appreciated ,

int Value = A0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(Value, INPUT);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(analogRead(Value));
  delay(500);

}

Add a variable where you can save a last variable value.

Compare this last variable with the current variable, if it changes more than an amount you think is reasonable, print the new variable and update the last variable.

Hi,
is it possible to give me some code, sorry I really can't seem to figure out the code.

thanks a lot

Maybe the code in this thread will help you.
https://forum.arduino.cc/t/debounce-and-single-press-ir-sensor-input/1019657/9
Look at posts 9 and 10.

Or, post the math. Then you can get help coding the math.

We will take this slow.

Here is sketch 1 of 3 (2 and 3 are to follow).

Do you fully understand what is happening this code ?



const byte analogPin = A0;

int currentValue;
int previousValue;


//********************************************^************************************************
void setup()
{
  Serial.begin(115200);

} //END of   setup()


//********************************************^************************************************
void loop()
{
  currentValue = analogRead(analogPin);
  
  Serial.println(currentValue);
  previousValue = currentValue;
  
  delay(500);

} //END of   loop()


//********************************************^************************************************

Yes i do understand whats going on here

Something to think about while you wait -

To know if something has changed, you have to know what it was before... :slight_smile:

Here is Sketch #2.

We compare the last reading with the current reading to see if they are different.

  • Sketch #3 will follow this sketch.


const byte analogPin   = A0;

int currentValue;
int previousValue;


//********************************************^************************************************
void setup()
{
  Serial.begin(115200);

} //END of   setup()


//********************************************^************************************************
void loop()
{
  currentValue = analogRead(analogPin);

  //****************************
  //are these two variables different ?
  if (currentValue != previousValue)
  {
    Serial.println(currentValue);

    //update the previous value
    previousValue = currentValue;
  }

  //****************************
  delay(500);

} //END of   loop()


//********************************************^************************************************

Do you understand what is happening in this sketch ?

Here is sketch #3.


The problem with sketch #2 is a difference of 1 (one) or more will evaluate, hence we will be printing to the serial monitor far too much.


What we need is a way to limit the printing only when the difference in the variables is more than a certain amount.

When the analog input is within a range +/- a certain amount, nothing gets printed.

When we go past this range, we print the new value and update the lastVariable.

See abs( )
https://www.arduino.cc/reference/en/language/functions/math/abs/

See hysteresis:


const byte analogPin   = A0;

//change this value to what's needed
const byte hysteresis  = 20;

int currentValue;
int previousValue;


//********************************************^************************************************
void setup()
{
  Serial.begin(115200);

} //END of   setup()


//********************************************^************************************************
void loop()
{
  currentValue = analogRead(analogPin);

  //****************************
  //are these two variables different "by at least 'hysteresis' amount" ?
  if (abs(currentValue - previousValue) >= hysteresis)
  {
    Serial.println(currentValue);

    //update the previous value
    previousValue = currentValue;
  }

  //****************************
  delay(500);

} //END of   loop()


//********************************************^************************************************

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