Joystick with Interrupts

So I have been puzzling over the best way to do this...

I have a joystick that I would like to throw an interrupt anytime that the stick moves a certain amount from it's last position, or at least that it throws an interrupt as it's value passes certain thresholds (if the values are mapped between 0-255, then every time the numeric value passes a factor of 16 for example). My current solution is to hook the joystick up to an 8-bit ADC and then pass the binary values to a i2c 16 port expander that supports interrupts. The interrupt only checks the bits higher than 16 for changes and is then hooked up to an interrupt pin on my Arduino Leonardo (pin 7). Every time I move the joy the correct amount it throws the interrupt and goes into the ISR where I read in the new data and pass it back to the program.

While this works for several seconds the program always eventually locks up to the point that my serial printout freezes and I have to unplug the arduino from my computer. Apparently I am doing something very wrong. Any ideas how to better approach this issue?

Your code does not show you are using interrupts.

Paul

Hi, Welcome to the Forum

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)

My question is more conceptual rather than asking for help checking my code, so no code will be posted. Generally how would be the best way to generate interrupts from a joystick as described above?

90ender: My question is more conceptual rather than asking for help checking my code, so no code will be posted. Generally how would be the best way to generate interrupts from a joystick as described above?

A joystick moves so slowly there is no reason at all to try to use interrupts. Your main loop code should be able to track the movement.

Your description of your problems may lie in trying to do too much in interrupt code. Like serial print. That is why you really need to show the code if you are serious about the problem.

Paul

The main reason I was wanting to use interrupts was to minimize power usage. Since I am only going to be jogging the joystick every now and then, I was wanting a method that would be less processor intensive then continuously polling it. I am exploring several different ways of doing this and am wondering if there is a conceptually superior method.

90ender: I was wanting a method that would be less processor intensive then continuously polling it.

No matter how you write your code the Arduino will be doing 16 million instructions per second. You can use it for something useful (like polling) or you can leave it uselessly twiddling its thumbs.

The only way to reduce the activity is to put the microprocessor to sleep and have it wake up when something needs to be done. However I doubt if there is any need for that complexity with a program for reading a joystick.

...R

Exactly, I am planning on putting the processor to sleep between interrupts. I know it may not make much sense to you, but let's say for educational purposes that it needs to happen. How would you do it?

There is not a lot of point putting the Atmega 328 on an Uno to sleep because the rest of the Uno board will be consuming power. If you are really interested in sleeping it would make more sense with an Atmega 328 on a breadboard. Have a look at Nick Gammon's power saving tutorial.

You need to study the Atmega 328 datasheet to see what options there are for waking it.

I don't think the mere change in value at an analog input would be sufficient. Maybe you could press a button before you move the joystick?

I guess a more practical approach would be to put it to sleep for (say) 50 millisecs then wake and check the ADC value and if it has not changed go back to sleep for another 50 msecs. That way it could be asleep for about 99% of the time.

...R