Read analog and print something if position has changed from up to down

I am trying to program potentiometer so that it has a scale of 50 and if the value has changed up or down one time then I need to print something for one time.

I cannot wrap my head around it and maybe someone could help.

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT);
}

void loop() {
int brtCurrent = map(analogRead(A0),1023,0,0,50);
Serial.println(brtCurrent);

if(it has moved one position up){
  print something
}

if(it has moved on position down){
  print something else
}
}

Something like

prevPotVal = potVal;
potVal = analogRead(potPIn);
if (potVal > prevPotVal) {
   // do something
}
else if (potVal < prevPotVal) {
  // do something else
}

…R

int potVal;
int prevPotVal;

void setup() {
Serial.begin(9600);
pinMode(A0, INPUT);

}

void loop() {
prevPotVal = potVal;
potVal = map(analogRead(A0),1023,0,0,50);
if (potVal > prevPotVal) {
   Serial.println("up");
}
else if (potVal < prevPotVal) {
  Serial.println("down");
}

}

With this setup it is not precise. If I turn the potentiometer “upwards” then I am printing “down” also at random occurrences.

So, put some deadband in.

potVal = map(analogRead(A0),1023,0,0,50);  // backwards

AWOL:
So, put some deadband in.

Could you give me an example? :slight_smile:

Hiidtubakas: Could you give me an example? [of deadband]

Do something like this, so it only registers as a move if it's more than 2:

if (potVal > (prevPotVal + 2))

manor_royal: Do something like this, so it only registers as a move if it's more than 2:

if (potVal > (prevPotVal + 2))

Doesn't help. Any more suggestions?

Doesn't help.

Post expectations, observations and code.

Hiidtubakas: Doesn't help. Any more suggestions?

Worked for me. My value was changing sometimes by 1 just presumably with spontaneous movements of the wiper on the pot if I breathed too hard.

manor_royal: Worked for me. My value was changing sometimes by 1 just presumably with spontaneous movements of the wiper on the pot if I breathed too hard.

int potVal;
int prevPotVal;

void setup() {
Serial.begin(115200);
pinMode(A0, INPUT);

}

void loop() {
prevPotVal = potVal;
potVal = map(analogRead(A0),1023,0,0,50);
if (potVal > (prevPotVal + 2))  {
   Serial.println("up");
}
}

This prints nothing on the serial monitor. And if I remove the 2 from if statement then it prints "up" no matter which way I am turning the knob. Not acceptable.

The only Serial.println you have in that sketch says:

   Serial.println("up");

do you really expect anything else to be printed??

another case of tossing it over the fence for someone else to fix. then saying "unacceptable"

Dan95: another case of tossing it over the fence for someone else to fix. then saying "unacceptable"

Yeah I was thinking OP should call the Arduino helpdesk ;)

Have you considered printing the values of potVal and prevPotVal before you test them to see if they are what you expect ?

Hiidtubakas: Doesn't help. Any more suggestions?

What have you yourself thought about as a possible improvement? Or what do you think might be causing the problem? What experiments have you done to investigate the problem?

For example have you tried increasing the deadband?

I'm with @Dan95 ...

...R

UKHeliBob: Have you considered printing the values of potVal and prevPotVal before you test them to see if they are what you expect ?

Yes I have. They are correct but I am thinking that it is something to do with the unaccuracy of potentiometer and probably need to do some signal filtering or adding a hall effect to it for precision.

As previously suggested, have you tried increasing the size of the deadband ?

int potCurrent;
int potInitial;

void setup() {
  Serial.begin(115200);
  pinMode(A0, INPUT);
  potInitial = map(analogRead(A0),1023,0,500,0);
}

void loop() {

potCurrent = map(analogRead(A0),1023,0,500,0);

  if(potCurrent!=potInitial){
    
    if(potCurrent>potInitial && (potCurrent-potInitial)==10){
      Serial.println("up");
      potInitial=potCurrent;
    }
    else if(potCurrent<potInitial && (potInitial-potCurrent)==10){
      Serial.println("down");
      potInitial=potCurrent;
    }    
  }
}

Problem solved. I am not sure whether it was the “deadband” that was suggested here but now I got it working. Posted the code for someone else in need of help in the future.

potInitial - potCurrent) == 10Relying on exact equality is probably not a good idea as it would be easy to miss when the pot is adjusted. In any case, it seems to me that you should simply check whether the pot has moved by at least a certain amount in the appropriate direction, which is what I think you were doing originally.

If I checked for a change in both direction then it was working when I turned the potentiometer very fast but at slow movements, I was printing a mix of ups & downs on the serial monitor.

EDIT: Now it doesn’t respond well to fast movements. UKHeliBob you are right.

If i replace that exact equality with > or < then I’m getting jitter again.

int potCurrent;
int potInitial;

void setup() {
  Serial.begin(115200);
  pinMode(A0, INPUT);
  potInitial = map(analogRead(A0),1023,0,510,0);
}

void loop() {

potCurrent = map(analogRead(A0),1023,0,510,0);

  if(potCurrent!=potInitial){
    
    if(potCurrent>potInitial+9){
      Serial.println("Z11 UP");
      potInitial=potCurrent;
    }
    else if(potCurrent<potInitial-9){
      Serial.println("Z12 DOWN");
      potInitial=potCurrent;
    }    
  }
}

EDIT: I found out that I need 51 prints not 50 but that is irrelevant (changed 500 to 510). But now I have to add 9 to if statement potInital value and it works great at low and high speed but I am thinking, why nine?

int potInitial;

void setup() {
  Serial.begin(115200);
  pinMode(A0, INPUT);
  potInitial = analogRead(A0);
}

void loop() {

int potCurrent = analogRead(A0);

  if(potCurrent!=potInitial){
    
    if(potCurrent>potInitial & (potCurrent-potInitial)>19){
      Serial.println(F("Z11"));
      potInitial=potCurrent;
    }
    else if(potCurrent<potInitial & (potInitial-potCurrent)>19){
      Serial.println(F("Z12"));
      potInitial=potCurrent;
    }    
  }
}

EDIT: Got rid of map() function as its not necessary and so far it works like what I need. It may be the case that its printing 52/53 times instead of 51 but for my application its acceptable as long as it is not under 51. Still have to look over capacitor because without it the whole system goes haywire.