Thread: Programming Arduino Joystick

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    6

    Programming Arduino Joystick

    Just wondering, how do I read the X and Y value, and execute a command if the X or Y value reaches a certain value.

    What I would ideally like, is when I move the joystick on the +Y axis to go right, and -Y axis to go left. Also, if I move the joystick on the +X axis to press 'space', and on the -X axis to do nothing.

    Thanks to all that spends time on this post! Much appreciated!

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    214
    The setup is covered in tutorials like this one: Arduino Joystick Module Example |

    The rest is simple C stuff, once you obtain the values with

    Code:
    xPosition = analogRead(xPin);
    yPosition = analogRead(yPin);
    This assumes you've setup xPin and yPin (check the tutorial), and have integers xPosition and yPosition.

    Once you read these, you choose to do with yPosition as you describe, but what "goto right" means depends on way too much information we don't have....is the Arduino controlling motors or something?

    Also, what "press space" means is unclear. I assume something should happen when space is pressed - perhaps what you really need is merely to do that thing, whatever it is, when xPosition is positive.

    However, what is important to keep in mind is that positions taken from a joystick happen in a loop, repeated rapidly. That's why "goto right" is unclear. What if the sample of xPosition is at a rate of 100 times per second, do you want to "goto right" 100 times each second?

    These things require a plan, and to make that plan we must have far more information about the rest of what you're doing.
    Last edited by Niccolo; 06-17-2019 at 02:59 PM.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    These things require a plan, and to make that plan we must have far more information about the rest of what you're doing.
    I suggest that a good place to start is a state machine in the main loop.

    Just a switch statement should be enough to get you going, and it will also help to use an enum for your variable tracking.

    As for the Arduino itself, you are better off asking specific questions about it on the Arduino forum. These are people who know the devices inside out.

  4. #4
    Registered User
    Join Date
    Jun 2019
    Posts
    6
    Sorry! What I should've been more clear, I what I meant by go right and left, is ARROW_LEFT and ARROW_RIGHT, and what I mean by press space, I mean that when I move the joystick across the X axis, my spacebar is pressed. I don't have any motors/rotors, I simply want it to execute some keyboard functions/keys.

    Thanks again to everyone!

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    214
    I simply want it to execute some keyboard functions/keys
    That goes back to that part of my previous question and suggestion, that when a key is pressed, there's usually a response of some kind, and that response should be called.

    My point here is that how one goes about setting keyboard state information is dependent on the platform. Is this a supposed keyboard ON the Arduino?

    Indeed, my point being, that there are ways of implementing code which actuates the very signal that a key press (and a key release, it's a two step process) sends, but that may not be what you actually require.

    If you've written code that calls a function DoLeftArrow() when the left arrow key is pressed down, then perhaps you merely need to call DoLeftArrow() instead of simulating a keyboard signal state for the left arrow.

    Again, therefore, that brings up the issue of repetition. The keyboard repeats at a rate in the range of about 4 to 6 character repetitions per second, maybe 10 at most. The joystick sensors may process around 100 repetitions per second. How would code respond if you could hit the left arrow key at a rate of 100 times per second?

    Probably not what you expect.

    So.....we need to get into what you really expect.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    So you want to use your computer keyboard to control your Arduino...

    Via serial, TCP,...

  7. #7
    Registered User
    Join Date
    Jun 2019
    Posts
    6
    The opposite actually, sorry I can't word things very well.

  8. #8
    Registered User
    Join Date
    May 2019
    Posts
    214
    So let me get this straight, you're saying you want controls coming from the joystick, from code running on the Arduino, to send message to the PC so that the left/right arrows on the PC act as they there were tapped?

    Space, too, obviously.

    Is that the direction and plan?

    If so, what operating system is on the PC?

  9. #9
    Registered User
    Join Date
    Jun 2019
    Posts
    6
    Yes! Exactly!

    Sorry for being a pain to work with. I'm running the latest version of Windows 10.

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    How do you want to physically connect to the computer? USB? Or do you have the old style connector? Or are you going for something like Bluetooth?

  11. #11
    Registered User
    Join Date
    Jun 2019
    Posts
    6
    I will connect it through USB.

  12. #12
    Registered User
    Join Date
    May 2019
    Posts
    214
    @Swof, ....I'm married, so that's nothing new.

    Ok, first I can offer a general plan.

    What you require first and foremost is knowledge about the way your Arduino interfaces to your PC. Some use USB. Some have WiFi. Very different beasts. There's a study on that subject ahead of you, way too much to just lay out here.

    The idea, however, is to create an application for the PC first. This will probably be based on the "Tray Icon" example (there's lots around, look them up). The application just places an icon in the system tray, and runs in the "background" otherwise.

    This application would then listen for the Arduino. The Arduino will process the joystick sensor like the example I posted, then forward the information through whatever means you have to connect to the PC, repeatedly from that loop.

    This tray icon based application, running on Windows, will receive these messages. It will "hunt" the desktop for the current focused application (more than we can go into in a post here). It will acquire a handle to that application, and the main window of that application. It will then send keyboard messages to that application appropriate for that information received from the Arduino. The messages may be the WM_KEYUP or WM_KEYDOWN message, with the appropriate virtual key data packaged in the messages. That simulates the keystrokes. You'll have to experiment a bit, but this means you'll probably need to send both key up and key down messages. Most applications actually respond to key up, because key down often involves the possible key repetition before an "up" is received.

    In any event, that's the basic plan that will end up doing what you require. For experienced programmers that know windows this is somewhat trivial to medium level. For beginners, this is quite a winding road about how to package messages, how to find which window has focus and how to receive information from a connection either through USB or TCP/IP (or serial port, or whatever).

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Give this a go...
    Arduino Reference

  14. #14
    Registered User
    Join Date
    May 2019
    Posts
    214
    ...and here I am suggesting to re-create a tool they implemented.

    My time with embedded was a while ago, except for certain robotics controllers...

  15. #15
    Registered User
    Join Date
    Jun 2019
    Posts
    6
    Quote Originally Posted by Niccolo View Post
    @Swof, ....I'm married, so that's nothing new.
    Haha! Thank you very much, if I have any issues I'll update this thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arduino Programming for Servo motor
    By ddang in forum C Programming
    Replies: 4
    Last Post: 07-16-2013, 11:04 PM
  2. Raw input from a HID joystick
    By Cynic in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-10-2012, 09:19 AM
  3. Arduino project programming question
    By qtip2293 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2011, 03:57 PM
  4. Arduino programming
    By Terese in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2010, 01:03 PM
  5. Joystick
    By merlin371 in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 07:15 PM

Tags for this Thread