In 3 way toggle how to get 3 inputs if kept in off position in joystick library for arduino pro micro

hello i am not a programmer but i am trying to build a button with 8 toggles in which 3 are on-off-on type and 5 are on-on type.
i am not having any problem in making them work but the thing is i want the on-off-on toggle to work as 3 switches instead of 2 that is if it is in 0ff position and it should register as a switch i know it cant be done in hardware thats why can it be done in software

here is how it needs to work for example the on-off-on toggle is connected to 10,14 and ground and when 10/14 is not activated it should be registered as a button.

Use an AND on the 2 inputs.

if(input1 && input2) {
   #switch off. Both HIGH
}else{
   #test which input is LOW
}

here is the code

#include <Key.h>
#include <Keypad.h>

#include <Joystick.h>

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, 
25, 0, false, false, false, false, false, false,false, false, false, false, false);


void setup() {
  
  Joystick.begin();
  Serial.begin(38400);

  // Buttons
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  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(16, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);
  pinMode(20, INPUT_PULLUP);
  pinMode(21, INPUT_PULLUP);
}

void loop() {
  Joystick.setButton(1, !digitalRead(0));
  Joystick.setButton(4, !digitalRead(1));
  Joystick.setButton(2, !digitalRead(2));
  Joystick.setButton(17, !digitalRead(3));
  Joystick.setButton(18, !digitalRead(4));
  Joystick.setButton(3, !digitalRead(5));
  Joystick.setButton(0, !digitalRead(6));
  Joystick.setButton(6, !digitalRead(7));
  Joystick.setButton(8, !digitalRead(8));
  Joystick.setButton(14, !digitalRead(9));
  Joystick.setButton(11, !digitalRead(10));
  Joystick.setButton(13, !digitalRead(16));
  Joystick.setButton(9, !digitalRead(14));
  Joystick.setButton(12, !digitalRead(15));
  Joystick.setButton(16, !digitalRead(18));
  Joystick.setButton(7, !digitalRead(19));
  Joystick.setButton(19, !digitalRead(20));
  Joystick.setButton(20, !digitalRead(21));
  delay(50);
}

can you explain pls i posted my code above

in the loop

int input2 = digitalRead(2)
int input3 = digitalRead(3)
if(input2 && input3){
  #do off stuff
}else{
   if(!input2){
      #do input2 stuff
   }else{
      #do input3 stuff
   }
}

Easier to understand

int input2 = digitalRead(2)
int input3 = digitalRead(3)
if(input2 == HIGH && input3 == HIGH){
  #do off stuff
}else{
   if(input2 == LOW){
      #do input2 stuff
   }else{
      #do input3 stuff
   }
}

And to play by all the rules, and get paid for using more characters

bool input2 = digitalRead(2) == HIGH;
bool input3 = digitalRead(3) == HIGH;

if (input2 == true && input3 == true) {    // always kills me to see this kinda stuff
   // do off stuff
} else {
   if (input2 == false) {  // haha, or input2 != true
      // do input2 stuff
   } else {
      // do input3 stuff
   }
}

and to make sense and undo the pulled up backwards reading

# define PRESST LOW

//... 

bool input2 = digitalRead(2) == PRESST;
bool input3 = digitalRead(3) == PRESST;

if (!input1 && !input2) {
   // do off stuff
} else {
   if (input2) {
      // do input2 stuff
   } else {
      // do input3 stuff
   }
}

or this

# define PRESST LOW

//... 

bool input2 = digitalRead(2) == PRESST;
bool input3 = digitalRead(3) == PRESST;

  if (input1 && !input2) {
    // do input1 stuff
  }

  if (input2 && !input1) {
    // do input2 stuff
  }

  if (!input1 && !input2) {
   // do off stuff
  }
}

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.