Joystick Input

How do I get the BASIC Stamp to recognize commands from a joystick? I have it adjusting variables with the keyboard, but I ned a joystick to do it.



That would largely depend on the output format of your joystick. Is it outputting analog voltages or digital signals like serial data?

- Ben

I don’t even have a joystick yet. I was just wondering what I should get and how to make it work. I was planning on having the joystick hook into the computer, the computer to the BASIC Stamp, and th Stamp to the TRex JR’s.

Almost all joysticks are mounted to 2 degree of freedom joints that turn potentiometers, like this, and if all you want to do is drive motors in response to the joystick position, you can configure your TRex JR to analog mode, hack up any old joystick to get access to the potentiometer wires, and use the potentiometers to directly control the motor speeds. You wouldn’t actually need a computer or basic stamp at all in this setup, unless your stamp is doing other things as well. What is the basic stamp doing in your setup now anyway? Come to think of it, what is this project?

If you want remote control you could buy a hobby radio transmitter/receiver pair and hook the receiver up to the TReX in RC mode.

If you need the computer around anyway and/or don’t want to hack up a joystick there are libraries that will let you poll the position of a joystick (or a mouse for that matter). Are you still working in C?

If you wanted this to run separately from your computer, but needed more functionality than you can get from the TReX in analog mode alone, you could get some analog to digital converter ICs to interface with your Basic Stamp, but that’s a whole other level of complexity. You could also switch to a microcontroller that has analog-to-digital conversion built in.

Sorry that answer isn’t more straightforward, but now that you know some of your options, do you have an idea of what direction you want to go in?


Actually now I’m using BASIC because I’m using the BASIC Stamp as a liason between the TReX and the computer because I know BASIC a lot better than C. I want to use the BASIC control through the computer because I will ultimately be using 2 TReX Jr’s to control four motors. If I can get the BASIC Stamp to send out commands input by the joystick to the separate controllers, I get exactly what I want. I basically (no pun intended) want the Stamp to send out commands to the motors with analog variable control instead of digital.


P.S. I also don’t think I have the capability or experience to “hack” a joystick correctly.

P.S.S. The project is an underwater ROV for the MATE ROV competition.

Now I remember, I think you said it was for the MATE competition already in another thread.

Anyway, you’re sort of in a pickle here. As far as I see it there are basically two ways to go:

  1. Get a joystick that plugs into your computer and install/calibrate it, find libraries that let you read its position and communicate with the Basic stamp, or with the TReX directly. This option requires quite a bit of C programming, but it’s definitely doable. A quick google search reveals that lots of people have done something akin to this.

  2. Open up a joystick and get direct access to the potentiometers, wire these directly into your TReX, and change the jumper settings for analog mode (also, go through the calibration described here). This option requires some wiring, but I think it’s probably the best since it is by far the simplest and most robust!

When I say “hack” a joystick, I’m not talking about anything more complicated than taking out the screws, yanking out the circuit boards, and either finding the wires that connect to the potentiometers, or soldering wires onto the potentiometers themselves. You can even buy little joysticks with just potentiometers, but I bet you want something more like a big gaming joystick, and just about any old one you can find lying around will do.

Of course there are other options, but interfacing a joystick directly with a basic stamp is (in my opinion anyway) more complicated than either of these two.


The problem I have is that I need to control four motors. I suppose I could use two joysticks… Each wired to a separate TReX, one controlling the vertical motors and one controlling the horizontal motors…

What do you think would be the best way? Is there not a way to use BASIC instead of C for the joystick through the computer and BASIC Stamp? I know BASIC a lot better.

I’m a mechanical engineer by training, so I tend towards ‘simple’ mechanical solutions, and in general it’s always better to have fewer components, from a reliability point of view.

How are your motors arranged? The TReXes give you some channel mixing options, so you might be able to do something cool with a single joystick with a twist axis and/or a throttle slider (like one of the old Microsoft Sidewinders).

There are a variety of languages you can write Windows programs for, including some Basic-like languages, but not exactly Stamp basic. You said you had some experience with C though, so if you do keep the computer in the loop you should probably stick with that. You’re going to need to find libraries to poll a joystick position, and you’re more likely to find them with example code written in C or C++.


Our we have two horizontal motors and two vertical motors. They are hooked up to the two TReX’s. I don’t really know about the direct input to the TReX, I have more experience with the serial commands.

So at this point, I just am not sure what to do.

I was just curious about how you wanted to map motor speed to joystick axes, to see if you could do it with the TReX’s built-in mix mode option. I think you could actually, but regardless I think I just changed my mind completely.

Your ROV is going to be at the end of a long tether, right? Have you decided how long? In any case, direct analog control over long wires is not a good idea at all, so I guess computer control it is.

