TrimPots

Hi guys,

I’ have a project i am working on and i am stuck at a point where i need to be able to adjust the threshold value and the max servo angle using external trim pots. I am lost… how would i go about this. I need the threshold adjustable between values 100 - 200 and the angle adjustment from 75 to 90 with the turn of a few pots without having to go into the code. I have done a lot of reading but haven’t learned anything.

Here is my code i have set up to control a servo with a potentiometer

Any help to get this going would be awesome!

// include the servo library
#include <Servo.h>

Servo myServo;  // create a servo object

int const potPin = A0; // analog pin used to connect the potentiometer
int potVal;  // variable to read the value from the analog pin
int angle;   // variable to hold the angle for the servo motor
int const threshold = 170;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  myServo.attach(9); // attaches the servo on pin 9 to the servo object
  Serial.begin(9600); // open a serial connection to your computer
}

void loop() {
  potVal = analogRead(potPin); // read the value of the potentiometer
  // print out the value to the serial monitor
  Serial.print("potVal: ");
  Serial.print(potVal);
 if (potVal > threshold)
  // scale the numbers from the pot
  angle = map(potVal,338,0, 0, 140);

  // print out the angle for the servo motor
  Serial.print(", angle: ");
  Serial.println(angle);

  // set the servo position
  myServo.write(angle);

  // wait for the servo to get there
  delay(15);
}

threshold.ino (1.01 KB)

This should get you started.

int threshold = 0; 
int potVal; 

void loop()
{
  potVal = analogRead(potPin); // read the value of the potentiometer
  // print out the value to the serial monitor
  Serial.print("potVal: ");
  Serial.print(potVal);
  int threshhold = map(potVal,0,1023, 100, 200);
  Serial.print("threshhold : ");
  Serial.print(threshhold);
  delay(1000);
}

I wonder if this arrangement might not cause some jitter. The analogRead() function has some variability from reading to reading even when the input voltage is stable. Perhaps you should use a hysteresis zone, and not recognize a change unless the newest value is, say +/- 3 away from the old value. (Needs experiment.) Capacitors on the analog lines can improve this problem.

Maybe i am out of my juristiction lol I got the servo to move with original code i posted but the "add-ins" i need to tune the servo seem difficult for me to get working. Kinda at the point where i think i should hire an expert to finish the code and get it over with but not sure if there is anyone that does that on here. I am so busy with work and stuff to learn this stuff like i would like to....

TheMachinist:
Maybe i am out of my juristiction lol I got the servo to move with original code i posted but the “add-ins” i need to tune the servo seem difficult for me to get working. Kinda at the point where i think i should hire an expert to finish the code and get it over with but not sure if there is anyone that does that on here. I am so busy with work and stuff to learn this stuff like i would like to…

It’s not terribly difficult. You just need a little time to look at it.

Yeah just a little discouraged. I’ll keep at it, got to get it working. I still couldn’t get your part of the code added in to where it works right… the servo just randomly twitches around. Check it out maybe im doing something wrong in the code.

// include the servo library
#include <Servo.h>

Servo myServo;  // create a servo object

int const potPin = A0; // analog pin used to connect the potentiometer
int potVal;  // variable to read the value from the analog pin
int angle;   // variable to hold the angle for the servo motor
int threshold = A1;

void setup() {
  myServo.attach(9); // attaches the servo on pin 9 to the servo object
  Serial.begin(9600); // open a serial connection to your computer
}

void loop() {
  potVal = analogRead(potPin); // read the value of the potentiometer
  // print out the value to the serial monitor
  Serial.print("potVal: ");
  Serial.print(potVal);
  int threshhold = map(potVal,0,1023, 100, 200);
  Serial.print("threshhold : ");
  Serial.print(threshhold);
  delay(1000);


  // scale the numbers from the pot
  angle = map(potVal,338,0, 0, 140);

  // print out the angle for the servo motor
  Serial.print(", angle: ");
  Serial.println(angle);

  // set the servo position
  myServo.write(angle);

  // wait for the servo to get there
  delay(15);
}

I wasn't trying to fix everything. I was just showing you how to read a pot and map it to a range of values. In this case, the Threshold value.

Do the prints show the threshold is now controllable from the pot in the range that you wanted?

You need to do the same thing for angle.

But for now, maybe try to simplify everything and make sure you can get the servo to work correctly without your pot control. Once that works, then you can put the pot control back in.

My original code works great. I added in the threshold trim pot how you suggested. It burns to the arduino and will no longer work like the original code, it just randomly bounces around, and on the serial monitor does not print out reading it just stays zero while moving the pot. I will try another pot tonight....maybe the one I was trying with is bad.

TheMachinist:
My original code works great. I added in the threshold trim pot how you suggested. It burns to the arduino and will no longer work like the original code, it just randomly bounces around, and on the serial monitor does not print out reading it just stays zero while moving the pot. I will try another pot tonight…maybe the one I was trying with is bad.

You didn’t combine my code with your original code correctly.

More like this (untested):

// include the servo library
#include <Servo.h>

Servo myServo;  // create a servo object

int const potPin = A0; // analog pin used to connect the potentiometer
int potVal;  // variable to read the value from the analog pin
int angle;   // variable to hold the angle for the servo motor
int threshold = 0;

void setup() {
    myServo.attach(9); // attaches the servo on pin 9 to the servo object
    Serial.begin(9600); // open a serial connection to your computer
}

void loop() {

    potVal = analogRead(potPin); // read the value of the potentiometer
    // print out the value to the serial monitor
    Serial.print("potVal: ");
    Serial.print(potVal);
    int threshhold = map(potVal,0,1023, 100, 200);
    Serial.print("threshhold : ");
    Serial.print(threshhold);
    delay(1000);  

    if (potVal > threshold)
    {
        // scale the numbers from the pot
        angle = map(potVal,338,0, 0, 140);
    }

    // print out the angle for the servo motor
    Serial.print(", angle: ");
    Serial.println(angle);

    // set the servo position
    myServo.write(angle);

    // wait for the servo to get there
    delay(15);
}

I wired the pot into A1

Should I get rid of the standard threshold in my first code for this to work?

What Kohm pot should I be using for this to work right

first get the program above working so you can see what it is doing.

change

int const potPin = A0; // analog pin used to connect the potentiometer

to

int const potPin = A1; // analog pin used to connect the potentiometer

if you need to.

Still no luck. Code works great before trying to add in the threshold pot, have all my numbers figured out in the range so that part is done. I cannot get the threshold to read a variable voltage in relation to the potetiometer position i have wired to pin A1. The print screen threshold number seems jumping around. I have tried multiple variable resistors so i know that i dont have a malfunctioning potentiometer.

// include the servo library
#include <Servo.h>

Servo myServo;  // create a servo object

int const potPin = A0; // analog pin used to connect the potentiometer
int thresHold = A1;
int potVal;  // variable to read the value from the analog pin
int angle;   // variable to hold the angle for the servo motor





void setup() {
  myServo.attach(9); // attaches the servo on pin 9 to the servo object
  Serial.begin(9600); // open a serial connection to your computer

}

void loop() {

  potVal = analogRead(potPin); // read the value of the potentiometer
  // print out the value to the serial monitor
  Serial.print("potVal: ");
  Serial.print(potVal);
  {
    thresHold = map(thresHold, 0, 677, 100, 200);
  }
  thresHold = analogRead(thresHold);
  Serial.print("thresHold : ");
  Serial.print(thresHold);
  delay(500);

  if (potVal > thresHold)
  {

    // scale the numbers from the pot
    angle = map(potVal, 338, 0, 0, 140);
  }

  // print out the angle for the servo motor
  Serial.print(", angle: ");
  Serial.println(angle);

  // set the servo position
  myServo.write(angle);

  // wait for the servo to get there
  delay(500);
}

You are not doing what I tell you to do, so it is difficult to help.

You cannot do this:

thresHold = analogRead(thresHold);

where threshold is set to pin A1, and you are trying to read a value.

when you assign a pin, do it like you did with A0. which is to make it const. Then you cannot misuse it the way you are.

Haven’t had any luck getting the trimpot to work in this code. Any idea what i am doing wrong here? Ive been doing alot of research trying to learn. i am trying to read my second potentiometer off pin A4 and cant seem to get it to read to put out my activation threshold.

