Servo control using the joystick module

I wanted a way to build a steering system. I’ve previously covered using the joystick module and controlling a servo with the extender module.  Here is a way to control a servo with the joystick module.  This uses parts of both example. I also use the display module to show if the servo is moving left or right and the joystick position.  This full solution can be found on my Gadgeteer GitHub repository.

The setup is farily simple.

However, the assembly is a bit more complicated this time.  There isn’t enough power to drive the servo from the 5V pin.  I had to use an external power supply to provide the required 5V and 500mA current.  So the servo is only connected to the extender module to the PWM pin 9.

First, I wrote a method to demine if the joystick position had actually moved. There’s some variation in the joystick postion. I use a variance factor of 0.03.

The code to move the servo is similar to my previous example.

The code which responds to the joystick is not pretty. First, I needed to figure out how much to move the servo for each range 0.1 units of joystick movement. Then I move the servo to that position. For example, the lowest and highest servo positions are 650 and 2350, respectively. The entire range is 1700. The joystick has a range of 0 to 1. For each 0.1 the servo should move 170 units. This makes the code easy to read, if repetitive.

Using the move servo code from the previous example allows for very rapid servo movements. The code could be made to be more responsive to the joystick by adding more steps.