Simple switch panel and throttle for Flight Sim

Hello I'm just starting out with Arduino and decided on making a panel with switches for Flight sim. I've just trying to get something simple to work. I'm wondering if this is possible to sketch or if i should go a different route. i have been trying to learn python for a while but i feel that i get stuck in trying to read Arduino IDE code.

Welcome to the Arduino forum. IS this really your first attempt to learn to program an Arduino? Why begin with a massively big project? Why not begin to program with a single switch and learn how to program for it? Then add a second switch. Then learn how to use arrays to simplify programming for identical features.

Yeah its my first attempt but i felt like this would be as simple as doing with one switch beacuse i just use 1 digital input per switch but I'm maybe wrong. is it not like i just write same code over and over again per switch?

If you want to waste memory space. Learn about arrays for pin numbers and functions for code that is used more than once.

What about the output from your program? Are you doing anything with the data you receive from the switch positions, etc?

alright

Yeah ive installed a joystick library to make it a "gamepad" input in windows, ive gotten it to respond with my pc with my analog input and 2 switches. But i need to write my own code to get it to fit my case.

Maybe ive taken water over my head trying to save a few bucks...

Post your best working sketch.

a7

You are giving up way too soon. Give it 9 months before giving up!

1 Like

Your diagram shows an Uno and your picture shows a Leonardo. Leonardo could work for your project, Uno cannot, so you made the better choice, but building the circuit would have been much easier with a Pro Micro, and they are much smaller.

Uno and Leonardo have different pinouts. Can you show a diagram of how your Leonardo is connected please?

You may not know this, but, unlike Uno, on Leonardo it's ok to use pins 0 & 1 as digital inputs.

Your 12 switches could be connected using only 7 pins.

1 Like

I have only borrowed schetches from "GitHub - MHeironimus/ArduinoJoystickLibrary: An Arduino library that adds one or more joysticks to the list of HID devices an Arduino Leonardo or Arduino Micro can support." to try with

yeah i just felt it was way more complicated then i thought

No shame in that. No one would be anywhere without borrowing.

Post one that worked for you.

Post the any attempt you made to see and extend the programming patterns you learned about when you carefully read through the working example.

a7

1 Like

Yeah i just used "tinkercad" to show i got it wired and that is why it shows "UNO"

that i did not know.


did u mean like that? the black is ground for all the switches and all the colored ones are 1 per switch.

// Simple example application that shows how to read four Arduino
// digital pins and map them to buttons on a joystick or keys on a 
// keyboard uisng the Arduino Joystick and Keyboard libraries.
//
// The digital pins 9, 10, 11, and 12 are grounded when they are pressed.
//
// NOTE: This sketch file is for use with Arduino Leonardo and
//       Arduino Micro only.
//
// Pin  9 = Joystick Button 0 
// Pin 10 = Joystick Button 1
// Pin 11 = 1 key on the Keyboard 
// Pin 12 = 2 key on the Keyboard
//
// by Matthew Heironimus
// 2016-05-13
//--------------------------------------------------------------------

#include <Keyboard.h>
#include <Joystick.h>

Joystick_ Joystick;

void setup() {
  // Initialize Button Pins
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);

  // Initialize Joystick Library
  Joystick.begin();
}

// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 9;

// Last state of the button
int lastButtonState[4] = {0,0,0,0};

void loop() {

  // Read pin values
  for (int index = 0; index < 4; index++)
  {
    int currentButtonState = !digitalRead(index + pinToButtonMap);
    if (currentButtonState != lastButtonState[index])
    {
      if (index < 2) {
        Joystick.setButton(index, currentButtonState);
        lastButtonState[index] = currentButtonState;
      } else {
        if (currentButtonState) {
          Keyboard.write(47 + index);
          delay(500);
        }
      }
    }
  }

  delay(100);
}


that is for the switces but im looking for the one for my analog input
Untitled

#include <Joystick.h>

Joystick_ Joystick(0x03, 0x04, 3, false, false, false, false, true, true, false, false, true, false, false, false );

void setup() {
Joystick.begin();
}

void loop() {
Joystick.setThrottle(analogRead(A0));
Joystick.setRxAxis(analogRead(A1));
Joystick.setRyAxis(analogRead(A2));
delay (1);
}

THX. I'm due for a sleep cycle, but would it be fair to say at this point you are hoping to combine the functions of these two sketches?

And that you have had success with each, as it is?

a7

yeah ive just been trying and got kind of success of binding two simple ones, analog works and 3 switches

#include <Joystick.h>

Joystick_ Joystick(0x03, 0x04, 3, false, false, false, false, true, true, false, false, true, false, false, false );

void setup() {
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
Joystick.begin();
}

int lastButtonState[4] = {0,0,0,0};

const int pinToButtonMap = 9;

void loop() {
Joystick.setThrottle(analogRead(A0));
Joystick.setRxAxis(analogRead(A1));
Joystick.setRyAxis(analogRead(A2));
for (int index = 0; index < 4; index++)
  {
    int currentButtonState = !digitalRead(index + pinToButtonMap);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

  delay(50);
}

image

Shouldn't you be declaring the joystick with four buttons?

do you mean that i only got 3 buttons? im just trying with 3 right now to get it working

You're reading four, why not report them all?