Get Servo to Not Return to Center

I'd like to change my current code to keep the servos in their final orientation when the joystick is pressed instead of returning to the center. Any suggestions? Thank you.

#include <Servo.h>
 
Servo servo1;
Servo servo2;

int xaxis = 0;
int yaxis = 1;

int val;

void setup()
{
servo1.attach(3);
servo2.attach(5);
}

void loop()
{
val= analogRead(xaxis);
val = map(val, 0, 1023, 60, 120);
servo1.write(val);

val= analogRead(yaxis);
val = map(val, 0, 1023, 60, 120);
servo2.write(val);

}

The best method is to use a joystick without a spring return or just use a pot and knob to set the position instead or even “up/down” buttons.
It will be hard to detect that “ final position “ - how does it know the central position is not where you want it ?
If you go for it ...
You need a method of determining how final position is determined ( time window at a given position ? Push button ?)
You also need to guard against trying to over drive the servo position by moving the stick repeatedly back and forth - not an easy problem!

Why do you want to do this ? There may be other solutions

Have a look at the code in this link which is designed to do what you want.

...R

That’s cool Robin - think I had interpreted his question differently , thinking he wanted to say move the stick to say 50% forward and the servo go to that position and stay put when he let go , which after reading your post , is hopefully wrong .

hammy:
thinking he wanted to say move the stick to say 50% forward and the servo go to that position and stay put when he let go , which after reading your post , is hopefully wrong .

Until we hear from the OP I would not be prepared to bet that you are wrong :slight_smile:

...R

Thank you both for your responses. Robin2 - that is exactly what I meant; I'm sorry that I wasn't more clearer with what I wanted. That code works for my joystick and servo.

I'm very new with Arduino but was assigned with doing the coding for my senior group design project, so I'm slightly freaking out. This code has eased my worries a bit; thank you very much.

In order to allow this code to control two servos with one joystick, do I need to add another set of variables (curMillis, readIntervalMillis, & lastReadMillis)?

amencurley:
In order to allow this code to control two servos with one joystick, do I need to add another set of variables (curMillis, readIntervalMillis, & lastReadMillis)?

Those variables only control the timing of reading so I don't think they need to be duplicated.

If you need more help please post the code you have tried.

...R