How do I make the value of the potentiometer only print once?

int potential = A0; //A0 is a HEXADECIMAL number
int potValue;

void setup() {


  pinMode(potential, INPUT);
  Serial.begin(9600); // Setting baud rate to 9600 bits per second
 

}

void loop() {



  potValue = analogRead(potential);
  Serial.println(potValue);

  
}

What I'm trying to do is:
if the value of potentiometer =1023,
it will print only once in the serial monitor until the value is changed.

You need a variable.
Compare the new value with that variable, to detect if something has changed.
Then store the new value in that variable for the next time.

2 Likes

So, if I get potValue and make a potValue2, should I compare them in an 'analogRead' statement?

You can name it: "oldPotValue" or "previousPotValue", or just "oldValue".

Then use a if-statement:

potValue = analogRead(potPin);

if( potValue != oldPotValue)
{
  Serial.println("Something has changed");
  oldPotValue = potValue;      // remember for the next time
}

I tried this and it doesn't work? It does print the "Something has changed" but it keeps repeating the value of the potentiometer on top of "Something has changed" I'm looking for it to only print the value one time, and when the value changes, it prints the current value only once. :sob:

int potential = A0; //A0 is a HEXADECIMAL number
int potValue;
int lastPotValue;

void setup() {


  pinMode(potential, INPUT);
  Serial.begin(9600); // Setting baud rate to 9600 bits per second
 

}

void loop()
{
  potValue = analogRead(potential);
  if(potValue != lastPotValue)
  {
    lastPotValue = potValue;
    if(lastPotValue == 1023)
      Serial.println(lastPotValue);
  }    
}
1 Like

well if you kept this

then for sure it's printed.... move the Serial.println(potValue); inside the if

1 Like

If the potentiometer is stationary, the ADC count may vary slightly due to noise.
You might want to check that it doesn't change by more than some threshold, before you print the new value.

Thank you. Saved my life. :pray:
The 2nd 'if' statement was unnecessary though.

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