I am new to the software component of arduino and code...lol
I would like to take a voltage and feed it to the arduino and have a servo change its position according to the voltage feed to it. it will be between 0-5vdc. I would like to know how to set parameters like cutoffs, neutral, etc.
i.e. 2vdc corresponds to 120 degrees on the servo, then if the voltage were to chang to 2.56vdc i want it to be a little more like 130.
ANY HELP WOULD BE AWESOME! :o
I need to know how to write the code and set it all up.
I have looked up sample codes using a pot but am unsure what to replace the pot line with???? :-/ because it will no longer be using a pot...
heres the link to the code
The following code lets you control Servo on pin2 by potentiometer on analog 0
#include <SoftwareServo.h>
SoftwareServo 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(2); // attaches the servo on pin 2 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
SoftwareServo::refresh();
}
im realtively new to software, i understand hardware very well...
Thanks guys for the tips. I do understand that the pot adjusts voltages on the analog in. But like the pot line (code) says, 0, 1023, 0, 179 that is the supposed conversion factor from the 1023 steps in a pot to the 179 in my servo. will it go hand in hand with voltage? for example the #'s between 0vdc and 5vdc are 6, but what happens when it is 0.112 volts and then jumps to 4.938 how will arduino know to calculate the degree of the servo based on those different voltages?
What if the desired position on the servo is not what i want i.e. 1vdc turns the servo to 20 degrees when actually i want it at 140?
Im sorry for asking so many questions ;). I need to be able to tell arduino things like, if voltage is between 3.5v and 3.7v goto servo position 145-150, and so on for higher and lower. thats what i am confused about.
The "map" transfers (maps) the range 0 to 1023 (from the 10 bit ADC) to the range 0 to 179 for degrees, so, say, 511 or half range on the ADC corresponds to 90 degrees output from the "map".
If you want to map other ranges, it's just simple arithmetic to plug into the "map".
Note that the 0..179 is just a convention for servos, and may need calibrating to a particular angular displacement for a particular type or brand of servo.
Some servos can't manage 180 degree displacement.
Thnx groove, i figured that it would just calculate averages between both values.
but what if it is at 90 degrees but instead of me wanting it at 90 degrees at 2.5v i want it at 100, even tho it should be at 90. But i only want that 100 if its at 2.5v, if it were to change to 2.6v i would want it to go back to its originally figured out degree i.e. 93degrees if that were it.
The map function is useful if there is a linear relationship between the input value (the voltage) and the output value (the servo position).
If you want a non-linear relationship, you need to create a look-up table, where you look up the output value (the servo position) based on an index (the voltage).
Since you don't get the voltage directly, you need to define what range of analogRead values correspond to a voltage of interest (2.5V / 5V = 0.5 corresponds to a digitalRead value of 511). Each range of interest corresponds to an index, that is used to look up a servo value.
int servoPos[] = {14, 69, 18, 49, 129, 18, 0, 57, 99, 103};
int index = -1;
int val = analogRead(somePin);
if(val < 100)
index = 0;
else if(val < 180)
index = 2;
else if(val < 300)
index = 3;
// Add the rest of the else ifs...
// Now, look up the servo position for that
// value and position the servo
if(index >= 0)
Servo.write(servoPos[index]);
Though if or why you want non-linear output is not clear.
Any tips on how to read the out pwm to my servo so that i can see whats happening instead of guessing the position of the servo. like a conversion? i.e. output X microsecs = servo position X, that way I could easily know the position out by reading what its feeding the servo...
If not its cool, you have been a great help! everyone has! thank you
thnx groove, so where would i insert that line? Remember i don't know didly about code, also where would i insert the table that was shown in the previous reply, in relation to my suggested code that was given earlier.
thanks a alot, really thank you all, i have been working on my project from about 1 year, and this is the last thing!
Thnx for your response, my range is 0-5vdc in on the analog pin.
Angle 0 for 0v and 179 (or max deflection) for 5 v
The exact positions should be estimated (0-179) depending on the voltage input as stated earlier.
My problem is application of the software, could someone incorporate the previous added code into a complete code? tell me why you did what, i.e. i put } because and i put () because...etc. if you know of a index for arduino code with all the variables, please point me the way
if you know of a index for arduino code with all the variables, please point me the way
Personally, I think you need to take an introductory beginners course in C/C++ programming at your local community college (or whatever the equivalent is where you're at)...
This is what i am doing, I am taking a variable (0-5vdc) voltage from another source that constantly varies, but it only varies up or down depending on certain conditions. Those conditions are not an issue.
The issue im having is the fact that when someone says add this line xxx in there and you will be fine. I have no idea where to put that line. is it after the write function you just talked about? is it before it? I am new to this and hoping someone will help me write the code needed for me to get this work, that basically what i need.
IF the scale from the 0, 1023, 0, 179 won't work very well for what i am doing, I would like to figure out how to specifically tell the servo to move given a < or > action for the given voltage on that analog pin. Just like what the other person posted earlier.
this all im asking, seems like alot to me! But maybe simple for you guys
Your very right. I will begin experimenting with the arduino.
My project and all its functions must be completed by the end of july, above doing this I am a full time student.
The best way for my project is to learn the language, and take my time building the arduino code and everything else. I do not have the time.
But I will be posting back in awhile if I have any issues with the project.
One last question before I start playing with the arduino.
can i change the values 0, 1023 to lets say 0, 500, 0 179 ?
will that decreases the sensitivity to the voltage making it less accurate? Or is that a number of values it can read from?
I was just thinking about that because what is changing my voltage is a diaphram, and the high voltage is around 4 volts and low is only 2 volts...that piece of info about ignoring ranges will help a ton
Hey guys finally got around to testing the arduino, uploaded the servo program and now I am getting
avrdude: stk500_getsync(): not in sync: resp=0x00
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51