Confront previous / current potentiometer position

Hello everyone!
I would need some help with one section of the code that I'm writing. I have two servos controlled by a potentiometer.

I would need the following thing to happen:

If the potentiometer is rotated more then 5 degrees to the left, the first servo goes from 0° to 180°, if the potentiometer is rotated more than 5 degrees to the right, the second servo should go from 0° to 180° while the first one goes back to 0° and viceversa.

The system will be used in a helicopter to have the collective less hard to move. If the collective is moved up, the first servo pulls a spring connected to the collective so that its less hard to pull up. If the collective is moved down, the second servo pulls the spring, while the first one goes back to resting position.

Collective movement are usually slow giving the servos enough time to move.

How can i make such code for the potentiometer position check? So that it sees if its being turned right or left and react in base of that?

The potentiometer doesnt need to keep in memory a default position as it needs to understand only if its being moved left or right when confronted to the latest position.

Thanks

A potentiometer is a resistance device, ranging from lowest (maybe 0) at the full counter-clockwise, to highest (for example, 10k) at full clockwise, and you would have to ensure it was in the middle (about 5k) for your "+/-5degrees off center" to work.

I think you want a rotary encoder, which will be at center/middle at start up, can register if you move left or right, can be mapped to "+/- 5 degrees," and also rotates continuously, CW or CCW.

Take a look at this presentation of rotary encoders...

Hello and thanks for your reply,
potentiometers are rad by arduino with a value from 0 to 1023. I would need to confront the previous vs current value registered to then activate either servo 1 or servo 2.

Rotary encoders would definetly work, but I don't believe are necessary. If not of 5 degrees, even 5 points (of the 0...1023 scale) is ok.

Thanks

If you will be using the potentiometer, then you will should try the "map()" function.

int value = map(value, fromLow, fromHigh, toLow, toHigh);

For your potentiometer mapped into "-5 to +5" try

int value = map(analogRead(potentiometer), 0, 1023, -5, 5)

This should give an output of -5 to +5 with 0 in the center.

You will want to interpret the value into your servo movement.

I agree with xfpd as to using a map function. Keep a few things in mind. Using a pot if I apply 5.0 volts acrodd the pot the wiper will output 0.0 to 5.0 volta on the wiper. I would run the pot wiper into an analog input of your Arduino which should give you about 0 to 1023 counts (bits). Just about 512 bits being pot center. A 10K pot at center of span should be about 2.5 volts out and about 512 bits.

You mention rotating your pot +/- 5.0 degrees from center is my read. You mention 5.0 degrees and I assume from center or 512 bits. Short of multi turn pots the average off the shelf generic potentiometer has about 270 degrees of rotation, some more and some less. If we call it 270 degrees so figure each degree of change on the pot in this example will be about 18.5 mV. A 5.0 degree change in either direction from center will only be about 92 mV (not very much). Consider your Arduino using a 10 bit ADC will only give you about 4.8 mV step changes. Your +/- 5.0 degree rotation from your pot center is not going to give you much to work with.

I would start simple only reading your bit count on your Arduino and get some real world numbers and then apply what you get using the MAP function on two digital out pins to drive each servo.

All of that assumes I am reading you correctly? :slight_smile:

Ron

Hello,
yes that is correct I didn't do the math properly. I will try using the map function, yet I still have an issue about comparing the previous and current pot. value to understand if its being moved left or right to then activate either servo1 or servo2

Compare CURRENT potentiometer reading to PREVIOUS reading... with this, you do not need to map()... the ADC readings of the potentiometer will range from 0 (full anti-clockwise) to 1023 (full clockwise)...

void setup() {
  Serial.begin(9600);
}

void loop() {
if (previousValue < currentValue)
  Serial.println("LEFT");
else if (previousValue > currentValue)
  Serial.println("RIGHT");
 
  previousValue = currentValue;
  currentValue = analogRead(potentiometer);
}

Use a return to center joystick like the KY-023 (left):

Add your own servo code.
Untested.
Leo..

const byte potPin = A0;
int rawValue, potValue;

void setup() {
  potValue = analogRead(potPin); // pre-read
}

void loop() {
  rawValue = analogRead(potPin); // read
  if (abs(rawValue - potValue) > 20) { // 5 degrees is about 20 A/D values
    if (rawValue < potValue) {
      // 5 degrees left action
    }
    else {
      // 5 degrees right action
    }
    potValue = rawValue; // reset
  }
}

What if rawValue == potValue? (no movement)... see post#7... check for < and > but do nothing with ==

The first if will only run if the difference between old and new pot value is >20.
Leo..

1 Like

Overcome this first as springs will not suffice connected to a collective device and so is flawed to begin with.

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