element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum In need of assistance please
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 8 replies
  • Answers 2 answers
  • Subscribers 409 subscribers
  • Views 839 views
  • Users 0 members are here
  • x
  • joysticks
  • y
  • 2
  • servos
  • and
  • axis
  • arduino
  • 4
  • z
Related

In need of assistance please

Former Member
Former Member over 12 years ago

Hello - Tony here, and I am a totally new person to the whole arduino world.

First my project, my equipment, why I decided to use arduino, what I am attemting to do, a Fritzing diagram, the code I am using, and a Fritzing diagram (jpg format).

 

My project is relatively simple; the ability to pan and tilt 2 cameras that weight .06 ounce.  The cameras are being used to record the weather.  The programming below works, but the problem I am having is that the servos return to the zero (or do we call it the 90° location) when I release the joystick.  The joysticks I am using are the same type used in the Playstation 2 game pads.  They allow for a Z axis which for me, I would like to use as a way to keep a camera pointed in the direction I last positioned it to, until the time I wish to pan or tilt it to a different axis.  I hope I explained it clearly.  Lastly I do not want to use wireless, I wish to have it hard wired as I believe it will cause less interference by other radio frequencies in my area.

 

My equipment or components are as follows:

     Arduino UNO board

     Bread board

     Circuit board

     2 Joysticks (Playstation 2 style with X, Y and Z axis)

     4 TowerPro servo motors SG5010

 

I decided to use arduino, because it was recommended to me by computer / electronic shop personnel.  I have no prior knowledge of coding arduino or any other type of programming.  I was however able to take coding for 1 joystick and 2 servos, and expand it to include an additional joystick and 2 additional servos.  So as stated, I have coding for 4 servos and 2 joysticks, I just cannot and do not understand what I need to do in order to get the Z axis enabled to do what I stated above.

I have included the Fritzing diagram in jpg format so that it may be easier to understand what I have going on.  I apologize to those that are more experienced, that I bring this here, but I really am in need of assistance.

 

The coding:

 

#include <Servo.h>

 

const int servo1 = 3;        // first servo

const int servo2 = 11;      // second servo

const int servo3 = 6;        // third servo

const int servo4 = 10;     // fourth servo

const int joyH = 3;           // L/R Parallax Thumbstick

const int joyV = 4;           // U/D Parallax Thumbstick

const int joyI = 1;             // L/R Parallax Thumbstick

const int joyW = 2;          // U/D Parallax Thumbstick

 

 

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

Servo myservo3;  // create servo object to control a servo

Servo myservo4;  // create servo object to control a servo

 

 

 

 

void setup() {

 

   // Servo 

   myservo1.attach(servo1);  // attaches the servo

   myservo2.attach(servo2);  // attaches the servo

   myservo3.attach(servo3);  // attaches the servo

   myservo4.attach(servo4);  // 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, 179);      // scale it to use it with the servo (result  between 0 and 179 to keep from being jittery)

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, 179);      // scale it to use it with the servo (result  between 0 and 179 to keep from being jittery)

     myservo4.write(servoVal);                           // sets the servo position according to the scaled value

 

     // Read the horizontal joystick value  (value between 0 and 1023)

     servoVal = analogRead(joyI);          

     servoVal = map(servoVal, 0, 1023, 0, 179);      // scale it to use it with the servo (result  between 0 and 179 to keep from being jittery)

     myservo1.write(servoVal);                           // sets the servo position according to the scaled value

 

     // Read the horizontal joystick value  (value between 0 and 1023)

     servoVal = analogRead(joyW);          

     servoVal = map(servoVal, 0, 1023, 0, 179);      // scale it to use it with the servo (result  between 0 and 179 to keep from being jittery)

     myservo3.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 ("----------------");

     Serial.print(analogRead(joyI));

     Serial.print ("---");

     Serial.print(analogRead(joyW));

     Serial.println ("----------------");

 

}

 

 

Thanks for taking the time to help me.  I do appreciate it.

 

Tony

Attachments:
image
  • Sign in to reply
  • Cancel