If i connect it to pin A0 i get reading on the print screen for (potpin2, threshold, potval) all getting the same readings from the pin A0 (different numerals, it is mapping my threshold scale correctly). Maybe i am not activating the pin somehow. At least thats what it seems to me…

Its prolly something simple i am not getting here. Problem is the only time i have to work on this is late at night when i am already tired from a long day, so its hard to think clearly.

#include <Servo.h>


Servo myServo;  // create a servo object

int const potPin = A0; // analog pin used to connect the potentiometer
int potVal;  // variable to read the value from the analog pin
int angle;   // variable to hold the angle for the servo motor

int potpin2 = A4;
int threshold;

void setup() {
  myServo.attach(9); // attaches the servo on pin 9 to the servo object
  Serial.begin(9600); // open a serial connection to your computer



}

void loop() {

   
  potpin2 = analogRead(threshold); // read the value of the potentiometer
  // print out the value to the serial monitor
  Serial.print("potpin2: ");
  Serial.print(potpin2);

  threshold = map(potpin2, 0, 677, 100, 500);

  Serial.print("threshold : ");
  Serial.print(threshold);
  delay(50);

  
if (potVal > threshold);
{

potVal = analogRead(potPin); // read the value of the potentiometer
// print out the value to the serial monitor
Serial.print("potVal: ");
Serial.print(potVal);
}




// scale the numbers from the pot
angle = map(potVal, 338, 0, 0, 140);


// print out the angle for the servo motor
Serial.print(", angle: ");
Serial.println(angle);

// set the servo position
myServo.write(angle);

// wait for the servo to get there
delay(50);
}

The single most important thing you can do is change this to const so you will get an error showing you how you are misusing it:

const int potpin2 = A4;

a pin is a constant.

You are not using analogRead correctly. It takes a Pin (constant) as a parameter, and returns a variable.

So, it should be like this:

  threshold = analogRead(potpin2); 

  Serial.print("threshold: ");
  Serial.print(threshold);

  threshold = map(threshold, 0, 1023, 100, 500);
  Serial.print("mapped threshold: ");
  Serial.print(threshold);

  delay(1000);

Ok, so i got everything reading correctly on the print screen. Now if i turn the servo control knob it starts turning the servo at the same point in the map regardless of what my threshold is saying.

#include <Servo.h>


Servo myServo;  // create a servo object

int const potPin = A0; // analog pin used to connect the potentiometer
int potVal;  // variable to read the value from the analog pin
int angle;   // variable to hold the angle for the servo motor



void setup() {
  myServo.attach(9); // attaches the servo on pin 9 to the servo object
  Serial.begin(9600); // open a serial connection to your computer



}

void loop() {

  int const potpin2 = A4;
  int threshold;
  int potval2;


potval2 = analogRead(potpin2); // read the value of the potentiometer
  
  // print out the value to the serial monitor
  Serial.print("potval2: ");
  Serial.print(potval2);

  threshold = map(potval2, 0, 677, 100, 300);

  Serial.print("threshold : ");
  Serial.print(threshold);
  delay(50);
{ 



potVal = analogRead(potPin); // read the value of the potentiometer
// print out the value to the serial monitor
Serial.print("potVal: ");
Serial.print(potVal);
}




// scale the numbers from the pot
angle = map(potVal, 338, 0, 0, 140);

if (potVal > threshold);

// print out the angle for the servo motor
Serial.print(", angle: ");
Serial.println(angle);

// set the servo position
myServo.write(angle);

// wait for the servo to get there
delay(50);
}

this

if (potVal > threshold);

has a semicolon on the end, and the next block should be in braces for the “if”. Plus you had some scope problems.

see if this gets you closer:

#include <Servo.h>

Servo myServo;  // create a servo object

int const potPin = A0; // analog pin used to connect the potentiometer
int const potpin2 = A4;

void setup() {
    myServo.attach(9); // attaches the servo on pin 9 to the servo object
    Serial.begin(9600); // open a serial connection to your computer
}

void loop() {
    int potval2 = analogRead(potpin2); // read the value of the potentiometer

    // print out the value to the serial monitor
    Serial.print("potval2: ");
    Serial.print(potval2);

    int threshold = map(potval2, 0, 1023, 100, 300);

    Serial.print("threshold : ");
    Serial.print(threshold);
    delay(50);

    int potVal = analogRead(potPin); // read the value of the potentiometer
    // print out the value to the serial monitor
    Serial.print("potVal: ");
    Serial.print(potVal);

    // scale the numbers from the pot
    int angle = map(potVal, 1023, 0, 0, 140);

    if (angle > threshold)
    {
        // print out the angle for the servo motor
        Serial.print(", angle: ");
        Serial.println(angle);

        // set the servo position
        myServo.write(angle);

        // wait for the servo to get there
        delay(50);
    }
}

So i got the code working! Had to move some things around in the map function (see code), first it was modifying the servo angle not the lower threshold number. Kinda weird sometimes the print screen scrolls data sideways rather than up-down depending on where the pots were turned. I did away with the print function because it was making things jumpy. Is that normal?

FYI i am using 3.3v to run pots, thats why the 0-677 units

Now i need to put a digital switch to over-ride everything and send servo to its 180 position…seems pretty simple i think :o

Thank You :slight_smile:

#include <Servo.h>

Servo myServo;  // create a servo object

int const potPin = A0; // analog pin used to connect the potentiometer
int const potpin2 = A4;
int const threshold2 = 60;




void setup() {
    myServo.attach(9); // attaches the servo on pin 9 to the servo object
    Serial.begin(9600); // open a serial connection to your computer
}

void loop() {
    int potval2 = analogRead(potpin2); // read the value of the potentiometer

    
    

    int threshold = map(potval2, 0,677, 450, 370);
            //Last two values adjust fully shut treshold window

    int potVal = analogRead(potPin); // read the value of the potentiometer
    
        // scale the numbers from the pot
    int angle = map(potVal, 60, threshold,  90, 180);

    if (potVal > threshold2)

    if (potVal < threshold)
    
    {
        
        myServo.write(angle);

        // wait for the servo to get there
        delay(5);
    }
}

Congratulations. You should format your code and remove double empty lines to make it easier to read. Use indentation consistently to help with readability as well.

Also, things like this are very confusing and can cause bugs later:

    if (potVal > threshold2)

    if (potVal < threshold)
   
    {
...
    }

you should change it to either:

    if (potVal > threshold2)
    {
        if (potVal < threshold)
        {
        ...
        }
    }

or

    if (potVal > threshold2 && potVal < threshold)   
    {
    ...
    }

You can choose your own style, but keep it consistent inside the file. This is much easier to read and find bugs in:

#include <Servo.h>

Servo myServo;  // create a servo object

int const potPin = A0; // analog pin used to connect the potentiometer
int const potpin2 = A4;
int const threshold2 = 60;

void setup() 
{
    myServo.attach(9); // attaches the servo on pin 9 to the servo object
    Serial.begin(9600); // open a serial connection to your computer
}

void loop() 
{
    // read the value of the potentiometer
    int potval2 = analogRead(potpin2); 

    // Last two values adjust fully shut treshold window
    int threshold = map(potval2, 0,677, 450, 370); 

    // read the value of the potentiometer
    int potVal = analogRead(potPin); 
   
    // scale the numbers from the pot
    int angle = map(potVal, 60, threshold,  90, 180);

    if (potVal > threshold2)
    {
        if (potVal < threshold)
        {
            myServo.write(angle);

            // wait for the servo to get there
            delay(5);
        }
    }
}

or, if you prefer a tight style. Note: related things are grouped together vertically, with an empty line between not so closely related things. Similar to formatting English with sentences and paragraphs.

#include <Servo.h>

Servo myServo;  // create a servo object

int const potPin = A0; // analog pin used to connect the potentiometer
int const potpin2 = A4;
int const threshold2 = 60;

void setup() 
{
    myServo.attach(9); // attaches the servo on pin 9 to the servo object
    Serial.begin(9600); // open a serial connection to your computer
}

void loop() 
{
    int potval2 = analogRead(potpin2); // read the value of the potentiometer
    int threshold = map(potval2, 0,677, 450, 370); // Last two values adjust fully shut treshold window

    int potVal = analogRead(potPin); // read the value of the potentiometer
    int angle = map(potVal, 60, threshold,  90, 180); // scale the numbers from the pot

    if (potVal > threshold2)
    {
        if (potVal < threshold)
        {
            myServo.write(angle);
            delay(5); // wait for the servo to get there
        }
    }
}