in arduino, electronics

Worklog #2: Handling joystick input on Arduino and LinkIt ONE.

After expanding the LinkIt ONE and Arduino analog inputs, I had to find a proper way to handle user input. The easier and most convenient way was using an analog joystick, so I ordered one off AliExpress.

As I figured it will take way too much time to get here, I decided I’d open up my Xbox 360 controller and use its joysticks in the meanwhile.

 

Butchered 360 controller. Red = VCC, Violet = GND, Yellow = A0, Orange = A1.

Butchered 360 controller. Red = VCC, Violet = GND, Yellow = A0, Orange = A1.

Joysticks are made by joining two analog pots together (one for vertical movement, and one for horizontal) and a tactile button.

After hooking them up to +5V and GND, I saw they output an analog value > than 900 if they are held up or left, and around zero when held down or right.

The first and best way to do this is by using timer interrupts (check this nice instructable for more info). Doing so, the Arduino checks the joystick for changes pretty much always, no matter where it is in the code. However, I am not sure if this can be done on the LinkIt ONE, as there is no documentation at all about timer interrupts.

The Arduino way – using timer interrupts.

Take a look at the following code:

 

It’s pretty easy: it runs the ISR() function 50 times a second (!) which checks the value of the joystick and updates a “direction” variable accordingly. The code in the main loop then updates the vertical and horizontal positions.

The timer interrupt is disabled then re-enabled so that the Arduino updates the direction only if the previous joystick movement is complete. Shouldn’t we disable them, holding the joystick down for half a sec would make the “position” variable skyrocket (the ISR would run way too many times). This is only replaceable with a delay(), which stops everything in your program and that is why this is the better solution.

Another solution is checking the joystick state at the beginning of the loop() function, without using timer interrupts. This is what I’m currently using on the LinkIt ONE.

No interrupts – for both Arduino and LinkIt ONE

Here it is:

 

As you can see, the joystick checking code is put at the beginning of the main loop. This is not a problem is your program is not doing anything, but you may run into some trouble if the Arduino is busy as it may miss your joystick movement.

Button handling (for confirmation and exiting/entering modes!) coming early next week.

Have fun!

Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Share on RedditEmail this to someoneFlattr the authorShare on Tumblr

Write a Comment

Comment

 

Loading Disqus Comments ...
Loading Facebook Comments ...