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()
{
myservo.attach(9); // attaches the servo on pin 9 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, 179); // 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
delay(15); // waits for the servo to get there
}
what I want to be able to do is add another pot input that will variate the 0 position of the servo between 0 to 10 degrees I have tried a few things but haven't managed to get anywhere any help would be appreciated ?
I made the below for testing more than one pot/servo using the serial monitor to check the output. In your setup you would probably map the servo 0, 10 instead of 0, 179 for the second pot/servo.
//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor
#include <Servo.h>
Servo myservo1; //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
int potpin1 = 0; //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;
int newval1, oldval1; //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;
void setup()
{
Serial.begin(9600);
myservo1.attach(2);
myservo2.attach(3);
myservo2.attach(4);
myservo2.attach(5);
myservo2.attach(6);
Serial.println("testing multi pot servo");
}
void loop()
{
newval1 = analogRead(potpin1);
newval1 = map(newval1, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band
myservo1.write(newval1); //position the servo
Serial.print("1- ");
Serial.println(newval1); //print the new value for testing
oldval1=newval1; //set the current old value
}
newval2 = analogRead(potpin2);
newval2 = map(newval2, 0, 1023, 0, 179);
if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){
myservo2.write(newval2);
Serial.print("2- ");
Serial.println(newval2);
oldval2=newval2;
}
newval3 = analogRead(potpin3);
newval3 = map(newval3, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){
myservo1.write(newval3);
Serial.print("3- ");
Serial.println(newval3);
oldval3=newval3;
}
newval4 = analogRead(potpin4);
newval4 = map(newval4, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){
myservo1.write(newval4);
Serial.print("4- ");
Serial.println(newval4);
oldval4=newval4;
}
newval5 = analogRead(potpin5);
newval5 = map(newval5, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval5 > (oldval5+2)){
myservo1.write(newval5);
Serial.print("5- ");
Serial.println(newval5);
oldval5=newval5;
}
delay(50); //to slow loop for testing
}
going back to the arduino example code, it does not seem to use the full range of the pot i'm only using about 25% of the pot to move the servo it full travel and the other 75% of the pot does nave all, I have dun a serial read of the analog input from the pot and it uses all 1024 bit's and looks smooth. I have also tried another pot and another servo and tried another power supply all making no effect
any ideas how to over come this?
some servos can work outside the 0-180 range, which is in reality a pulse range (1000-2000 uS iirc). when you create your servo, you can redefine max and min pulse length. push these limits out further till you find the actual limits of your phsyical servo, then put a saftey margin back in to protect it. If that was confusing I suggest you look online at servo pulse and control signals, since I am bad at explaining this kind of stuff.
the great thing about standards, is everyone can ignore them.
some servos can work outside the 0-180 range, which is in reality a pulse range (1000-2000 uS iirc). when you create your servo, you can redefine max and min pulse length. push these limits out further till you find the actual limits of your phsyical servo, then put a saftey margin back in to protect it. If that was confusing I suggest you look online at servo pulse and control signals, since I am bad at explaining this kind of stuff.
the great thing about standards, is everyone can ignore them
so are you on about playing with the 0-180 in the code or (and) the Pulse frequency if so how do I alter that?
I got the below pots for tinkering and they seem to work well with the arduino and the price and service is good.
no, you don't need to edit the library, check here
OH SORRY, I misunderstood your problem. ignore me, although the above is nice info to understand if you don't already. in my code I use servo pulses for a bunch of stuff, but no longer use degrees and use pulse length for everything.
you have used the serial monitor to check the values you get from analog read is my understanding, yes? knob sweep is fairly linear or smooth and goes from 0-1023?
you have used the serial monitor to check the values you get from analog read is my understanding, yes?
yes on the 10k pot 0-1023 nice and smooth but not linea
on the 5k pot it go's 0-740 so I edited the 1023 to 740 what work fine but I still had the dead band so I played with the 0-180 and took it down the 0-150 and now seems to work over the full range
as you can see I have added calibrate function what works fine, but when the arduino is powered down it forgets it range so can I save the values to the arduino, I presume we have to use the eeprom? how do we do that?
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int potpin2 = 1; // analog pin used to connect the potentiometer
int val2; // variable to read the value from the analog pin
int buttonPin = 11; //caliberate button
int sensorValue = 0; // the sensor value
int sensorMin = 740; // minimum sensor value
int sensorMax = 0; // maximum sensor value
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(buttonPin, INPUT);
if (digitalRead(buttonPin)== HIGH)
{ // ** ...cailable... ** //
digitalWrite(12, HIGH);
// calibrate during the first five seconds
while (millis() < 5000) {
sensorValue = analogRead(potpin);
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
digitalWrite(12, LOW);
}
}
void loop()
{
sensorValue = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val2 = analogRead(potpin2); // reads the value of the potentiometer (value between 0 and 1023)
// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 740);
// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 740);
val2 = map(val2, 0, 1023, 0, 20); // scale from 0 to 10 for the zero for the servo
sensorValue = map(sensorValue, 0, 740, val2, 150); // scale it to use it with the servo (value between 0 and 180)
myservo.write(sensorValue); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}