arduino and Joystick

Hello.

I am having some trouble.

I am working with a Joystick (Keyes_SJoys) and arduino UNO. everything works fine but I need to leave the servo in a fixed position, but with the joystick it always come to the begining position.

i dont now if you understand my problem.

this is the code I am using.

#include <Servo.h> 
 
const int servo1 = 6;       // first servo
const int servo2 = 9;       // second servo
const int joyH = 1;        // Joystick Horizontal Axis
const int joyV = 2;        // Joystick vertical Axis
 
int servoVal;           // variable to read the value from the analog pin
 
Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo
 
 
 
void setup() {
 
  // Servo
  myservo1.attach(servo1);  // attaches the servo
  myservo2.attach(servo2);  // attaches the servo
 
  // Inizialize Serial
  Serial.begin(9600);
}
 
 
void loop(){
 
    // Display Joystick values using the serial monitor
    outputJoystick();
 
    // Read the horizontal joystick value  (value between 0 and 1023)
    servoVal = analogRead(joyH);
    servoVal = map(servoVal, 0, 1023, 0, 180);     // scale it to use it with the servo (result  between 0 and 180)
 
    myservo2.write(servoVal);                    	  	 // sets the servo position according to the scaled value
 
    // Read the horizontal joystick value  (value between 0 and 1023)
    servoVal = analogRead(joyV);
    servoVal = map(servoVal, 0, 1023, 0, 180);     // scale it to use it with the servo (result between 70 and 180)
 
    myservo1.write(servoVal);                      	    // sets the servo position according to the scaled value
 
    delay(15);                                  		   // waits for the servo to get there
 
}
 
 
/**
* Display joystick values
*/
void outputJoystick(){
 
    Serial.print(analogRead(joyH));
    Serial.print ("---");
    Serial.print(analogRead(joyV));
    Serial.println ("----------------");
    }

Sorry for my bad/poor english. is not my native lenguage.

THANKS! :slight_smile:

but with the joystick it always come to the begining position.

You mean when the joystick is released and goes back to the centre?

This has been discussed before- maybe try a search- and I can't remember the solution, if there was one. Perhaps what you could do is use the joystick button- or another button- and have the logic only move the servos while the button is pressed. Then release the button at the target position so when the joystick centres, the servos don't move.

Trouble with that is, that when you press the button again for next time, the servos will fly to 0 0 .... that will need some thinking.

But as I say, have a search: there might have been a solution before.

JimboZA:

but with the joystick it always come to the begining position.

You mean when the joystick is released and goes back to the centre?

Correct.

I am searching now with not too many luck.

I like the idea of the button, I am going to try it.

thanks.

Your code looks like it would cause the 2 servos to follow the X and Y deflections of the joystick. If you want to move the joystick, have the servos move to the X/Y positions and stay there when the joystick is moved again then you will need something to indicate that the servos should remain where they are. This could be a button or switch, if you have one, or the fact that the joystick has moved further from it home (central) position and that the servos should follow but only ever move in one direction away from home.

Perhaps you could explain a little more what it is you want the relationship between joystick and servo movement to be.

I want to make the same of this video.

http://www.youtube.com/watch?v=WnKNEcDZwfM

When the joystick it is released it come to the begining position. but when the putton is pressed once (second 16 of the video) the joystick moves free.

Is it just me, or is there an un-resistored LED in that video?

JimboZA: Is it just me, or is there an un-resistored LED in that video?

arduino has a resistor integrated on pin 13 so it dont need to be resistored.

I want to make the same of this video.

So, do you have a pushbutton ?

UKHeliBob:

I want to make the same of this video.

So, do you have a pushbutton ?

yes. jostick has an integrated button, and i have some spare buttons

Robo_proyect:

JimboZA: Is it just me, or is there an un-resistored LED in that video?

arduino has a resistor integrated on pin 13 so it dont need to be resistored.

Um yes but it's surely in series with the built-in led, not that big one sticking up? That resistor is across 5v even if it's in pin 13 and I don't see a resistor....

JimboZA: Um yes but it's surely in series with the built-in led, not that big one sticking up? That resistor is across 5v even if it's in pin 13 and I don't see a resistor....

That LED will be in parallel with the resistor and the built-in LED, so yes, it's directly from pin 13 to ground. Of course it is possible that there is a built-in current limiting chip in that LED itself, but otherwise, yes, it'll be drawing too much current.

But we digress....

Below is some basic toggle code which might be useful. You might add a flag set by the toggling which could then be used in new if/else for switching between your current code and new code for the non joystick following pan/tilt.

//zoomkat LED button toggle test 11-08-2012

int button = 5; //button pin, connect to ground as button
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Look closer at that video and you’ll realize what’s happening after the button is pushed. Rather than simply having the servo position mirror the joystick position, the servo’s position will be stored in a value and the delta of that value (how much to change it) will be computed from the joystick.

In psuedocode:

servo_position = 90;

loop
{
  int joystick_delta = analogRead(joystick) - joystick_center_value;
  int servo_delta = map(joystick_delta, 0, 512, 0, max_servo_delta);
  servo_position += servo_delta;
  writeServoPosition(servo_position);
  delay(some_milliseconds);
}

