More than one button - how to setup?

Posted on
  • Hi Espruinos!

    In my application I would like to use four buttons C2, C3, C4 and C5.

    The question is, what is the best way to set up these buttons? I tried

    function onInit() {
       //Set the buttons to 0
    setWatch(function() {
      l = l + 1;
    }, C5, { repeat: true, edge: "falling", debounce:100});
    setWatch(function() {
      m = m + 1;
    }, C4, { repeat: true, edge: "falling", debounce:100});
    setWatch(function() {
      n = n + 1;
    }, C3, { repeat: true, edge: "falling", debounce:100});
    setWatch(function() {
      o = o + 1;
    }, C2, { repeat: true, edge: "falling", debounce:100});

    Each of the buttons is connected to V_bat on the one side and to C2,3,4 or 5 on the other side. But when I press a button (let's say the one connected with C2), the variable "o" does not increase when I press it the first time, when I press it again it jumps from 0 to 2, then it works and jumps from 2 to 3 and so on but with no reproducibly.
    I just want to increase by 1 when a button is pressed.

    Thanks a lot, Andreas

  • Hi. I think your problem is that when you run setWatch, the pin state is changed to an input. Try manually specifying it by replacing:



      pinMode(C2, "input_pulldown");
      pinMode(C3, "input_pulldown");
      pinMode(C4, "input_pulldown");
      pinMode(C5, "input_pulldown");

    Your other problem (which isn't good for Espruino) is that when you press the button, you're applying 5v (from VBat) to 3.3v only pins. SeeĀ­OBOARD - the pins that can't take 5v are marked with 3.3.

    I'd suggest that instead of connecting the other side of the buttons to VBat, you connect it to 3.3

    Hope that helps!

  • Hi Gordon,

    Thanks again! It works absolutely fine now. I'm very new in hardware programming and the espruino is really great and a lot of fun to work with!

    Best regards, Andreas

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

More than one button - how to setup?

Posted by Avatar for Andreas @Andreas