I have a problem with the program I wrote.
Whene I move the joystick the servo move's but whene I let it go the servo move's back to his starting
position and that's not what I want.
I want it to stay in that position.
can someone help me? Please?
Thanks in advance.
This is the program.
#include <Servo.h>
const int SERVOX=9;
const int SERVOY=10;
const int POTX=0;
const int POTY=1;
Servo myServox;
Servo myServoy;
int valX=0;
int valY=0;
void setup()
{
myServox.attach(SERVOX);
myServoy.attach(SERVOY);
}
Add a "lock" switch to your system, that you hit when the servos have been positioned?
Or an "enable" switch that only passes servo writes when the switch is pressed?
Add a switch as suggested or maybe time how long the joystick has stayed in the position offset from the centre and when the required time has passed lock the servo in that position. How will the program know that you want to move the servo again ?
Your program is perfect!!
But can you help me write if for 2 servo's please?
I'm bad at programming and make a lot off mistakes and it does not want to work
Thanks in advance
#include <Servo.h>
byte servoPin1 = 6; // servo signal connection
byte servoPin2 = 9;
Servo myServo1;
Servo myServo2;
int servoPos1 = 90;
int servoPos2 = 90;
int servoMax1 = 130;
int servoMax2 = 130;
int servoMin1 = 45;
int servoMin2 = 45;
int servoMove1 = 3; // number of degrees per movement
int servoMove2 = 3;
byte potPin1 = A0; // center pin of joystick or potentiometer connected here
byte potPin2 = A1;
int potValue1 = 0;
int potValue2 = 0;
int potCentre1 = 512; // adjust to suit your joystick
int potCentre2 = 512;
int potDeadRange1 = 50; // movements by this much either side of centre are ignored
int potDeadRange2 = 50;
unsigned long curMillis;
unsigned long readIntervalMillis = 100;
unsigned long lastReadMillis;
void setup() {
Serial.begin(9600);
Serial.println("Starting JoystickServo.ino");
myServo1.attach(servoPin1);
myServo1.write(servoPos1);
myServo2.attach(servoPin2);
myServo2.write(servoPos2);
}
void loop() {
void readJoystick();
// check the time
if (curMillis - lastReadMillis >= readIntervalMillis);
lastReadMillis += readIntervalMillis;
// read the joystick
potValue1 = analogRead(potPin1);
// figure if a move is required
if (potValue1 > potCentre1 + potDeadRange1) {
servoPos1 += servoMove1;
}
if (potValue1 < potCentre1 - potDeadRange1) {
servoPos1 -= servoMove1;
}
// check that the values are within limits
if (servoPos1 > servoMax1) {
servoPos1 = servoMax1;
}
if (servoPos1 < servoMin1) {
servoPos1 = servoMin1;
}
if (curMillis - lastReadMillis >= readIntervalMillis) {
lastReadMillis += readIntervalMillis;
// read the joystick
potValue2 = analogRead(potPin2);
// figure if a move is required
if (potValue2 > potCentre2 + potDeadRange2) {
servoPos2 += servoMove2;
}
if (potValue2 < potCentre2 - potDeadRange2) {
servoPos2 -= servoMove2;
}
// check that the values are within limits
if (servoPos2 > servoMax2) {
servoPos2 = servoMax2;
}
if (servoPos2 < servoMin2) {
servoPos2 = servoMin2;
}
}
}
void moveServo() {
myServo1.write(servoPos1);
myServo2.write(servoPos2);
}
void showPosition() {
Serial.print("PotValue1 ");
Serial.print(potValue1);
Serial.print(" ServoPos1 ");
Serial.println(servoPos1);
Serial.print("PotValue2 ");
Serial.print(potValue2);
Serial.print(" ServoPos2 ");
Serial.println(servoPos2);
}