I’m still a little confused about your current setup. I understand that you have your basic stamp building command strings for the TReX but you said you’re adjusting variables with a keyboard. Are you typing characters into a terminal program and having the stamp receive them and do things in response, or did you make your own Windows program to read key presses and send serial characters in response? In either case, could you post your source code/codes?

If you’ve already got a working C program that reads key presses and sends them (or other bytes) out a serial port though, it shouldn’t be much work to add generic windows joystick polling to it.

Also, how do you have the Basic Stamp connected to your computer? Is it on one of the breakout/programmer boards, like the board of education or the homework board? Is it using a USB link or RS-232 serial? RS-232 (computer serial port) is probably your best choice for a signal that has to go over a long tether to your ROV.


P.S. If you don’t want to do any programming AT ALL, I remember an old Logitech Wingman joystick with drivers that let you map motions of the joystick to keyboard key presses so you could play any game with it. Unfortunately I haven’t been able to get it to work with anything later than Windows 95, but it might be worth looking into.

When testing the analog mode of the TReX I used the joysticks on a generic Playstation controller, which you can buy for under $20. Each joystick has a horizontal potentiometer and a vertical potentiometer, so you’d be able to control four motors with one controller (and two TReXs). The controller outputs digital signals that are well documented online; you can probably do a web search for projects involving Playstation controllers and the Basic Stamp.

I wanted access to the analog signals, so I opened up the controller and modified it slightly. Using documentation I found on the web, I knew what color wires were power and ground. After supplying power to these wires, I used a multimeter to find the outputs of the four potentiometers on the PCB, and then I soldered wires to these outputs (specifically, I cut wires from the cable that I didn’t need and re-soldered them to the potentiometer outputs, which let me continue using the cable that came with the controller).

- Ben

I am at home now, so I don’t have the source code, I will post it tomorrow morning. Here is a diagram of how everything is set up:

I will edit this post with the source code around 7:30 PST tomorrow morning.

Another wrench in the works!

I see you have the USB version of the board of education, which is certainly more convenient to work with, but un-powered USB extension cables can only be 5m (16.4ft) long. You can connect multiple 5m powered extension cables, but you’re going to have a heck of a time waterproofing the connections.

If you wanted to you could pick up an older board of education with a serial interface, since normal serial cables can be on the order of 50 feet, but then you would really want a computer with a hardware serial port (a USB to Serial adapter wouldn’t work too well in this case).

A better idea might be to keep the board you have now, but get something like an extra long USB active extension cable with built in signal repeaters (like this), or a pair of USB over CAT5 cable adapters which would let you use a long network cable as your tether (like this). I don’t have any experience with either of those products in particular, they were just the first examples I could find with a quick Google search.

So, how long does your tether need to be anyway?


Actually, I don’t think that is a problem. The board is going to be above water next to the computer. We will have a cable (probably Cat-5e) running through the water to the robot, which will have the TReX’s in it. Or we could even have the TReX’s above water too.

When you start getting into longer cables strange things can start to happen. Low power things like serial signals start to pick up noise, and high voltages like on your motor power lines will drop a lot over the length of the wire and cause horrible RF interference, which your long signal wires are prone to pick up. In general you always want your motor wires to be as short and large diameter as practical, and twisted. I would really recommend having the TReX on board the ROV (assuming you can keep it dry of course!).

I don’t have a good idea how far a Basic Stamp is going to be able to push a serial signal (try it and find out!) but Cat5 is probably a good choice for serial signals between your Basic Stamp and your TReX. You would want to run the serial line and a ground line from the Stamp to the TReX using one of the twisted pairs in the CAT5 cable (a solid color wire and it’s corresponding color/white stripe wire). I would also suggest powering your Basic Stamp board from another power source, rather than running 5V all the way back from the ROV just to power it, especially when it has it’s own 5V regulator.


I don’t understand why we would run 5V from the ROV back to the board. We have a battery pack with 4 AA batteries powering the board now.

So we will have the TReX’s on board the ROV, but the BASIC Stamp above water with us.

My mistake! Your wiring diagram didn’t show a power source for the Board of Education, and I thought you were using the TReX’s battery elimination circuit to power it, but that’s just a ground connection (I always get confused by the VDD and VSS naming scheme).


Oh ok. haha. Sorry about not including that in the diagram. I completely forgot.

So I will post the source code tomorrow morning and we can continue the joystick troubleshooting. Sound good?

Sounds good.


Here is the source code:

As of now, I can control the forward speed of the motor only. Faster by pressing “a”, or, in ASCII, 97; and slower by pressing “s”, or, in ASCII, 115.
Today I will add the ability to turn the motor in reverse, but I don’t think the code with reverse control is necessary for what we are trying to do, so I won’t post it unless you want me to.