Rotary switch and Joystick.h library

Afternoon all - very new to this, but have been putting in the time to try and figure it out but have come full circle so hoping someone can help me understand what is going on.

A year of so ago I built a helicopter collective to be used in flight sims - just a couple of hall sensors and basic code - it's worked well and happy with it. I decided I wanted to add a few toggle switches and a couple of rotary switches. Each of the rotary switches have 6 positions, I didn't want to use up pins unnecessarily on the arduino so followed the advice on the web and used resistors to join the pins so changes in voltage out put could be detected and displayed in the monitor. All that works fine, tolerences are good and consistent.

So as it stands I have two bits of code, one for the collective itself, one for the test rotary switch - I'm at a loss on how to merge them. I use the joystick.h library and though it does most things, I can't see anyway of integrating the rotary switches, or 'maping' the output from each position of the toggle switch to a specific joystick button.

Another thing which is quite strange, in the sketch I have compiled, when I use the toggle switch it moves the 'X Rotation' bar in windows game controllers -so clearly I have done something completely wrong.....

So any help would be appreciated and gratefully recieved.


```cpp
#include <Joystick.h>
#define Collective A0
int rxAxis_ = 0;
#define Throttle A1
int Throttle_ = 0;
#define Rotary1 A5
int sensorPin = A5;
int sensorValue = 0;
int rotswitchNumber = 0;




Joystick_ Joystick(0x04, JOYSTICK_TYPE_JOYSTICK,

                   6, 0,  // Button Count, Hat Switch Count

                   false, false, false,  // X and Y, Z Axis

                   true, false, false,  //  Rx, Ry, or Rz

                   false, true,  //  rudder, throttle

                   false, false, false);  // accelerator, brake, or steering

//const bool initAutoSendState = true;


void setup() {
  Joystick.begin();

  Serial.begin(9600);
}

void loop() {
  Throttle_ = analogRead(Throttle) / 04;
  Throttle_ = map(Throttle_, 0, 1023, 0, 255);
  Joystick.setThrottle(Throttle_);


  rxAxis_ = analogRead(Collective);
  rxAxis_ = map(rxAxis_, 0, 1023, 0, 255);
  Joystick.setRxAxis(rxAxis_);



  // read the value from the sensor:
  sensorValue = analogRead(Rotary1);
  Serial.print("Value:");
  Serial.println(sensorValue);
  Serial.print("SwitchNumber:");
  Serial.println(rotswitchNumber);
  delay(1000);  // delay in between reads for stability


  if ((sensorValue >= 700) && (sensorValue <= 720)) {
    rotswitchNumber = 1;
  } else if ((sensorValue >= 740) && (sensorValue <= 760)) {
    rotswitchNumber = 2;
  } else if ((sensorValue >= 800) && (sensorValue <= 830)) {
    rotswitchNumber = 3;
  } else if ((sensorValue >= 850) && (sensorValue <= 880)) {
    rotswitchNumber = 4;
  } else if ((sensorValue >= 930) && (sensorValue <= 960)) {
    rotswitchNumber = 5;
  } else if ((sensorValue >= 1015) && (sensorValue <= 1023)) {
    rotswitchNumber = 6;
  }



  Joystick.sendState();

  delay(10);
}

Until you can tell specifically and exactly what each switch should be doing when set one way and when set the opposite way, not much can be done to help. Surely you have that documented somewhere.

Sorry, not sure I follow - I'm not refering to toggle switches - that I can do. What I'm refering to is the rotary switch. As far as documentation is concerned, no I don't and not sure why I would need to. Right now I am just playing with ideas so see what I can and can not do, especially within my limited knowledge which is the point of asking the question.

In it's simplest form I am asking if there is a way of mapping an ouput from each position of the toggle switch, which is a definative 6 position switch to a yet undefined joystick button.

So in the setup of the joystick sketch I can stipulate 6 buttons which would show up in windows under the arduino gaming controller. Is there a way I can marry the two?

Do you mean the rotary switch? If so, you can create a string or resistors, all in series and connect one switch terminal to the top of the resistors and one to each of the connections where two resistors are connected. If the resistor chain is connected to 5 volts at one end and ground at the other, that will give you a bunch of different voltages that can be tested for using the A/D converter of the Arduino. Then you can write code to do something with each step of voltage.
BUT! You have to be aware that your rotary switch will have an OPEN position as you rotate the switch from position to position.

1 Like

Are they rotary SWITCHES, or rotary encoders?

These are 1 pole 6 position rotary switches. It's not a biggie I have some expansion cards that will provide enough pins - just seemd silly if I could use one pin with resistors. Like I said it works fine - it's about how I map the output from each rotary position to a specific switch.

Morning Paul, yep that is how I have wired it, like I say it gives a consistent reading - I think one of the things that was mind blocking me was that from what I read this type of setup has to go to an analog pin - where as in my head my flawed logic was saying surely digital - especially as in the joystick.h library that is where all switches are assigned leaving the analog for axis. I did recall reading some time ago that the analog pins could be used as digital - clearly that thread got lost in the fog! Anyway thanks for reminding me, that will be my journey today! Re the OPEN, I had read this, but as this is just a function for a flight SIM I don't envisage it breaking anything. I guess in context of that I could slow the polling / delay of the switch state. In that can you define a delay for a particular part of the loop, or is a delay statement global, in which case that won't work too well for the hall sensors. Again thanks for the pointer.

Delay is just what it means. Stop right there and do nothing for the requested time.

1 Like