im a newby please help.
i have a a model excavator built.
i have three servos for the arm controls
how do i keep the servo from coming back to center
im a newby please help.
i have a a model excavator built.
i have three servos for the arm controls
how do i keep the servo from coming back to center
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo1;
Servo myservo2;
int potpin = 0; // analog pin used to connect the potentiometer
int potpin1 = 1;
int potpin2 = 2;
int val; // variable to read the value from the analog pin
int val1;
int val2;
int pos = 0; // i know this go somewhere
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo1.attach(10);
}
void loop()
{
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(10);
{
val1 = analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023)
val1 = map(val1, 0, 1023, 0,150);// scale it to use it with the servo (value between 0 and 180)
myservo1.write(val1); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
}
}
You don't describe the circumstances in which the servo goes back to centre when that is not wanted.
I think it would be better if you separate your code into separate functions like this to make it easier to maintain
void loop() {
readPotentiometers();
moveServos();
}
void readPotentiometers() {
// read the Pots and save the postion values in global variables
}
moveServos() {
// move each servo depending on the saved position
}
...R
I assume you're referring to what happens when you power up the arduino. When you attach() a servo it will go to its command position. By default, this is 90. You can override this by doing a write() before the attach, but the excavator will still move from wherever it was when you turned it off to your override position.
You can have a shutdown routine that puts the servos back in their home position before you turn it off, but even then, there's no defense against the system being moved by hand while powered off & the arduino will not know.
Basically, since regular hobby servos have no feedback mechanism to tell you where they are, you will often get large jerky movements when you start.
In your case though, you could read your pots and do the servo.write in setup, before the attach. Then at least you wouldn't center and then move to the position set by the pots.
wildbill:
I assume you're referring to what happens when you power up the arduino. When you attach() a servo it will go to its command position. By default, this is 90. You can override this by doing a write() before the attach, but the excavator will still move from wherever it was when you turned it off to your override position.You can have a shutdown routine that puts the servos back in their home position before you turn it off, but even then, there's no defense against the system being moved by hand while powered off & the arduino will not know.
Basically, since regular hobby servos have no feedback mechanism to tell you where they are, you will often get large jerky movements when you start.
In your case though, you could read your pots and do the servo.write in setup, before the attach. Then at least you wouldn't center and then move to the position set by the pots.
thanks guys
im using two analog joysticks that go back to center when i release them. how do i keep the servos from following.
lets say i push the joysick forward: servo follows
i want to be able to let the joystick go: servo remains where it is until the joysick goes in the opposite direction past center
almost like directional control
servopos = 90
if i push forward on stick servo moves > 90 in increments of +1 until i release the stick but i dont want the servo to come back until stick is < 90 then -1 until i release the stick
want value on stick to be used for servo speed and direction rather than digital proportional control
do i need continuous rotation servo or steppers
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree [b]when i push the stick forward then stop when i let the stick go[/b]
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Why not make the rate of change of servo position proportional to the distance of the stick from the centre location.
Edit: spelling
AWOL:
Why not make the rate of change of servo position proportional to the distance of the stick from the centre locution.
please show me how
Think about what you want to do.
You'll probably want an unresponsive dead-band either side of the stick rest position, so work through that bit first.
Then, the further from the centre reading your stick reading is, the larger you want your rate of change to be.
This will depend on your mechanics being controlled.
AWOL:
Think about what you want to do.
You'll probably want an unresponsive dead-band either side of the stick rest position, so work through that bit first.
Then, the further from the centre reading your stick reading is, the larger you want your rate of change to be.
This will depend on your mechanics being controlled.
will you please give me a snippet of code to start with
Start by writing a sketch that makes a servo move 2 degrees (say) every time you pull the joystick towards you and then release it and move 2 degrees in the opposite direction every time you push the joystick away from you and release it. Every push (or pull) and release will move it 2 degrees.
The program structure that I suggested earlier will facilitate this approach. Something like this (I have already given you most of it)
byte mainArmAngle = 0;
void setup() {
// all the setup stuff
}
void loop() {
readPotentiometers();
moveServos();
}
void readPotentiometers() {
int mainArm = analogRead(pinForMainArmPot);
if (mainArm < 500) {
mainArmAngle = mainArmAngle + 2;
}
if (mainArm > 520) {
mainArmAngle = mainArmAngle - 2;
}
}
void moveServos() {
mainArmServo.write(mainArmAngle);
}
Doing it this simple way it doesn't matter how far you move the joystick.
When you get that working you can move on to get the joystick to keep adding 2 degree movements as long as it is pulled or pushed.
And later, if you need it, you could get the joystick to cause small 1 deg steps near the centre increasing to larger 5 deg steps when it is pulled to its extremity.
...R
thanks @Robin. let me give this a shot
im using two analog joysticks that go back to center when i release them. how do i keep the servos from following.
Your code is a basic servo follow joystick setup. What you need to develop first is incremental servo movement in two directions based on an event (like pot value > 512 and pot value <512). When the event is encountered, incrementally increase/decrease the current control value being sent to the servo. When that is done, then move to adding to the code a variable delay to the code that is based on the pot position away from the servo neutral value. Should be fairly easy to code.
#include <Servo.h>; //servo library
Servo Mainservo;
int MainPot = A0; //pot pin for main arm
int Main = 0; //place to store value of pot
int LastMain;
int delaytime;
void setup(){
Mainservo.attach(9);
Serial.begin(9600);
}
void loop(){
SPEED();
int Main = analogRead(MainPot);
if (Main < 500) {
DOWN();
}
else if (Main > 525) {
UP();
}
Serial.print(" ");
Serial.println(LastMain);
Mainservo.write(LastMain);
}
void SPEED() {
int servospeed = analogRead(MainPot);
servospeed = map(servospeed,0,1023,-70,70);
servospeed = abs(servospeed);
servospeed = map(servospeed,70,0,1,100);
delaytime = servospeed;
Serial.print(servospeed);
}
void DOWN() {
if (LastMain > 30) {
LastMain = LastMain - 1;
delay(delaytime);
}
}
void UP() {
if (LastMain < 145) {
LastMain = LastMain + 1;
delay(delaytime);
}
}
here what i came up with
You seem to have sent me a PM which I don't understand. It just had a link to here.
The code you came up with is nothing like what I suggested. But my way is not the only way.
Is your way working properly now?
...R
Robin2:
You seem to have sent me a PM which I don't understand. It just had a link to here.The code you came up with is nothing like what I suggested. But my way is not the only way.
Is your way working properly now?
...R
yes. i have direction and speed control of the servos
i modified your code because the servo wouldnt "rest"
this is the first project ive taken on and i think im addicted
I suspect you have got tripped up by the scope of your variables.
At the start of your sketch you have "int Main = 0;"
And inside loop() you also have "int Main = analogRead(MainPot);"
This creates another variable also called Main into which the analog value is placed. Because the variable is created within loop() it is not accessible by code outside loop(). The code within loop() should be
"Main = analogRead(MainPot);"
if you want to use the global variable.
And if it was your intention to use a local variable within loop() it is not good practice to use the same name as another global variable - it just causes confusion.
...R
Right. "Main" will be a global function. i was trying to get the program to work for the "Main" servo. now that i have succeeded, with your help, i will call these programs for all the servos with a little renaming of variables
thanks
techmodek:
"Main" will be a globalfunctionvariable.
...R
Robin2:
techmodek:
"Main" will be a globalfunctionvariable....R
Right!!! Thanks
By the way "main" is potentially a confusing choice for a variable name in a C/C++ program because all such programs have a function called main(). The Arduino IDE hides this from you, but it is there in the background and people who are familiar with C will be aware of that and may be confused.
...R