1

Topic: Using joystick to control servo motor

Hello guys!

I am stuck into a problem. I want to use a virtual joystick (obviously from RemoteXY) to control the directional angular motion/movement of my servo.

I've tried coding multiple times to get my desired job done. But, every time something is left insignificant or wrong. Therefore I cannot control it through the joystick, how I actually want.

I want to control my servo as:- 1). It should rotate clockwise when I swipe to the positive x-axis of Joystick (exact same for
                                                   positive y-axis) and
                                              2). It should rotate anticlockwise when I swipe towards the negative x-axis of the joystick. (exact
                                                   same for negative y-axis)

I've attached the servo to Pin No. 13 (All other pins except Analogue ones are already reserved).


The Raw Code of my project (without any interference of mine) is below, please check and improve the same, Please smile :-


/* 
   -- New project -- 
    
   This source code of graphical user interface  
   has been generated automatically by RemoteXY editor. 
   To compile this code using RemoteXY library 2.3.3 or later version  
   download by link http://remotexy.com/en/library/ 
   To connect using RemoteXY mobile app by link http://remotexy.com/en/download/                    
     - for ANDROID 4.1.1 or later version; 
     - for iOS 1.2.1 or later version; 
     
   This source code is free software; you can redistribute it and/or 
   modify it under the terms of the GNU Lesser General Public 
   License as published by the Free Software Foundation; either 
   version 2.1 of the License, or (at your option) any later version.     
*/ 

////////////////////////////////////////////// 
//        RemoteXY include library          // 
////////////////////////////////////////////// 

// RemoteXY select connection mode and include library  
#define REMOTEXY_MODE__ESP8266_HARDSERIAL_POINT

#include <RemoteXY.h> 

// Include the Servo library 

#include <Servo.h> 

// Declare the Servo pin 

int servoPin = 13; 

// Create a servo object 

Servo Servo1; 

// RemoteXY connection settings  
#define REMOTEXY_SERIAL Serial 
#define REMOTEXY_SERIAL_SPEED 115200 
#define REMOTEXY_WIFI_SSID "Servo control " 
#define REMOTEXY_WIFI_PASSWORD "" 
#define REMOTEXY_SERVER_PORT 6377 


// RemoteXY configurate   
#pragma pack(push, 1) 
uint8_t RemoteXY_CONF[] = 
  { 255,2,0,0,0,12,0,8,13,0,
  5,19,29,7,46,46,2,26,31 }; 
   
// this structure defines all the variables of your control interface  
struct { 

    // input variable
  int8_t joystick_1_x; // =-100..100 x-coordinate joystick position 
  int8_t joystick_1_y; // =-100..100 y-coordinate joystick position 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY; 
#pragma pack(pop) 

///////////////////////////////////////////// 
//           END RemoteXY include          // 
///////////////////////////////////////////// 



void setup()  
{ 
  RemoteXY_Init ();  
   
  // We need to attach the servo to the used pin number 

   Servo1.attach(servoPin);
   
  // TODO you setup code 
   
} 

void loop()  
{  
  RemoteXY_Handler (); 
   
  // I've erased all my garbage from here. So, that you could concentrate easily :D. Thank You So much for taking interest. 
   
  // TODO you loop code 
  // use the RemoteXY structure for data transfer 


}

I am a newbie. I love these kind of things and Internet and you people are only teacher of mine in this subject. I learn from you only and I don't have any other means to learn these things. Please please please support and help me. I'll always be grateful to you.
I appreciate your efforts of caring such new children in robotics like me.


Thank You So Much in Advance!!!

Have a nice Day

2

Re: Using joystick to control servo motor

You cannot use pin 13 for a PWM Servo output, it is not a PWM pin.

Only pins 3, 5, 6, 9, 10, and 11 are PWM that can be attached to the Servo library.

Once you have attached a PWM output pin to your servo instance, an analogWrite() of 0 to 255 to the digital pin makes it PWM.

2B, or not 2B, that is the pencil ...

3

Re: Using joystick to control servo motor

Thank You So Much. I'll contact you again as soon as I try this method out.

4

Re: Using joystick to control servo motor

Not clear from your original post, but do you want the servo to move to a new commanded position, or do you want continuous rotation of the servo until the joystick is centered again ??

A "servo" will just move to the new position and stop.

You can make a servo rotate continuously, by removing the feedback potentiometer inside it and replacing it with two resistors, same value, where the potentiometer was.  I prefer to put a 20-turn potentiometer in instead of the two fixed resitors, because then I can trim the servo to stop when I PWM 1500 uS to it.  There will also likely be a physical stop which will also need to be removed.  The speed the servo will go will be dependent upon how far the joystick is moved from centre.

If you only want Left/Right (x-axis), why not just use a horizontal slider ....

2B, or not 2B, that is the pencil ...

5 (edited by naved.the.sheikh 2018-09-02 18:08:40)

Re: Using joystick to control servo motor

Daba wrote:

Not clear from your original post, but do you want the servo to move to a new commanded position, or do you want continuous rotation of the servo until the joystick is centered again ??

A "servo" will just move to the new position and stop.

You can make a servo rotate continuously, by removing the feedback potentiometer inside it and replacing it with two resistors, same value, where the potentiometer was.  I prefer to put a 20-turn potentiometer in instead of the two fixed resitors, because then I can trim the servo to stop when I PWM 1500 uS to it.  There will also likely be a physical stop which will also need to be removed.  The speed the servo will go will be dependent upon how far the joystick is moved from centre.

If you only want Left/Right (x-axis), why not just use a horizontal slider ....

Thank You So Much for your detailed reply.

I wanted it to move clockwise and anticlockwise with respect to one static postion. But, when I set 90° as the initial position of the servo and then change it's value into 0° when Joystick moved left and 180° when Joystick moved right. But, when I use

myservo.write(0)

for it, it just simply doesn't move in any direction.

This is the main problem.

I also tried with horizontal slider. But, again the servo doesn't move at all when I move the slider towards left side and only moves when I slide it towards right.

I used the code for horizontal slider which is given in the examples of the website and I also replaced the pin from 13 to 3 as you suggested.

Actually I am installing a WiFi action camera into my Bluetooth controlled robot. I wanna Install it on the servo, so that whenever o would need to see right or left side of the robot, I could move the camera without touching it.


Thanks in advance for any further help.
Your support is always appreciated by me.

6

Re: Using joystick to control servo motor

If you post ALL of your code, I can take a look at why your servo is not moving, perhaps you've missed an important step...

2B, or not 2B, that is the pencil ...