#include <Servo.h>
Servo servoX;
Servo servoY;
int posX = 0;
int posY = 0;
int incrXpos = 0;
int incrXneg = 0;
int incrYpos = 0;
int incrYneg = 0;
void setup() {
servoY.attach(5);
servoX.attach(6);
pinMode(A0,INPUT);
pinMode(A1,INPUT);
Serial.begin (9600);
}
void loop() {
posX = analogRead(A0);
posY = analogRead(A1);
Serial.println(posX);
Serial.println(posY);
if (posX > 550 ) {incrXpos = 1 ; }
if ((posX <= 550 ) && (posX >= 500)) {incrXneg = 0 ; }
if ((posX <= 550 ) && (posX >= 500)) {incrXpos = 0 ; }
if (posX > 500 ) {incrXneg = 1 ; }
if (posX > 550 ) {incrYpos = 1 ; }
if ((posY <= 550 ) && (posY >= 500)) {incrYneg = 0 ; }
if ((posY <= 550 ) && (posY >= 500)) {incrYpos = 0 ; }
if (posY > 500 ) {incrYneg = 1 ; }
for (posX= 0; posX <= 180; posX += incrXpos) {
servoX.write(posX);
}
for (posX = 180; posX >= 0; posX -= incrXneg) {
servoX.write(posX);
}
for (posY = 0; posY <= 180; posY += incrYpos) {
servoY.write(posY);
}
for (posY = 180; posY >= 0; posY -= incrYneg) {
servoY.write(posY);
}
delay(20);
}
Wow.
If the increment variables are all equal to zero, how can you think will ever exit any for() loop??
int incrXpos = 0;
int incrXneg = 0;
int incrYpos = 0;
int incrYneg = 0;
Welcome @sams08 to the forum.
Your for loops, if indeed they are looping, will execute very rapidly and it would be no surprise to observe no reaction from the servo.
Before the for loops, after the if statements, print the values of incrYPos and the other incr variables to make sure they are plausible. @docdoc seems to think they'll be zero, I'm too hot to think through if that's correct.
In the for loops, temporarily add a small delay to give the servo a chance to react. Like 50 or 100 ms. Adjust to taste.
a7
how can i skip the for loop if the variables are equal to zero
put an if around it..
Test the value of your increment?
A more common method of testing ranges is to use if/else starting at the top
if ( posX > 550 ) {
// above 550
// do whatever
}
else if ( posX > 500 ) {
// between 500 and 550
// do whatever
else {
// below 500
// do whatever
}
if i do that they begin twitching
im trying to let servos move with a joystick but i want them to stay if i let go of the joystick
so this means whenever you push/pull the joystick away from neutral position then the servo shall move but as soon as you let go the joystick the servos shall hold their momentary position?
best regards Stefan
jup
shall the servo move at highest possible speed or slowly or shall the servo-moving-speed dependend on how far you push/pull the joystick?
slowly
int incrXpos = 0;
int incrXneg = 0;
int incrYpos = 0;
int incrYneg = 0;
The reason your in an endless loop your not incrementing. Your incremementing by 0 each time
it shoudnt move if the joystick is in the natural position
duckduckgo is always worth a five minute search
best regards Stefan
Try
posX = analogRead(A0) / 4;
posY = analogRead(A1) / 4;
a simply assigning of analogRead(A0) / 4;
will move the servo back to neutral position as soon as you let go the joystick.
for example servo-horn at 90 degrees
As far as I understand sams08
if joystick is pushed to the left slowly move servo from 90 degree towards 180 degrees
if you let go the joystick at 150 degrees the servo shall stay and keep the angle of 150 degrees
best regards Stefan
that is corect
if i look at my serial monitor i only get 1 update bevore it gets stuck but they move to the position of the joystick
what about
#include <Servo.h>
#define MAX 1024
#define G 50
Servo servoX;
Servo servoY;
int posX = 0;
int posY = 0;
char s [80];
void loop()
{
int dX = G * (analogRead (A0) - MAX / 2) / (MAX / 2);
int dY = G * (analogRead (A1) - MAX / 2) / (MAX / 2);
posX += dX;
posY += dY;
posX = 0 > posX ? 0 : (MAX < posX ? MAX : posX);
posY = 0 > posY ? 0 : (MAX < posY ? MAX : posY);
servoX.write (posX);
servoY.write (posY);
sprintf (s, " X %6d %6d, Y %6d %6d", posX, dX, posY, dY);
Serial.println (s);
delay(50);
}
void setup()
{
Serial.begin (9600);
servoY.attach(5);
servoX.attach(6);
}