Hello,
I am doing a project that involves a potentiometer controlling a servo motor. However, I do not want the servo to move with the pot entirely, like it does when using the Knob program. I would like the servo to move at a specified pot value, if possible. I have been searching for ways to do this but have not had any luck. The pot is fixated on a joint, so it is moving with the joint and when the joint reaches a certain angle, I want to program the pot to control the servo. Is this something I have to manipulate in the map() function? Or add another element to my code? I am a beginner at coding so I am a little confused as to what I need to add.
I appreciate any feedback and tips.
Thanks so much.
francism4:
to my code ?
where is your code ?
please post it here, using code tags
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
Serial.begin(9600);
myservo.attach(10); // attaches the servo on pin 10 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
Serial.println(val);
delay(15); // waits for the servo to get there
}
I am still using the Knob code from the arduino library. I havent had luck with anything else, so i am trying to modify off this one.
francism4:
Hello,
I am doing a project that involves a potentiometer controlling a servo motor. However, I do not want the servo to move with the pot entirely, like it does when using the Knob program. I would like the servo to move at a specified pot value, if possible. I have been searching for ways to do this but have not had any luck. The pot is fixated on a joint, so it is moving with the joint and when the joint reaches a certain angle, I want to program the pot to control the servo. Is this something I have to manipulate in the map() function? Or add another element to my code? I am a beginner at coding so I am a little confused as to what I need to add.
I appreciate any feedback and tips.
Thanks so much.
How are intending to tell the program "I am moving the pot, but don't respond to the movement"? Then how do you intend to tell the program "the pot is set, begin movement"?
Paul
Paul_KD7HB:
How are intending to tell the program "I am moving the pot, but don't respond to the movement"? Then how do you intend to tell the program "the pot is set, begin movement"?
Paul
That is my question. I am not sure how to do that.I was thinking some type of for loop or something that when it reaches a certain bit, thats when the motor turns on. I am unsure how to write the code exactly though.
A lot will depend on the direction of moving the pot control.
Paul
Your requirements are not clear. How far do you want to turn the potentiometer before the servo starts to move? Does the servo have to move over the full 180 degrees controlled by the part rotation of the potentiometer or how far does it need to move?
To me it sounds like you need an if statement to check if the pot has moved far enough yet i.e. is 'val' is high enough. If it is then change the map() statement to account for the smaller range of pot movement that you're interested in and write it to the servo as now. Give it a try and if you still have problems post your attempt here and tell us what it does and what you want that's different.
Steve
Do not understand the requirements.
Initially I thought it was in reference to speed but the more one reads, the more confusing it gets.
I have created a code that addresses my needs but am still running in to issues. I am mainly having problems with the servo doing the operation that it is given, and I believe it is due to how I have it written, but am unsure of how else to write a command.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int servowait = 165; // position 1 of servo
int servoreturn = 55; // position 2 of servo
void setup() {
Serial.begin(9600);
myservo.attach(10); // attaches the servo on pin 10 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 270); // scale it to use it with the servo (value between 0 and 270)
if((val <= 190) && (val >= 170)) { //is pot reading between 190 & 170?
myservo.write(servoreturn); //send servo to position at 55
}
else if((val >= 35)&& (val <= 50)) { // is pot reading between 35 & 50?
myservo.write(servowait); // send servo to position 165
}
Serial.println(val);
delay(50); // waits for the servo to get there
}
Congratulations on getting that far, but just saying you are "having problems with the servo doing the operation that it is given" isn't very helpful. Exactly what is the servo doing that you think is wrong and when is it doing it?
The commands look fine so I would guess your problems are with the way the servo is wired or powered. What type of servo is it, what is powering the system and how exactly is everything connected?
Steve
slipstick:
Congratulations on getting that far, but just saying you are "having problems with the servo doing the operation that it is given" isn't very helpful. Exactly what is the servo doing that you think is wrong and when is it doing it?
The commands look fine so I would guess your problems are with the way the servo is wired or powered. What type of servo is it, what is powering the system and how exactly is everything connected?
Steve
Its a TowerPro MG995R. It is connected to the servo port on an Adrafruit motor shield. So to be more clear with what the servo is doing is that it sometimes gets stuck, I guess you could say, in the when the pot is moving and hits that range it does nothing. Then when you rotate the pot back in the opposite direction, the servo will moving, but now is moving in the opposite direction than the rotation of pot, which I do not want. I want the servo to follow the direction of the pot.
Also with that, the servo will moving in both directions during the duration of a pot rotation, which is also not wanted. Basically, rotate the pot in one direction, servo moves at the specified range. Move the pot back, servo moves back when it hits the specified range.
francism4:
Its a TowerPro MG995R. It is connected to the servo port on an Adrafruit motor shield. So to be more clear with what the servo is doing is that it sometimes gets stuck, I guess you could say, in the when the pot is moving and hits that range it does nothing.
I see you are printing out the value of the val variable.
What values are being displayed when the servo is 'stuck'?
Then when you rotate the pot back in the opposite direction, the servo will moving, but now is moving in the opposite direction than the rotation of pot, which I do not want. I want the servo to follow the direction of the pot.
I am confused here. Any time that the pot is within your first range, you move the servo to 55.
When the pot is within the second range, you move the servo to 165.
Is the servo moving to those 2 positions when the pot is in your defined ranges?
I am not sure what you mean when you say 'follow the direction of the pot'
Also with that, the servo will moving in both directions during the duration of a pot rotation, which is also not wanted. Basically, rotate the pot in one direction, servo moves at the specified range. Move the pot back, servo moves back when it hits the specified range.
hmmm... I am still not following you.
50 milliseconds is nowhere near enough time for an MG995R to move 110 degrees. Just for a test try changing your delay to about 500 and see if that makes any difference.
Steve