Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: Beginner needs help - NC Pushbuttons

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    5

    Beginner needs help - NC Pushbuttons

    Hey guys, don't be rude but my Question is quite simple/dumb

    I have some NC Pushbuttons, and connected each with a pin and the common. (Teensy 2.0)
    Whats the code for this? Nothing seems to work. I have had a code example already on my PC from several days back.

    Code:
      
      pinMode(PIN_D3, INPUT_PULLUP);   // PIN_B3
      pinMode(4, INPUT_PULLUP);   // PIN_B7
      pinMode(5, INPUT_PULLUP);   // PIN_D0
      pinMode(6, INPUT_PULLUP);   // PIN_D1
      pinMode(7, INPUT_PULLUP);   //PIN_D2
      pinMode(8, INPUT_PULLUP);   //PIN_D3
      pinMode(9, INPUT_PULLUP);
      pinMode(10, INPUT_PULLUP);
      pinMode(22, INPUT_PULLUP);
      pinMode(23, INPUT_PULLUP);
    
    
    }
    
    void loop() {
      // Update all the buttons.  There should not be any long
      // delays in loop(), so this runs repetitively at a rate
      // faster than the buttons could be pressed and released.
    
      button3.update();
      button4.update();
      button5.update();
      button6.update();
      button7.update();
      button8.update();
      button9.update();
      button10.update();
    
    
    
      if (button3.fallingEdge()) {
        Joystick.button(4, 0);
      }
      if (button4.fallingEdge()) {
        Joystick.button(5, 0);
      }
      if (button5.fallingEdge()) {
        Joystick.button(6, 0);
      }
      if (button6.fallingEdge()) {
        Joystick.button(7, 0);
      }
      if (button7.fallingEdge()) {
        Joystick.button(8, 0);
      }
      if (button8.fallingEdge()) {
        Joystick.button(9, 0);
      }
      if (button9.fallingEdge()) {
        Joystick.button(10, 0);
      }
        if (button22.fallingEdge()) {
        Joystick.button(23, 0);
      }
      if (button23.fallingEdge()) {
        Joystick.button24, 0);
      }
    
    
     if (button3.risingEdge()) {
        Joystick.button(4, 1);
      }
      if (button4.risingEdge()) {
        Joystick.button(5, 1);
      }
      if (button5.risingEdge()) {
        Joystick.button(6, 1);
      }
      if (button6.risingEdge()) {
        Joystick.button(7, 1);
      }
      if (button7.risingEdge()) {
        Joystick.button(8, 1);
      }
      if (button8.risingEdge()) {
        Joystick.button(9, 1);
      }
      if (button9.risingEdge()) {
        Joystick.button(10, 1);
      }
      if (button10.risingEdge()) {
        Joystick.button(11, 1);
      }
      if (button22.risingEdge()) {
        Joystick.button(23, 1);
      }
      if (button23.risingEdge()) {
        Joystick.button(24, 1);
      }
    
    }
    Should work like this:

    NCs connecting Pin to the ground (Button not pressed)
    --> Press button
    Pullup Resistor pulls the pin to high signal
    --> "Active High"

    In fact, the problem ist my (non existing) general knowledge about arduino codes.
    Sorry for your wasted time

    Regards

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,154
    Your code is incomplete and incorrect. Try this code - it compiles for a Teensy 2 but I can't test it.

    Pete

    Code:
    #include <Bounce.h>
    Bounce button3 = Bounce(3,5);
    Bounce button4 = Bounce(4,5);
    Bounce button5 = Bounce(5,5);
    Bounce button6 = Bounce(6,5);
    Bounce button7 = Bounce(7,5);
    Bounce button8 = Bounce(8,5);
    Bounce button9 = Bounce(9,5);
    Bounce button10 = Bounce(10,5);
    Bounce button22 = Bounce(22,5);
    Bounce button23 = Bounce(23,5);
    
    void setup(void)
    {
      pinMode(3, INPUT_PULLUP);   // PIN_B3
      pinMode(4, INPUT_PULLUP);   // PIN_B7
      pinMode(5, INPUT_PULLUP);   // PIN_D0
      pinMode(6, INPUT_PULLUP);   // PIN_D1
      pinMode(7, INPUT_PULLUP);   //PIN_D2
      pinMode(8, INPUT_PULLUP);   //PIN_D3
      pinMode(9, INPUT_PULLUP);
      pinMode(10, INPUT_PULLUP);
      pinMode(22, INPUT_PULLUP);
      pinMode(23, INPUT_PULLUP);
    }
    
    void loop(void)
    {
      // Update all the buttons.  There should not be any long
      // delays in loop(), so this runs repetitively at a rate
      // faster than the buttons could be pressed and released.
    
      button3.update();
      button4.update();
      button5.update();
      button6.update();
      button7.update();
      button8.update();
      button9.update();
      button10.update();
      button22.update();
      button23.update();
    
    
      if (button3.fallingEdge()) {
        Joystick.button(4, 0);
      }
      if (button4.fallingEdge()) {
        Joystick.button(5, 0);
      }
      if (button5.fallingEdge()) {
        Joystick.button(6, 0);
      }
      if (button6.fallingEdge()) {
        Joystick.button(7, 0);
      }
      if (button7.fallingEdge()) {
        Joystick.button(8, 0);
      }
      if (button8.fallingEdge()) {
        Joystick.button(9, 0);
      }
      if (button9.fallingEdge()) {
        Joystick.button(10, 0);
      }
      if (button10.fallingEdge()) {
        Joystick.button(11, 0);
      }
      if (button22.fallingEdge()) {
        Joystick.button(23, 0);
      }
      if (button23.fallingEdge()) {
        Joystick.button(24, 0);
      }
    
    
      if (button3.risingEdge()) {
        Joystick.button(4, 1);
      }
      if (button4.risingEdge()) {
        Joystick.button(5, 1);
      }
      if (button5.risingEdge()) {
        Joystick.button(6, 1);
      }
      if (button6.risingEdge()) {
        Joystick.button(7, 1);
      }
      if (button7.risingEdge()) {
        Joystick.button(8, 1);
      }
      if (button8.risingEdge()) {
        Joystick.button(9, 1);
      }
      if (button9.risingEdge()) {
        Joystick.button(10, 1);
      }
      if (button10.risingEdge()) {
        Joystick.button(11, 1);
      }
      if (button22.risingEdge()) {
        Joystick.button(23, 1);
      }
      if (button23.risingEdge()) {
        Joystick.button(24, 1);
      }
    
    }

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    5
    Hey Pete, thanks for your time!

    tbh, i forgot to copy and paste the bounce part, and the setup header to the forum.

    it is the same code i used,.. not working. Even if i copy and paste yours, nothing happening

    // edit: for further information: i tried the blink.hex to check the board itself, it worked fine
    // Pins i soldered the NCs on are: B3, B7, D0, D1, D2, D3, C6, C7, D4 and D5 ... if that helps in some ways.

  4. #4
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,679
    Use a wire and connect one of your pins with GND - does it work?

  5. #5
    Junior Member
    Join Date
    Jul 2019
    Posts
    5
    Hey Frank,

    no need to try that anymore. Tested it at work several minutes ago. With my work-pc it worked as expected.
    So something with my windows is wrong at home, ffs.

    Thanks to you 2 guys for your time! I really appreciate your help! Have a nice week.

    Alex

  6. #6
    Senior Member
    Join Date
    Feb 2015
    Location
    Finland
    Posts
    123
    Quote Originally Posted by WakabaJDM View Post
    So something with my windows is wrong at home, ffs.
    You might need an USB 2 hub in between.

    I use a cable hub, one that has one standard USB male on one end, and mini-B (old Teensies) and micro-B (new Teensies, cellphones etc.) and a couple of full-size female USB connectors on the one end. I bought it from a local store for less than 10 euros. I also usually keep the Teensy I'm working on connected to it, so that the connect/disconnect stress is on the full-size connector at the other end of the cable, and not on Teensy's small connector.

  7. #7
    Junior Member
    Join Date
    Jul 2019
    Posts
    5
    Hey Nominal Animal,

    i am kinda use a "stress realease" for the teensy. It is implemented in my Simracing Wheel, vie Aviation connector, to a USB extension. So there shouldn't be a Problem.

  8. #8
    Senior Member
    Join Date
    Feb 2015
    Location
    Finland
    Posts
    123
    The main point was the hub. I don't recall the exact details, but it looks like some USB 3 ports have issues with USB 2 devices; with the easiest workaround being using an USB 2 hub in between.

    I use a cable hub, because it not only avoids those issues, but also reduces the stress on the Teensy USB connector (as I keep it connected to the cable hub).

    As to GX12/GX16 aviation connectors, I use those too (sourced from fleabay for my hobby needs). I like'em.

  9. #9
    Junior Member
    Join Date
    Jul 2019
    Posts
    5
    Actually, there are guys like me with only 2 USB 3.0 Ports, rest ist 2.0. So no issues in that way. But good to know

  10. #10
    Senior Member
    Join Date
    Feb 2015
    Location
    Finland
    Posts
    123
    Another option would be to put the buttons on a separate isolated circuit instead:
    Click image for larger version. 

