jbearnolimits Posted May 7, 2020 Share Posted May 7, 2020 I'm trying to code a switch panel for FSX. I'm using an arduino microcontroller. I have an on/off switch. Its wired to GND and D3. I can get it to turn the landing lights on by flipping the switch. But then the switch just stays on until I flip it off. At which point the computer recognizes the button is no longer pressed. BUT the landing lights stay on. Obviously, without another button being pressed to turn the lights off the simulator doesn't know I want that to happen. So, is there a way to code the switch so that when it goes from on to off the computer can recognize that it needs to basically hit the button again? I know I could probably just buy a momentary switch but that isn't really something I want to do because I already have this switch and if I can get it working then all the better. Link to comment Share on other sites More sharing options...
jbearnolimits Posted May 8, 2020 Author Share Posted May 8, 2020 I'm wondering if it's because fsx expects button inputs like a joystick but I am using a switch? Link to comment Share on other sites More sharing options...
TheAwesomeCobra Posted May 9, 2020 Share Posted May 9, 2020 I was about to ask the same thing. Basically I need it to see each toggle (off to on, on to off etc.) as the same button press. Link to comment Share on other sites More sharing options...
jbearnolimits Posted May 10, 2020 Author Share Posted May 10, 2020 I think I have it figured out. I'm going to test my code later tonight. If it works I will share the code with you. Link to comment Share on other sites More sharing options...
jbearnolimits Posted May 15, 2020 Author Share Posted May 15, 2020 Got the code for anyone that needs it. Just remember to have the switches turned off when starting the sim. // This example makes an on off toggle switch act like a joystick button. A quick pulse is sent rather than a continual pulse. // // NOTE: This sketch file is for use with Arduino Leonardo and // Arduino Micro only. // // //-------------------------------------------------------------------- #include Joystick_ Joystick; void setup() { // Initialize Button Pins - Mine starts with pin D3 because the D2 pin was burned out. pinMode(3, INPUT_PULLUP); pinMode(4, INPUT_PULLUP); pinMode(5, INPUT_PULLUP); pinMode(6, INPUT_PULLUP); pinMode(7, INPUT_PULLUP); pinMode(8, INPUT_PULLUP); pinMode(9, INPUT_PULLUP); pinMode(10, INPUT_PULLUP); pinMode(11, INPUT_PULLUP); pinMode(12, INPUT_PULLUP); pinMode(13, INPUT_PULLUP); // Initialize Joystick Library Joystick.begin(); } // Constant that maps the phyical pin to the joystick button. Again, I use pin 3. const int pinToButtonMap = 3; // Last state of the button int buttonState = HIGH ; int lastButtonState = LOW ; int index = 0 ; int buttonPushCounter = 0 ; void loop() { // read the pushbutton input pin: Again, I use pin 3. buttonState = digitalRead(3); // compare the buttonState to its previous state if (buttonState != lastButtonState) { // if the state has changed, increment the counter if (buttonState == HIGH) { // if the current state is HIGH then the button went from off to on: // "press" the joystick button for 10 milliseconds Joystick.setButton(0, HIGH); delay(50); Joystick.setButton(0, LOW); } else { // "press" the joystick button for 10 milliseconds Joystick.setButton(0, HIGH); delay(50); Joystick.setButton(0, LOW); } delay(50); } // NOTE: save the current state as the last state, for next time through the loop lastButtonState = buttonState; } Link to comment Share on other sites More sharing options...
Recommended Posts