Joystick / Controller

Posted on
  • for a LED tetris game, I'll need a controller. Which joystick would you recommend and how do I connect/control it?

    thank you,

  • If you want something that looks nice and is easy to wire up, the Wii Nunchuk could be a good idea:

    Or are you after something much smaller?

  • Also - if you have old PC joysticks (with the 15 pin D connector) then those are pretty easy to wire up.

  • thank you, this is already helpful! while the Wii nunchuck is well documented, which modules / functions would I use to control a joystick?

  • 5 plain Buttons - not as elegant as joystick - will do just as well and are actually easier to program: setWatch(); and you have much more distinctive events. With ajoystick uou may need to poll - and depending the type of joystk - handle the analogue values yourself. @user697's suggestion to use a WII NUNCHUCK gives you much more dynamics that can outweigh the drawback of polling... dealing with the contiguous values requires much more logic though.

  • thanks, got it. I found some documentation on two buttons, but still have no idea how to wire up several of them or a joystick. I think this is a common use case for projects:

    Do you know a similar detailed tutorial with example code and instructions how to wire it for a joystick?

  • Start with the Espruino Pico Button example... Btw, there are already several implementations of Tetris' on forum, for example, Tetris on RGB matrix.

  • I'd say:

    • Take your buttons, and connect one side of each to 3.3v
    • Connect the other side of each to a spare IO
    • For each button, add the command pinMode(BTN_PIN, "input_pulldown")

    Now, you could use setWatch to call when something happens, but as you're making a game chances are you have a function that gets called each frame. If so, you might want to do something like:

    var BTN = { LEFT: A5, RIGHT: A6, FIRE: A7 };
    pinMode(BTN.LEFT, "input_pulldown");
    pinMode(BTN.RIGHT "input_pulldown");
    pinMode(BTN.FIRE, "input_pulldown");
    function onFrame() {
      if (;  // go left
      if (;  // go right
      if (;  // fire!
  • great, that looks manageable. I'll continue with the project mid-november once all the components are there; meanwhile I can practice :-)

  • I'd do it the other way - set the pins input_pullup, and wait for them to go low, rather than high, and connect the other side of the button to ground instead of 3v3. Better practice to do it like this since you don't have to run the power rail all over hell (and you're already running ground all over hell), and a short involving Vcc is likely to be worse than a short involving Gnd.

    You'll find that this is almost always how non-matrix'ed buttons are done in commercial products. Also more generally applicable, since many micros only have pullups built-in, no pulldowns.

  • As @DrAzzy says, that's probably more sensible. It just means that digitalRead will then return 0 if the button is pressed and 1 if it isn't.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Joystick / Controller

Posted by Avatar for user69728 @user69728