Top Replies

  • cookieglitch
    cookieglitch over 12 years ago +2 suggested
    If I'm reading this correctly, the problem you are having is that the servos jump back to zero (or 90 or the start position or the default position, whatever you wish to call it.) when you release the…
  • mcb1
    mcb1 over 12 years ago in reply to Former Member +1
    Tony Randazzo wrote: 41 views? and not one person has responded? Tony Your title didn't really give many clues as to the nature or complexity of your problem. There are a very large number of members,…
  • Former Member
    Former Member over 12 years ago in reply to billabott +1
    Hi Bill sorry it took so long to respond back, have been away from the computer. I appreciate your input and have taken your advise on naming the parts Tony P.S. the aka 44 too funny
Parents
  • cookieglitch
    0 cookieglitch over 12 years ago

    If I'm reading this correctly, the problem you are having is that the servos jump back to zero (or 90 or the start position or the default position, whatever you wish to call it.) when you release the joystick. This is a fairly simple problem in the code and rather easy to fix. With any problem like this, let's make it as simple as possible and work from there (Fewer parts can after all be a bit less of a headache). We can start out with one axis and one servo.

     

    Currently in your program, whenever you read from the joystick, you are moving the servo. To avoid the problem you are having, you need a way to remember where the servo should be, check if you do need to move (i.e. not at the start position on the joystick) and handle the change from there. To do this, you will need a variable to hold the current position, and another to hold the new reading from the joystick. You can then check the conditions of these, such as if they match, using an if statement.

     

    One way to make the solution simpler while you start out is to interpret zero as "do not move" rather than a position and to instead use a button (such as depressing the joystick) to mean "return to start position". This may require additional buttons as you start introducing the other axis, but that is a concern for later. To some extent, you need to understand how the joysticks work. Inside the stick is a potentiometer, setup in a way that means the start position (zero) is actually a value such as 5k ohms (This will vary depending on the part, refer to the datasheet or measure it with your multimeter). When this is read by your arduino, it will be around 512 (Extreme left being zero, extreme right being 1024, you will need to check those). One way your program could work is to look at the value, if it is not "zero" and is different to the current value, move to it. If it is "zero", or the same as the current value, do nothing. This would look something like the code below. This is by no means the best solution, and may not work exactly as it should, it may however give you an idea of what you are aiming for. This example will produce a lot of jitter when you run it and the reset button requires debouncing for it to work correctly.

     

    #include <Servo.h>
    
    const int cam1panPin = 3; //Cam 1, Pan servo pin
    
    const int joyH = 3;      // L/R Parallax Thumbstick Pin
    
    Servo cam1pan;           // create servo object to control a servo
    
    int c1Pan_OldVal = 512;  //Old value for comparison
    
    int c1Pan_Pos = 512;     //Current position
    
    int resetButtonPin = 5; 
    
    void setup()
    {
      // Servo 
      cam1pan.attach(cam1panPin);  // attaches the servo
      
      // initialize the pushbutton pin as an input:
      pinMode(resetButtonPin, INPUT);   
    
      // Inizialize Serial
      Serial.begin(9600);
    }
    
    void loop()
    {
      // Display Joystick values using the serial monitor
      outputJoystick();
    
      int c1Pan_temp = analogRead(joyH);
      
      if(c1Pan_temp == 512) //If in start position. May need to be a range of values
      {
        //Do nothing
      }
      else if(c1Pan_temp != c1Pan_OldVal)
      {
        //If different to old value, move
        c1Pan_Pos = map(c1Pan_temp, 0, 1023, 0, 179);
        cam1pan.write(c1Pan_Pos);
        
        //Save current reading as old
        c1Pan_OldVal = c1Pan_temp;
      }
      
      //Read reset button
      //Requires debouncing
      int buttonState = 0;
      
      //If button is pressed, go back to middle
      buttonState = digitalRead(resetButtonPin);
      if(buttonState = HIGH)
      {
        c1Pan_Pos = map(512, 0, 1023, 0, 179);
        cam1pan.write(c1Pan_Pos);
        
        //Save current reading as old
        c1Pan_OldVal = 512;
      }
      else
      {
        //Do nothing
      }
    
      delay(15);                                       // waits for the servo to get there
    }
    
    /**
     * 
     * Display joystick values
     * 
     */
    
    void outputJoystick()
    {
      Serial.print(analogRead(joyH));
      Serial.print ("---");
    }

     

    The short (and probably much less confusing) version of this is that you need to look into using if statements, and use them to make sure the position only changes when you want it to. With any luck this should help you solve your problem!

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Reply
  • cookieglitch
    0 cookieglitch over 12 years ago

    If I'm reading this correctly, the problem you are having is that the servos jump back to zero (or 90 or the start position or the default position, whatever you wish to call it.) when you release the joystick. This is a fairly simple problem in the code and rather easy to fix. With any problem like this, let's make it as simple as possible and work from there (Fewer parts can after all be a bit less of a headache). We can start out with one axis and one servo.

     

    Currently in your program, whenever you read from the joystick, you are moving the servo. To avoid the problem you are having, you need a way to remember where the servo should be, check if you do need to move (i.e. not at the start position on the joystick) and handle the change from there. To do this, you will need a variable to hold the current position, and another to hold the new reading from the joystick. You can then check the conditions of these, such as if they match, using an if statement.

     

    One way to make the solution simpler while you start out is to interpret zero as "do not move" rather than a position and to instead use a button (such as depressing the joystick) to mean "return to start position". This may require additional buttons as you start introducing the other axis, but that is a concern for later. To some extent, you need to understand how the joysticks work. Inside the stick is a potentiometer, setup in a way that means the start position (zero) is actually a value such as 5k ohms (This will vary depending on the part, refer to the datasheet or measure it with your multimeter). When this is read by your arduino, it will be around 512 (Extreme left being zero, extreme right being 1024, you will need to check those). One way your program could work is to look at the value, if it is not "zero" and is different to the current value, move to it. If it is "zero", or the same as the current value, do nothing. This would look something like the code below. This is by no means the best solution, and may not work exactly as it should, it may however give you an idea of what you are aiming for. This example will produce a lot of jitter when you run it and the reset button requires debouncing for it to work correctly.

     

    #include <Servo.h>
    
    const int cam1panPin = 3; //Cam 1, Pan servo pin
    
    const int joyH = 3;      // L/R Parallax Thumbstick Pin
    
    Servo cam1pan;           // create servo object to control a servo
    
    int c1Pan_OldVal = 512;  //Old value for comparison
    
    int c1Pan_Pos = 512;     //Current position
    
    int resetButtonPin = 5; 
    
    void setup()
    {
      // Servo 
      cam1pan.attach(cam1panPin);  // attaches the servo
      
      // initialize the pushbutton pin as an input:
      pinMode(resetButtonPin, INPUT);   
    
      // Inizialize Serial
      Serial.begin(9600);
    }
    
    void loop()
    {
      // Display Joystick values using the serial monitor
      outputJoystick();
    
      int c1Pan_temp = analogRead(joyH);
      
      if(c1Pan_temp == 512) //If in start position. May need to be a range of values
      {
        //Do nothing
      }
      else if(c1Pan_temp != c1Pan_OldVal)
      {
        //If different to old value, move
        c1Pan_Pos = map(c1Pan_temp, 0, 1023, 0, 179);
        cam1pan.write(c1Pan_Pos);
        
        //Save current reading as old
        c1Pan_OldVal = c1Pan_temp;
      }
      
      //Read reset button
      //Requires debouncing
      int buttonState = 0;
      
      //If button is pressed, go back to middle
      buttonState = digitalRead(resetButtonPin);
      if(buttonState = HIGH)
      {
        c1Pan_Pos = map(512, 0, 1023, 0, 179);
        cam1pan.write(c1Pan_Pos);
        
        //Save current reading as old
        c1Pan_OldVal = 512;
      }
      else
      {
        //Do nothing
      }
    
      delay(15);                                       // waits for the servo to get there
    }
    
    /**
     * 
     * Display joystick values
     * 
     */
    
    void outputJoystick()
    {
      Serial.print(analogRead(joyH));
      Serial.print ("---");
    }

     

    The short (and probably much less confusing) version of this is that you need to look into using if statements, and use them to make sure the position only changes when you want it to. With any luck this should help you solve your problem!

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube