Jump to content

On Off Switch


Recommended Posts

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...