Control servo with joystick

sokinsokin Posts: 32
Hello, does any one have any thoughts on scaling a joystick to control a servo? What I would like to do is have the servo start at 100 degrees and move plus or minus 80 degrees depending on what voltage the joystick displays. So as the joysticks voltages go up the servo would start to move up as well, if the voltage were at 5 volts the servo would be at 180 degrees and then when the voltage is at zero volts the servo would be at 20 degrees. I also need it to scale so if I stopped at three volts the servo would stop in that position. I have tried some If statements but that has not worked, anyone have any ideas?

Thanks in advance and here's what I am starting with.

  Simple Propeller P8X32A Propeller C demo with the Parallax 2-axis Joystick   

#include "adcDCpropab.h"                            // Include adcDCpropab
#include "simpletools.h"                            // Include simpletools
#include "servo.h"  

int main()  
                                        // Main function
  pause(1000);                                      // Wait 1 s for Terminal app
  adc_init(21, 20, 19, 18);                         // CS=21, SCL=20, DO=19, DI=18

  float lrV, udV;                                   // Voltage variables
  int ud, lr;
  servo_angle(17, 900);
  while(1)                                          // Loop repeats indefinitely
    udV = adc_volts(2);                             // Check A/D 2                
    lrV = adc_volts(3);                             // Check A/D 3
    ud = 120*(udV-2.5)/5;
    lr = 120*(lrV-2.5)/5;
    putChar(HOME);                                  // Cursor -> top-left "home"
    print("Up/Down = %d %c\n", ud, CLREOL);    // Display voltage
    print("Left/Right = %d %c\n", lr, CLREOL); // Display voltage
    pause(100);                                     // Wait 1/10 s


  • 4 Comments sorted by Date Added Votes
  • A couple of questions:

    How is the joystick wired?
    Are you using an ActivityBoard? The Prop BOE board has a different ADC and doesn't use the Simple ide library.
    When you run the program you listed, do you get any values printed? If so, do they make sense?

  • The joysticks wiring is in this link
    I am using the Activity Board WX, when I run the program I get expected results in my terminal. The print functions I have work. I just need help having the servo react to the joysticks movements.

  • twm47099twm47099 Posts: 543
    edited July 21 Vote Up0Vote Down
    The equation that gives angles from 20 deg to 180 deg depending on the Left to Right position (voltage) of the joystick is:
    lr = 20+(160*lrv)/5;

    I came up with that equation by subtracting 20 from your specified angle limits (to get 0 deg at 0 volts and 160 deg at 5 v), and then using a proportion 5/160 = volts/angle, solving for angle = (160 * volts)/5, and then adding back the 20 to get the equation above.

    To use that to drive the servo the command would be:
    servo_angle(17, lr*10);

    The values for "lr" from the voltage "lrv" in half volt intervals is
    lrv	lr 
    0	20
    0.5	36
    1	52
    1.5	68
    2	84
    2.5	100
    3	116
    3.5	132
    4	148
    4.5	164
    5	180

    If you want to position the servo based on both left-right and up-down, you would have to decide what relation you would want to use. If 2 servos one for LR and one for UD you would just use the equations above.

  • Thanks Tom I appreciate the help
Sign In or Register to comment.