Need help with servo sketch

Hi,
Basically I have a modified servo knob sketch that I have slightly modified. Problem I am having is I need to constrain the pot input reading between 350 and 850. This works fine and all, but when the pot reading falls under (0-350) this range too quickly the servo gets stuck somewhere in the middle of its motion and not returning to its servo min location. I need it to return the to its servo min location when the pot falls under 350 but I can not seem to figure out how to do it. My pot is capable of moving rather quickly in its locations. If I move the pot slowly then everything is fine but sadly it will not always work like this.

#include <Servo.h>

Servo myservo;


const int  SERVO_MIN = 0; 
const int  SERVO_MAX = 180; 
const int  potmin = 350;
const int  potmax = 850;
int potpin = 0;
int val;




void setup()
{
myservo.attach(9);
}
void loop()
{
val = analogRead(potpin);  
         
         
val = map(val, potmin, potmax, SERVO_MAX, SERVO_MIN );  

        
myservo.write(val);    
 
delay(15);      
}

Map() does not stop mapping when the input falls outside of the range potmin to potmax. Use the constrain() function before map to keep the input values between potmin and potmax.

ok, I tried that with no luck… maybe I am doing it wrong. I have little experience with coding.

#include <Servo.h>

Servo myservo;


const int  SERVO_MIN = 0; 
const int  SERVO_MAX = 180; 
const int  potmin = 250;
const int  potmax = 850;
int potpin = 0;
int val;






void setup()
{
myservo.attach(9);
}
void loop()
{
val = analogRead(potpin); 

val = constrain(val, 250,850);       
         
val = map(val, potmin, potmax, SERVO_MAX, SERVO_MIN );  

        
myservo.write(val);    
 
delay(15);      
}

We usually do debugging by looking at actual values. Something like sticking a Serial.print(val) immediately after the analogRead() and another just before the write(). Then you can see on the monitor exactly what you are getting from the pot and what you are writing to the servo.

But servos can't move very fast so if your pot is going back and forth too quickly it is quite likely that the servo won't be able to keep up with it.

It's also possible that you are having problems with power (that's the cause of almost all servo problems). What sort of servo do you have and exactly how is it connected and powered (it shouldn't be via the Arduino 5V pin, though you might get away with it if it's a single very small servo). Circuit diagram and picture of the layout might both be useful.

Steve

I am using a trinket so I can not do serial printing. My servo is being powered by a 6v buck convertor, capable of 3 amps. I have a brand new hitec d625mw digital servo. Everything works perfect except for when the potentiometer falls out of its scaled value. if I scale them 1:1 like 0,1023, 0, 180 then there is no issue.

Except i need the first and last quarter of the pot to be a dead zone. Problem is if i turn my pot all the way to max servo position and then QUICKLY turn it towards its min position it follows along until the pot is out of its middle working constrain. Leaving the servo almost to its minimum position but not quite there. As soon as i start tuning the pot again, and it enters the scaled region the servo jumps to its min then climbs from there?

I hooked up a servo and a pot to my Mega and loaded your code. It works as expected. Turn the pot from 0 and the servo does not move till the analog read is over 250. The servo then moves from 0 to 180 as the pot is turned and until the analog read reaches 850. Then the servo stops at 180 as the analog read increases. The code seems to be doing what you want even as quickly as I can manage to turn the pot. If your results are different, the fault does not seem to be with the code.

Is the trinket a 3.3V device? Is the servo rated to work with a 3.3V signal?

It is a 5v trinket. Interesting that it works fine on your end.... Wonder if it could be a programed setting inside my servo that needs changed with the dpc-11 hitec programmer.

Yeah, I don't know. I used one of the small hobby servos. I did the test before you posted about the digital servo.

Its almost like the code stops sending a signal to the servo once it falls out of the range and leaves my servo hanging real close to the min angle.

So, i set up a failsafe position in the hitec servo program so when this happens now the servo goes to the position i have set, meaning there is no signal being sent to the servo. problem with using that as a bandaid is when i turn my pot past its MAX constraint that it defaults my servo to that failsafe location instead of staying at its max positon.

One other thing i noticed is when i change my min/max constrain positions in the mapping at also messes with the min/max servo angle i have set. I only need the servo to rotate roughly 90 degree. Is that because i am mapping everything together?

#include <Servo.h>

Servo myservo;


const int  SERVO_MIN = 40; 
const int  SERVO_MAX = 120; 
const int  potmin = 250;
const int  potmax = 850;
int potpin = 0;
int val;






void setup()
{
myservo.attach(9);
}
void loop()
{
val = analogRead(potpin); 

val = constrain(val, 250,850);       
         
val = map(val, potmin, potmax, SERVO_MAX, SERVO_MIN );  

        
myservo.write(val);    
 
delay(15);      
}

Hi,
Change;

val = constrain(val, 250,850);

To;

val = constrain(val, potmin, potmax);

In your code if you change potmin and potmax, you are not changing the constrain values as well.

Tom.. :slight_smile: