Is there any way to determine the side of rotation, currently I am using a potentiometer

#include <Arduino.h>

#define Knob 4


int newValue;
int count;
int value;
int prevValue;


void setup() { 

  pinMode(Knob,INPUT_PULLDOWN);
  Serial.begin(9600);

  


} 

void loop() { 





 newValue = analogRead(Knob);

value = map(newValue,0,4095,0,100);

 if(value != prevValue && value>prevValue){
  Serial.println("++");
 }else {Serial.println("--");}

 delay(500);
 
 Serial.print(newValue);

 prevValue = value;




}

explain what you want to do with your potentiometer

if you want it to behave like a joystick you coud measure the voltage (0 - 4095) and decide that if the value is between [0 , 2000[ it turns CCW at a given speed, if it's between [2000, 2095[ it is stopped and if it's between [2095 and 4095] it turns CW at a given speed.

if you want to also drive the speed based on the pot, you could also use the information on how far close to 0 (goes faster CCW) or toward 4095 (goes faster CW) to make the decision.

The map function can help get the variable speed value.

I would do it in two steps in order avoid printing if nothing changed.

Untested:

#include <Arduino.h>

#define Knob 4

int newValue;
int count;
int value;
int prevValue;

void setup() {
  pinMode(Knob, INPUT_PULLDOWN);
  Serial.begin(115200);
}

void loop() {
  newValue = analogRead(Knob);
  value = map(newValue, 0, 4095, 0, 100);

  if (value != prevValue) {
    if (value > prevValue) {
      Serial.println("++");
    } else {
      Serial.println("--");
    }
    Serial.print(newValue);
    prevValue = value;
  }
  //delay(500); 
}

By moving stuff inside the if(value != prevValue) {...} it is doing "change detection" and can ignore the likely common no-change observations.

what i want to do is when i turn potentiometer clockwise then it should increment value +1 otherwise increment -1….

I would start here with a base understanding of the circuit you have created. There are a number of circuits you could create, so you need to tell us more than just that there is a potentiometer.

Do you understand that a potentiometer is a variable resistor, so it will resist from a lower ohm to a higher ohm. What you detect on the analog input is a voltage that is dropped by the varying resistance. Again this depends on the circuit you have created.

It is still not clear what you want.

For instance, if it sits still (another way of not turning clockwise) do you want value to decrement?

Or with this delay:

If you turn the potentiometer all the way to the end in 0.5 sec, what do you want to have happen next?

i am using ESP32s3 as a microcontroller.... programming in Platform IO (Arduino environment) Visual Studio code. still, you guys don't understand...when I am turning the potentiometer Clockwise should increment by+1 and if I am turning the potentiometer Counterclockwise then it should Decrement by -1...then if I am not turning the knob of the potentiometer it should not increment or decrement....as for the potentiometer i am using 10kohm potentiometer ....the left pin of the potentiometer is connected to the Ground of esp32s3 and third pin connected to the 3.3volt pin and ,middle pin is connected to the pin four of the esp32s3. (if you look from the side where the knob is attached) esp32s3 pin four can be used as digital or can also be used as Analog because that pin supports Analog to Digital Converter[ADC]
i hope this helps to understand.....

Pots are often bad for increment/decrement operations because they have limits.

If the pot is turned all the way clockwise when you power the system on, what do you want to have happen? Whatever the thing is that is incrementing can't increment because it would already be at the limit.

The code you provided already has a a variable value in the 0-100 range (that might be further constrained by the quality of the ESP32 ADC) are you talking about incrementing/decrementing that variable or a different variable?

Here's a modification of the now-tested code from #3

#include <Arduino.h>

#define Knob 4

int newValue;
int count;
int value;
int prevValue;
int otherValue = 0;

void setup() {
  pinMode(Knob, INPUT_PULLDOWN);
  Serial.begin(115200);
}

void loop() {
  newValue = analogRead(Knob);
  value = map(newValue, 0, 4095, 0, 100);

  if (value != prevValue) {
    Serial.print("value:");
    Serial.print(newValue);
    Serial.print(" othervalue");
    if (value > prevValue) {
      Serial.print("++");
      ++otherValue;
    } else {
      Serial.print("--");
      --otherValue;
    }
    Serial.print("=");
    Serial.println(otherValue);
    prevValue = value;
  }
  //delay(500); 
}

and its results from changing from full CCW to CW(fast) and back to full CCW(slow):

value:508 othervalue++=1
value:2266 othervalue++=2
value:4095 othervalue++=3
value:3975 othervalue--=2
value:3807 othervalue--=1
value:3687 othervalue--=0
value:3563 othervalue--=-1
value:3515 othervalue--=-2
value:3467 othervalue--=-3
value:3419 othervalue--=-4
value:3346 othervalue--=-5
value:3250 othervalue--=-6
value:3178 othervalue--=-7
value:3034 othervalue--=-8
value:2938 othervalue--=-9
value:2770 othervalue--=-10
value:2722 othervalue--=-11
value:2506 othervalue--=-12
value:2386 othervalue--=-13
value:2218 othervalue--=-14
value:2050 othervalue--=-15
value:1781 othervalue--=-16
value:1661 othervalue--=-17
value:1349 othervalue--=-18
value:1157 othervalue--=-19
value:989 othervalue--=-20
value:773 othervalue--=-21
value:556 othervalue--=-22
value:408 othervalue--=-23
value:288 othervalue--=-24
value:144 othervalue--=-25
value:24 othervalue--=-26

It is easy to determine the "side of rotation" of a potentiometer, but incrementing/decrementing likely won't work well with the inherent hard limits of a potentiometer.

are you sure you have a potentiometer or is it a rotary encoder (which would be better suited for what you do)

1 Like

Shall the direction signal happen only once when movement is detected (a subproblem) or continuously while the pot is being turned?

I don't have a rotary encoder...

Do you want the behavior of a rotary encoder? A pot will not work like a rotary encoder because it had hard limits. Maybe you could add a button to inhibit the incrementing/decrementing so you could re-adjust the pot away from a limit and get more travel. But if you want non-awkward rotary encoder behavior, get a rotary encoder.

so what happens if you rotate till it reads 4095 and you still want to increase the associated count? you are stuck...

you could just use the absolute position to drive your associated count.

If you have the item connected to 5 volts as shown in analog read, as you rotate to low Ohms will increase the value returned by analog read as your voltage increases to closer to 5 volts. If you rotate to high Ohms, you decrease the value returned by analog read as your voltage decreases to ground. So, if you rotate a little, it will add or subtract a little and if you rotate more it will add or subtract more. If you leave it in the same place, it will stay the same value.

You can even map the values so that the range of the potentiometer outputs the number you want from x to x+n as you lower the resistance and from x+n to x as you increase the resistance.

This is why I don't understand what you want to do because it sounds like this is not adequate for you.