# Interfacing Arduino with an Analog Joystick

I know, someone's probably asked about this before. But I have an Arduino Mega and an analog slide joystick from here, and I want it to somehow trigger an interrupt when it is moved (that's why I'm posting!). I've heard about people doing this with the pulse of a 555 timer, but I'm a little confused about how to make this work.

Here are the voltages outputted by the joystick when moved (decoded by the Mega's ADC, in x-y pairs). It appears that it is a resistive joystick that uses two voltage divider circuits. Up: 4.26, 2.57 Down: 0.85, 2.46 Left: 2.51, ??? (not stable, in the 1-4 volt range though...) Right: 2.67, 0.82 Center: Both values can range from 2.5-2.8 volts

Basically, I want something to fire an interrupt if one of the voltages is not in that range. How would I do this? Would I use a circuit that generates pulses, or a timer interrupt, or what? And how? These output voltages seem to be pretty complex, so I don't know if there's a circuit that could do it.

Considering it uses resistive outputs for the x and y axes. That stick is basically like have a pair of spring loaded pots that always return to center. You should be able to hook it us to a couple of the analog pins and perform an analogRead on them. Then set your parameters accordingly. If it doesn’t have to be precise then any value between 3 and 4 volts on the one axes is up. Anything between 1 and 2 volts is down. Then on the other pin you look for pretty much the same values to check left and right.

Just as an example:

``````int analogLR = 2;
int analogUD = 3;

int valLR = 0;
int valUD = 0;

void setup()
{
}

void loop()
{

if(analogLR >=3 && analogLR <=4)
{
// perform desired action for left
}
if(analogLR >=1 && analogLR <=2)
{
// perform desired action for right
}
if(analogUD >=3 && analogUD <=4)
{
// perform desired action for up
}
if(analogUD >=1 && analogUD <=2)
{
// perform desired action for down
}
}
``````

Its very basic but should give you an idea of the direction you want to head.

Thanks for your help. I know how to do that, but I am wondering how to set it up on an interrupt (the main loop is going to be very busy). It has to be either a timer interrupt or an external interrupt, I'm still wondering how to do that.

So am i wrong in thinking the interrupt could be defined in the if() statements?

So am i wrong in thinking the interrupt could be defined in the if() statements?

Yes. An interrupt service routine is a completely separate "void" function. I don't think an interrupt is the answer here, though.

I was wondering if I could define a timer interrupt to read the values, though. Which timers would I use for the Mega?

Ah! It seems two questions are being discussed here…

a) How to make something, say a change in the input to one of the analog inputs, CAUSE and interrupt to happen

b) How to use an interrupt, driven by a timer, to periodically execute some bit of code which isn’t being called some other way.

You've got that right ;)

the library I developed here: http://code.google.com/p/arduino-joystick/

should work for this as well. It might help you for half of the problem.

Thank you! I figured out how to set up a timer interrupt for the Mega BTW. I used the excellent library Timer3. If I increase the clock to the ADC, nothing should lock up and it should work. All in theory. :)

I will try it out.