Joystick with Espruino WiFi

Posted on
  • Hi everyone,
    I would like to use this Joystick: https://www.adafruit.com/product/512
    with the Espruino WiFi (2v02), it currently works on Feather M4 Express (CircuitPython):

    import ...
    
    stickY = analogio.AnalogIn(board.A0) # analog
    stickX = analogio.AnalogIn(board.A1) # analog
    stickSelect = DigitalInOut(board.A2) # digital
    stickSelect.direction = Direction.INPUT
    stickSelect.pull = Pull.UP
    
    while True:
    ... yValue = stickY.value
    ... xValue = stickX.value
    ... selectValue = stickSelect.value
    ... if yValue > 90 and yValue < 32000: print("Y DOWN")
    ... elif yValue > 40000 and yValue < 65500: print("Y UP")
    ... elif xValue > 90 and xValue < 32000: print("X LEFT")
    ... elif xValue > 40000 and xValue < 65500: print("X RIGHT")
    ... elif selectValue == False: print("PRESS")
    ... else: pass
    

    In JavaScript I'm doing tests, Y and X both are recognized at '50 %' (yes UP and LEFT, no DOWN and RIGHT) ...

    var BTN = {Y: A0, X: A1, SELECT: B10};
    pinMode(BTN.Y, "input_pulldown");
    pinMode(BTN.X, "input_pulldown");
    pinMode(BTN.SELECT, "input_pulldown"); // (auto, input_pulldown and input_pullup) => NOT WORK
    
    while(true){
      if (BTN.Y.read()){console.log("Y");}
      else if (BTN.X.read()){console.log("X");}
      else if (BTN.SELECT.read()){console.log("SELECT"­);}
      else {};
    };
    

    What do you think I can solve?

    THANK YOU

  • Looks to me like that's an analog joystick, so you need to read analog values. You also don't need the pin modes that you had:

    var BTN = {Y: A0, X: A1, SELECT: B10};
    pinMode(BTN.SELECT, "input_pullup");
    
    setInterval(function() {
      yValue = analogRead(BTN.Y);
      xValue = analogRead(BTN.X);
      selectValue = !digitalRead(BTN.SELECT);
      console.log(xValue, yValue, selectValue);
    }, 500);
    

    Something like the above should print some values between 0 and 1, and you should be able to figure out when the joystick is over one side by looking at those values (eg less than 0.2 or greater than 0.8 maybe).

    Also one thing to watch out for is I'd really avoid while(true) in JavaScript because JS likes to execute small functions that execute quickly in order to be able to multi-task. I changed it for some code that checks every 500ms (twice a second)

  • great, it works!

    thank you so much!

    :-)

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

Joystick with Espruino WiFi

Posted by Avatar for Andrea @Andrea

Actions