It appears that if the delta change in joystick position is greater than a certain value, a new position is not written to the servo. this would the joystick rapid movement when released to not write a new value to the servo. I would also expect some dead band around the neutral joystick position in this code section where no new servo positions are written.

zoomkat: It appears that if the delta change in joystick position is greater than a certain value, a new position is not written to the servo. this would the joystick rapid movement when released to not write a new value to the servo. I would also expect some dead band around the neutral joystick position in this code section where no new servo positions are written.

Most servos inherently have a limited range of motion, it has nothing to do with the delta value. You just can't let the servo_position value get less than 0 or greater than 180 (or whatever the range is).

zoomkat: Below is some basic toggle code which might be useful. You might add a flag set by the toggling which could then be used in new if/else for switching between your current code and new code for the non joystick following pan/tilt.

//zoomkat LED button toggle test 11-08-2012

int button = 5; //button pin, connect to ground as button int press = 0; boolean toggle = true;

void setup() {  pinMode(13, OUTPUT); //LED on pin 13  pinMode(button, INPUT); //arduino monitor pin state  digitalWrite(5, HIGH); //enable pullups to make pin 5 high }

void loop() {  press = digitalRead(button);  if (press == LOW)  {    if(toggle)    {      digitalWrite(13, HIGH);   // set the LED on      toggle = !toggle;    }    else    {      digitalWrite(13, LOW);    // set the LED off      toggle = !toggle;    }  }  delay(500);  //delay for debounce }

Thanks, I think it can be usefull, works fine.

...

I am going to try this one now. Thanks

Jiggy-Ninja: Look closer at that video and you'll realize what's happening after the button is pushed. Rather than simply having the servo position mirror the joystick position, the servo's position will be stored in a value and the delta of that value (how much to change it) will be computed from the joystick.

In psuedocode:

servo_position = 90;

loop {  int joystick_delta = analogRead(joystick) - joystick_center_value;  int servo_delta = map(joystick_delta, 0, 512, 0, max_servo_delta);  servo_position += servo_delta;  writeServoPosition(servo_position);  delay(some_milliseconds); }

Jiggy-Ninja:

zoomkat: It appears that if the delta change in joystick position is greater than a certain value, a new position is not written to the servo. this would the joystick rapid movement when released to not write a new value to the servo. I would also expect some dead band around the neutral joystick position in this code section where no new servo positions are written.

Most servos inherently have a limited range of motion, it has nothing to do with the delta value. You just can't let the servo_position value get less than 0 or greater than 180 (or whatever the range is).

The delta I'm talking about is a delta change in the analog pot value and not the servo position.

zoomkat:

Jiggy-Ninja:

zoomkat: It appears that if the delta change in joystick position is greater than a certain value, a new position is not written to the servo. this would the joystick rapid movement when released to not write a new value to the servo. I would also expect some dead band around the neutral joystick position in this code section where no new servo positions are written.

Most servos inherently have a limited range of motion, it has nothing to do with the delta value. You just can't let the servo_position value get less than 0 or greater than 180 (or whatever the range is).

The delta I'm talking about is a delta change in the analog pot value and not the servo position.

I see what you're saying, and its not necessary. When the joystick is released, it returns to the center position where there will be no delta value, and no change in the servo position. There's no need to computer the joystick speed, only its position.

It is possible there's a deadband though, but we can't be sure without inspecting the guy's code.

Below is a link to servo code that uses millies instead of a ms delay. Bottom is some test code with a dead band I made to test using a joystick with an Ethernet shield.

http://www.bajdi.com/controlling-a-servo-without-the-delay-function/

//zoomkat dual pot/servo test 12-29-12
//view output using the serial monitor

#include <Servo.h> 
Servo myservoS1;
Servo myservoS2;

int potpinS1 = 0;  //analog input pin A0
int potpinS2 = 1;

int newvalS1, oldvalS1;
int newvalS2, oldvalS2;

void setup() 
{
  Serial.begin(9600);  
  myservoS1.attach(2);  
  myservoS2.attach(3);
  Serial.println("testing dual pot servo");  
}

void loop() 
{ 
  newvalS1 = analogRead(potpinS1);           
  newvalS1 = map(newvalS1, 0, 1023, 0, 179); 
  if (newvalS1 < (oldvalS1-2) || newvalS1 > (oldvalS1+2)){  
    myservoS1.write(newvalS1);
    Serial.print("1- ");
    Serial.println(newvalS1);
    oldvalS1=newvalS1;
  }

  newvalS2 = analogRead(potpinS2);
  newvalS2 = map(newvalS2, 0, 1023, 0, 179);
  if (newvalS2 < (oldvalS2-2) || newvalS2 > (oldvalS2+2)){  
    myservoS2.write(newvalS2);
    Serial.print("2- ");    
    Serial.println(newvalS2);
    oldvalS2=newvalS2;
  }
  delay(50); //slow down looping to better read serial monitor 
}