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

Thread: Joystick, Spoofing a button press.

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    4

    Joystick, Spoofing a button press.

    Hello all,

    I am currently working on a button box for dcs, using the joystick example as a template.
    My question is, if I am using an on-off-on style spdt toggle switch. Is there a way that I can have windows/dcs recognize the central off position as an input?
    This is so I can create a key bind for Flaps Auto, Flaps half and Flaps full.
    My crude idea is to have an "IF" both pins 0 and 1 are low, output high on pin 2. And then have pin 2 connected to a resistor leading to ground. But I have my reservations about this idea working.

    The reason that I am using this style of switch is that I already have them and will be using them for other functions and it being next to impossible to find reasonably priced on-on-on switches.
    I am aware that I can beat this behaviour into dcs itself but that would require learning how to edit the control scheme lua files and would only affect individual planes and not be across the board.
    I apologise for the lack of code, I am at work so unable to attach any or try my crude idea

    Thank you all and have a nice day.
    Last edited by Beserker; 07-10-2020 at 04:22 PM.

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,422
    Use the bounce library to handle pins 0 and 1 and wire them normally high with pinMode INPUT_PULLUP. When either of them goes low you handle that button "press". When either of them goes high, that is when the SPDT has been returned to the off position.

    Pete

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    4
    Hi Pete,

    I assume that the bounce library will cause there to be a clean high to low/ low to high signal generated?
    How would I turn the lack of signal on both Pins 0 and 1 into a a signal that would be picked up as a button press?

  4. #4
    DCS lets you cycle through the 3 flap positions, when you assign a button or keypress. I just used an on-off-on momentary switch in my button box as otherwise DCS Bios needs editing to take care of an always-on switch. Clicking down shifts the flap position to the next state, eg: half to full. Click up twice from there to move switch to Auto flaps.

  5. #5
    More DCS info about handling a toggle switch: https://forums.eagle.ru/showthread.php?t=89226 its a long thread!

  6. #6
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,422
    @ Beserker
    When you return the switch to the neutral position, you will have just turned either the "left" (pin 0) or "right" (pin 1) side of the switch off. If you use the Bounce library, it is easy to detect when either pin 0 or 1 are released and you interpret that as a button press on the "middle". When either pin 0 or pin 1 are pressed that is interpreted as a release of the "middle" button.

    I've written a simple example of handling one button with the Bounce library.
    Code:
    #include <Bounce.h>
    /*
    Quick demo of using the bounce library to read
    a button. When the button is grounded the builtin LED
    is turned on. When the button is release (goes HIGH)
    the LED is turned off. In both cases an appropriate
    message is prinetd on the serial monitor.
    The pin does not need any external resistors -
    the internal pullup is used.
    */
    
    // Pick a pin for the button
    #define BUTTON 2
    
    #define LED LED_BUILTIN
    
    // Instantiate a debounced button with 5ms settling time
    Bounce bouncer = Bounce( BUTTON, 5 ); 
    
    void setup(void)
    {
      Serial.begin(9600);
      while(!Serial);
      pinMode(BUTTON,INPUT_PULLUP);
      pinMode(LED,OUTPUT);
      digitalWrite(LED,0);
    }
    
    void loop(void)
    {
      bouncer.update();
    
      // Has the button just been pushed?
      if(bouncer.fallingEdge()) {
        Serial.println("Button pressed");
        digitalWrite(LED, 1);
      }
      // Has the button just been released?
      if(bouncer.risingEdge()) {
        Serial.println("Button released");
        digitalWrite(LED, 0);
      }
    }
    If I have time, I'll extend the code to handle two buttons wired to a SPDT switch.

    Pete

Posting Permissions

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