OK. Here is some explanation to my current setup:
I have one setup where I use a Arduino Leonardo that simulates a Joystick.
Here I have 1 trim rotor, two buttons for Gear up/down and three lights.
Here is the code using the Joystick-library mentioned above.
Just upload and assign keys inside MSFS:
#include <Joystick.h>
Joystick_ Joystick;
int gearDown = 1;
int countMax = 100;
int count = countMax;
// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 3;
// Last state of the button
int lastButtonState[10] = {0,0,0,0,0,0,0,0,0,0};
void setup() {
// Initialize Button Pins
setupPins();
// Initialize Joystick Library
Joystick.begin();
}
void setupPins(void){
// Set all the digital pins as inputs
// with the pull-up enabled, except for the
// two serial line pins
for (int i = 2; i <= 12; i++){
pinMode(i, INPUT_PULLUP);
}
pinMode(A5, OUTPUT);
digitalWrite(A5, gearDown);
}
void loop() {
// Read pin values
for (int index = 0; index < 10; index++)
{
int currentButtonState = 0;
if(index < 2){
currentButtonState = digitalRead(index + pinToButtonMap);
}else{
currentButtonState = !digitalRead(index + pinToButtonMap);
}
if (currentButtonState != lastButtonState[index])
{
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}
Joystick.setYAxis(analogRead(A0));
Joystick.setXAxis(analogRead(A1));
if(count > 0 & count < countMax){
if(count % 10 == 0){
if(digitalRead(A5)) {
digitalWrite(A5, LOW);
} else {
digitalWrite(A5, HIGH);
}
}
}
if(gearDown & lastButtonState[1] & count == countMax) {
countā;
}else if(!gearDown & lastButtonState[0] & count == countMax) {
countā;
}else if (count < countMax) {
countā;
}
if(count == 0){
if(!gearDown) {
gearDown = 1;
count = countMax;
digitalWrite(A5, gearDown);
} else {
gearDown = 0;
count = countMax;
digitalWrite(A5, gearDown);
}
}
delay(50);
}
Then I have one Arduino UNO reading heading from MSFS:
For this you need to have FSUIPC running.
Then I use a Java-library:
And I implement this into the example from: http:\mouseviator.com
Basically on my PC I run the following code to send values to the Arduino:
outputValue = outputValue + "#";
System.out.println("Value: " + outputValue);
try {
byte[] comBytes = String.valueOf(outputValue).getBytes();
sp.getOutputStream().write(comBytes);
sp.getOutputStream().flush();
} catch (IOException ex) {
Logger.getLogger(FSUIPCSimMonitor.class.getName()).log(Level.SEVERE, null, ex);
}
Then on the Arduino side I have the following code running:
if (Serial.available() > 0) {
byte incomingByte = 0;
incomingByte = Serial.read(); // read the incoming byte:
heading = incomingByte;
lcd.setCursor(0, 0); // set cursor to first row
lcd.print("Heading: "); // print out to LCD
if(incomingByte == 35){
cursor = 0;
}else{
lcd.setCursor(cursor, 1); // set cursor to secon row
lcd.print((char)incomingByte); // print out the retrieved value to the second row
cursor++;
}
}
There is definately some room for improvements, as I now send the # for recognizing when the text stopsā¦
But so far working fine at least