Detect turning direction of potentiometer with ResponsiveAnalogRead

Hi all,
I'm writing the code to read the potentiometer status using ResponsiveAnalogRead.h . I followed the tutorial https://github.com/dxinteractive/ResponsiveAnalogRead.

I got the stable values. However, i want to modify the code to detect the turning direction but was not able to do that.
Please see the code below. Your help would be much appreciated.

#include <ResponsiveAnalogRead.h>

const int sensorPin = A0;
int sensorBe4;
int sensorAft;
int moveDetect;
ResponsiveAnalogRead pot(sensorPin, true);

void setup() {

  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}

void loop() {
  pot.update();
  sensorBe4 = pot.getValue();
  sensorBe4 = map(sensorBe4, 0, 1023, 0, 250);
  delay(20);

  sensorAft = pot.getValue();
  sensorAft = map(sensorAft, 0, 1023, 0, 250);
  delay(5);

  moveDetect = sensorAft -sensorBe4;
  delay(5); 
// POT TURNING DIRECTION
    if (moveDetect >0){
      Serial.println("ClockWise Direction:");
      delay(100);
      }
    if (moveDetect < 0) {
      Serial.println("Counter Clockwise Direction:");
      delay(100);
      }
    if (moveDetect == 0) {
      Serial.println("No Turn");
      delay(100);} 
}

You do that exactly the same as when you use the millis function. Subtract the new value from the previous value. You MUST define in your mind and in your code what a negative difference and what a positive answer is. The sign will tell you the direction of the change.
When done, then move the new value to the previous value so you are set for the next time.

1 Like

Hi,
Thanks for the suggestion. I'll try it and let you know.

Try this code:

#define sensorPin  A0
int sensorBe4;
int sensorAft;
int sensitivity = 2;
unsigned long printOut = millis();
unsigned long myDelay = 500;
//------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}
//------------------------------------------------------------------
void loop() {
  sensorBe4 = map(analogRead(sensorPin), 0, 1023, 0, 250);
  if (abs(sensorBe4) > abs(sensorAft) + sensitivity ) {
    Serial.println("ClockWise Direction:");
    sensorAft =  sensorBe4;;
    printOut = millis();
  }
  if (abs(sensorBe4) < abs(sensorAft) - sensitivity  ) {
    Serial.println("Counter Clockwise Direction:");
    sensorAft =  sensorBe4;
    printOut = millis();
  }
  if (millis() - printOut > myDelay) {
    Serial.println("No Turn");
    printOut = millis();
  }
}

Thanks a lot Ruilviana, i'm appreciated.
It works but not very responsive. I adjusted the sensitivity number but the result not as expected.
I'm trying to make the ResponsiveAnalogRead library to work with the millis(). I don't know how to combine them together yet.
Does anyone know how to view / modify the update() and getValue() ?
Thanks

Hi, @arduno_dude

What potentiometer are you using?
value and taper.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hi TomGrorge,
Im using 1K pot, typical one.
Max value is 1023. Not sure the taper though.
Thanks

There is no typical potentiometer. There are linear, log, reverse log, Single turn, multiple, and many others, but there is no typical.

https://eepower.com/resistor-guide/resistor-types/potentiometer-taper/#using-arduino:programming-questions

sensitivity = 2 might be extremely sensitive. Try with 100 and check how it works.

First things I would try, is to check if the code is able to detect wide turns. 90° or something like that.

Sorry for my English.

getValue() will return the reading made during the last call to update()

Print both values to verify that they are the same.

You should call update() again before the second reading.

Try also using hasChanged() to determine … if the value has changed

And, you do not need this:

as the constructor takes care of this

1 Like

sorry i wasn't clear. I'm using a single turn rotary potentiometer.

[edit] Comments added.

int potValue, oldPotValue, potPin = A0;

void setup() {
  Serial.begin(115200);
  Serial.println("Center potentiometer around 512.");
  while (analogRead(potPin) < 500 || analogRead(potPin) > 524); // deadband, start
}

void loop() {
  potValue = analogRead(potPin); // read current pot value
  if (oldPotValue != potValue) { // compare to stored pot value
    if (oldPotValue > potValue) // if old value is greater than new value
      Serial.println("<- CCW"); // pot was turned "left"
    else if (oldPotValue < potValue) { // but if old value is less than new value
      Serial.println("CW -->"); // pot was turned "right"
    }
    oldPotValue = potValue; // store current pot value
  }
}

Awesome!
Many thanks

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