Name:	isolated-6-buttons.png 
Views:	8 
Size:	24.0 KB 
ID:	17006

    The six buttons have something like 10k current-limiting resistors (on the left), and something like 100k pull-down resistors on the bottom, six each. Oops, looks like I might have mistyped the bottom 10k; that should be 100k. Those values are just off the cuff, not precise at all. One could add debouncing capacitors too.

    Each Si8660BA-B-IS1 provides 6 inputs on the VDD1 side, and 6 corresponding outputs on the VDD2, but with isolation. Oops, I forgot the 100nF = 0.1uF bypass capacitors between VDD1-GND1 and VDD2-GND2 on those, so 4 capacitors total. These are high-speed chips, and utterly overkill for this task, but they only cost about 1.76€ apiece at Mouser. The second side of these is connected to the 3.3V and GND on their respective Teensy LCs.

    The two RFM-0505S are isolated DC-DC converters. They have a 10uF bypass capacitor on their input side, and are connected to the VUSB and GND on their respective Teensy LCs.
    Their output side grounds are the button circuit ground; note that the Teensies do not share grounds at all. The positive output from the two DC-DC converters go through a diode (I've drawn a Schottky diode, but any diode would basically work) so that whichever converter has higher output, powers the button circuit, without trying to backfeed the other converter.

    You can even add another diode and an USB connector, for powering the button circuit from an USB power pack. The Si8660s consume very little power. You could also add LEDs, just to indicate whenever a button is actually pressed.

    This way, each Teensy is electrically separated from the others (protected to up to 1 kV voltage difference), and the button circuit from the Teensies.
    You can do the same using optoisolators, but seeing as you need two optoisolators per button, I think the Si8660s are a better choice here.

    (With a little bit of thought, one could probably make those Si8660 + isolated DC-DC converters as small modules, so one could wire as many of them in parallel as they need. Might be an interesting little board to design at EasyEda.com, and cheap to manufacture, too.)

  11. #11
    Senior Member
    Join Date
    Feb 2015
    Location
    Finland
    Posts
    123
    I whipped up a possible board, here; 27mm by 23mm. It is completely untested, so no guarantees! It might break things.

    It uses one Si8660-BA-IS1 chip, two 100nF (0.1uF) ceramic capacitors in 0805 SMD footprint, and six 0805 resistors, 1kOhm to 10kOhm. The resistors limit the current.
    You can optionally add six 0805 pull-down resistors, 47kOhm to 1MOhm, on the other side.

    Also optionally, you can add a RFM-0505S isolated 5V DC-DC converter, a 10uF 0805 ceramic capacitor, and an RB161MM-20TR SMD Schottky diode, to provide power to the button circuit. (I think the circuit might need a 250 Ohm resistor on the RFM-0505S output, to "waste" 20mA, to ensure stable operation.)

    The idea is that you can put these modules in parallel, the right side (O side) connected to a Teensy, and the left side to the button circuit (or whatever circuit you have, for up to 6 outputs). The left side I1..I6 pins are connected to the buttons. The RFM-0505S provide max. 200mA at 5V to the button circuit. The diode ensures the DC-DC converters do not try to backfeed each other.

  12. #12
    Senior Member
    Join Date
    Feb 2015
    Location
    Finland
    Posts
    123
    Because reasons, I whipped a 51mm 51mm board for up to 8 buttons, using only through-hole components, here, so one can easily order both the boards and the components ($2 + shipping for five boards at JLPCB, $5 + shipping for parts at LCSC). This is completely untested, of course. This board is intended to operate in parallel too, although one might omit the debouncing capacitors from all but one board.

    The SMD version is much smaller, more efficient, and probably even easier to solder. I'll update the SMD version to apply the fixes I mentioned in post #11, too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •