Determining if a potentiometer has been turned a certain ammount

I've got a potentiometer defining its initial reading as 'original', I also have a smoother running to generate an 'average' value. I can get the serial monitor to print -1 or +1 depending if the average value is greater or less than the original value. But I am having difficulty coding to determine when the pot hasn't been twisted at all.

What I can't figure out is how to set a band value for the neutral position.....so that the clockwise or anticlockwise movement is only detected once it gets turned above or below a threshold ie. the average value would need to increase/decrease by +/-10 values before it is picked up as a twist.

Any advice would be great.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Have you got a bypass capacitor on the wiper of the pot to the gnd end of the pot?
0.1uF will help with any noise.

Thanks .. Tom.. :slight_smile:

Sorry for not formatting the code earlier. The setup is just a 3 pin connection but underneath the pot there are three switches which I have np\ot seen on other pots I've used(photo attached). The smallest increments I can get the pot to turn in is units of +/-8. I've managed to get it working by defining original_max and original_min values but if you would have done it more efficiently please let me know.
Underneath Pot

const int numReadings = 10;
int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;  // the average
int original_max;
int original_min;
int key =0;
int original=0;

int inputPin = A5;

void setup() {
  // initialize serial communication with computer:
  Serial.begin(9600);
  original=analogRead(inputPin);
  original_max = original + 10;
  original_min = original -10;
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(inputPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    readIndex = 0;
  }


if (average > original_max) {
    key = 1;
  }

if (average < original_min) {
    key = -1;
  }
  
if ((average > original_min) & (average < original_max)){
    key = 0;
  }
  
  average = total / numReadings;
  
  Serial.println(key);
  delay(1);        // delay in between reads for stability
}

Hi,

Can you post a link to where you got this pot please.

Can you post pictures of the other side of this pot, why is it enclosed?

Thanks.. Tom... :slight_smile:

It's a pot Jim, but not as we know it.

The simplest way of seeing if a pot has moved is to take the absolute value of the current pot reading subtracted from the last pot reading that you treated as significant ( not the last reading you took ). So if you want a vlaue change of 10 to trigger things use:-

currentReading = analogRead(pin);
if( abs(currentReading - lastReading) >= 10 ) {
lastReading = currentReading;
// add what you want to do with a pot change here
}

Hi,
Have you written a simple no bells and whistles code that reads the raw 0 to 1023 values from the analog input and outputs them to the serial monitor?
This is so you can see if your pot values are valid as you turn it from one end of its travel to the other?

Thanks .. Tom... :slight_smile:

Hi - I think i have got the coding going ok now.
This is the top of the pot, I didn't buy it so I don't know where it was purchased. It says 'turn & spin 1.0' on the label:

Can anyone recommend a solid way of using a pot in a project - I want to get the pot rotated by bicycle handlebars and I need it to be a bit robust, I have seen some projects where people used elastic and some where people have placed the pot inside a skateboard wheel.... this query is probably more related to hackaday but I thought I'd ask anyway.
Thanks for your help.

Hi,
Cut the heatshrink off and see what is under it, check the wiring.

Tom... :slight_smile: