As part of a larger project, adapting a midi joystick to control a robot to provide full movement with a laser turret mounted on top.
Hi, I’m Jason. You may remember me from Setting up LCD via GPIO on RasPi – Part 1 and Making a Pumpkin Sing. This time I will be adapting a midi joystick to control a robot to provide full movement but also a laser turret mounted on top.
First step is to find a midi joystick, and thanks to eBay I picked one up cheap (as no computer these days has a midi port, these can be picked up very cheap) and immediately took it apart to see how it works. It took a while to work out what goes where, as there is the X and Y axis, a switch and 2 buttons. I cut off the midi connector to create a pin using the expose wires.
Surprisingly there were only 6 wires, and using a LED and power source (provided by the trusty Arduino), I eliminated which wire was the power source (which actually turned out to be 2 wires, 1 each for X and Y, and 1 for each button). The auto-fire switch (when in the off position) lit the LED dimly and full brightness with the auto-fire on, which could prove to be a bit troublesome coding it. As it was tied directly into it’s own circuitry, it promptly came out and wired directly into a new pair of wires.
Most important is the X and Y control. As it worked in the same fashion as a potentiometer, providing analog values, ranging from 0 to +5v. Unfortunately the Raspberry Pi can’t read analog but the Arduino can, sporting 6 analog in pins. Taking the Arduino AnalogReadSerial example to read the range that the axis gives, to then use it to calculate the angle for the servo.
Expecting a simple read out (ranging from 0 to 1023 at each end) and a middle value of 511, but instead getting a middle value of 400, 0 from immediate left up to about a quarter leaning left and 500 at the upper limit to the right. Taking 5 readings at equal intervals the figure looked like this:
0 – 20 – 400 – 450 – 500
Instead of (using 500 as the upper):
0 – 125 – 250 – 375 – 500
This isn’t what I expected… Or wanted.
Now that I had to re-evaluate what’s happening (why I am getting an uneven figure), I tried experimenting with different circuits, swapping the ground and +5v on each terminal, adding resistors and trying to add some compensation code to even out the spread. Unfortunately nothing worked stable enough to be of any use.
Looking like an instant fix (which in real time took another 2 days of 3 hours), as a process of elimination I tried swapping the 5v for 3.3v not expecting anything miraculous to happen to be surprised with steady even figures that matched the position perfectly, (I could of cried after a simple swap (but I didn’t)) and trying it with the Y axis, again a steady stream of numbers gets reeled off in the serial monitor. At last I’m getting somewhere!
Deciding not to throw the joystick away and move onto something easier like quantum mechanics, I get both axis’ hooked up to the Arduino inputs, 2 servos into the digital pins (made sure they were in correctly!), modified the knob example and it springs into life! Then gluing 1 servo to the arm of the second giving full 180 degree movement, left right up and down.
Now all I need to do is stocking up on plenty of headache tablets and attempting to have the same control but over a distance using Wifi!
To be continued…