Modifier Switch

Hi.

I’m planning to build a button box with 30+ functions, but in order to keep the number of physical buttons to a minimum (and maybe circumvent the Windows limitation of 32 buttons), I’d like to know it’s possible to add a toggle switch to act as a modifier.

Example,

toggle down
button 1 = print “A”;
button 2 = print “B”.

toggle up
button 1 = print “+”.
button 2 = print “-”

Is this possible (also read as “simple enough”)?

Cheers

Assuming that this is an Arduino question, the answer is yes.

Yes, this is an Arduino question. I’m planning to use a Leonardo or duo (in case I include a thumb joystick).

So, using a button matrix, say 3x3 and one of the available scripts online, how would this toggle be included? Would it be included as a separate button and identified as some kind of modifier? Or would it switch to a different matrix binding?

Cheers

either - or, but the toggle on a different pin and software from thereon sounds simpler, imagine 2x 3x3 matrices :wink:

How would that be coded?

One of the concepts of the Arduino system is that you get to do everything yourself: conceptualizing, planning, building hardware, defining software, coding, testing. Perhaps it has been made too easy but the developers have tried to make it work for hardware people, software people, artists, dreamers, etc.

This forum is intended for people who have written code but are having a problem and want a little help. They describe their system, show their wiring, properly post their code, describe their problem (intended results versus actual results), and they request assistance.

Yes, there are people who will write code for you. They are mostly located in the “Gigs and Collaborations” forum. You should try there, and bring your wallet.

I'm not asking for someone to code the whole thing for me. I'm asking for guidance on the toggle switch alone and how it would bind to a different matrix.

Calado:
I'm asking for guidance on the toggle switch alone and how it would bind to a different matrix.

You've given yourself the answer. Decode the matrix then look at the toggle SW. If switch is on do stuff for 'A' matrix keys else...

But that’s a question. I haven’t answered myself.

I can’t figure out how to do it. I’ve read about matrixes and how they work, but I don’t know how to make the swap.

Using the basic keypad tutorial in a 4x3 matrix and the toggle connected to pin 9, how do I make an “if…else” statement to make this

#include <Keypad.h>

int togglePin = 9;  // //// this would be the toggle SW
int state = HIGH;  

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup(){
  Serial.begin(9600);
}

void loop(){
  char key = keypad.getKey();

  if (key != NO_KEY){
    Serial.println(key);
  }

into this

const byte ROWS2 = 4; //four rows
const byte COLS2 = 3; //three columns
char keys2[ROWS2][COLS2] = {
  {'r','t','y'},
  {'q','w','e'},
  {'a','s','d'},
  {'z','x','c'}
};
byte rowPins[ROWS2] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS2] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys2), rowPins, colPins, ROWS2, COLS2 );


void setup(){
  Serial.begin(9600);
}

To my understanding, it’s something like

void setup()
{
  pinMode(togglePin,  INPUT);
  }

void loop()
{
  reading = digitalRead(togglePin);

   {
    if (state == HIGH)  // ////use Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
     
    else   // //// use Keypad( makeKeymap(keys2), rowPins, colPins, ROWS2, COLS2 );
}

But how?

Edit: orthography correction

You don't need two matrices, but two ways of interpreting one matrix.

getKeyCode(); // call a function to read the keyboard

if(switchIsOn){
  // do 'A' stuff using keycode
}
else{ // switch is off
 // do 'B' stuff using keycode
}

dougp:
You don't need two matrices, but two ways of interpreting one matrix.

I can't find online clarification on this. So now I'm very lost :slight_smile:

Well how do you interpret the matrix ? you do that using the library, so yes you'd have to either make a separate keymap & create another object, or convert the resulting key into something else if the modifier switch is pressed.
i would try to do it making 2 objects or an array of objects. and call .getKey() for the different objects.

I have no idea how to do that. I guess I need to do a lot of reading before I can pull this off. Maybe I'll just lose the minimalistic design and add a lot of buttons

if you can describe exactly how it should work, the you just code what you described. Start coding in english, line by line. Each step, give it a name. Don't worry about "coding", 99.999% of the problem will always be getting yourself to get the solution straight in you own head.

-jim lee

Calado:
I have no idea how to do that. I guess I need to do a lot of reading before I can pull this off. Maybe I'll just lose the minimalistic design and add a lot of buttons

you are already nearly there !

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys1[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
char keys2[ROWS][COLS] = {
  {'r','t','y'},
  {'q','w','e'},
  {'a','s','d'},
  {'z','x','c'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keyp1 = Keypad( makeKeymap(keys1), rowPins, colPins, ROWS, COLS );
Keypad keyp2 = Keypad( makeKeymap(keys2), rowPins, colPins, ROWS, COLS );

please figure out the rest yourself, before i get in trouble with the moderator.

Hi.

Would this be it? Code isn’t returning errors, but only the second matrix works alone. If the toggle is set for the first matrix, it outputs both matrices at the same time.

Example

Toggle ON (Matrix 2)
a
a
a
a

Toggle OFF (Matrix 1)
a
7
a
7
a
7
#include <Keypad.h>
#include <Keyboard.h>

int ledPin = 13;
int togglePin = 9;  // toggle SW
int val = 0;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys1[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'#', '0', '*'}
};
char keys2[ROWS][COLS] = {
  {'r', 't', 'y'},
  {'q', 'w', 'e'},
  {'a', 's', 'd'},
  {'z', 'x', 'c'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keyp1 = Keypad( makeKeymap(keys1), rowPins, colPins, ROWS, COLS );
Keypad keyp2 = Keypad( makeKeymap(keys2), rowPins, colPins, ROWS, COLS );


void setup() {
  Serial.begin(9600);
}

void loop() {




  if (digitalRead(togglePin) == HIGH) {
    char key = keyp1.getKey();
    if (key != NO_KEY) {
      Serial.println(key);
      Keyboard.press(key);
      delay(150);
      Keyboard.release(key);
    }
  }
  if (digitalRead(togglePin) == LOW) {
    char key = keyp2.getKey();
    if (key != NO_KEY) {
      Serial.println(key);
      Keyboard.press(key);
      delay(150);
      Keyboard.release(key);
    }
  }
}

More or less yeah, i would use 'else' instead of a second if statement, also i have no idea how the in can be both high & low at the same time. are you using a pullup or pulldown resistor otherwise use a INPUT_PULLUP (and let the switch go down. Think of a way to test that it is really not a side effect of have 2 objects.

How is the switch wired to pin 9? I'm suspecting a floating pin.

Deva_Rishi:
Think of a way to test that it is really not a side effect of have 2 objects.

Yea, without being totally familiar with the inner workings of the keypad library, I’d be concerned about strange behavior from having to objects working the same set of physical row/column wires.

I don’t have those libraries installed, so I can’t compile. But, maybe something like this:

#include <Keypad.h>
#include <Keyboard.h>

int ledPin = 13;
int togglePin = 9;  // toggle SW
int val = 0;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns

char physicalKeys[ROWS][COLS] = {
  {1, 2, 3},
  {4, 4, 6},
  {7, 8, 9},
  {10, 11, 12}
};

char keys1[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'#', '0', '*'}
};
char keys2[ROWS][COLS] = {
  {'r', 't', 'y'},
  {'q', 'w', 'e'},
  {'a', 's', 'd'},
  {'z', 'x', 'c'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keyp1 = Keypad( makeKeymap(physicalKeys), rowPins, colPins, ROWS, COLS );

void setup() {
  Serial.begin(9600);
}

void loop() {
  char *ptr;
  if (key != NO_KEY) {
    if (digitalRead(togglePin) == HIGH) {
      ptr = (char *)keys1;
    } else {
      ptr = (char *)keys2;
    }

    key = *(ptr + key - 1);
    Serial.println(key);
    Keyboard.press(key);
    delay(150);
    Keyboard.release(key);
  }
}

actually to test it, changing the 2nd 'if' statement to an 'else' (without any condition), we could check the library, but the definition of the the object should be that it has it's own variables keyb1.rows is not the same as keyb2.rows other than that they have the same value. Also the function .getkey() only get called once per part and only returns 1 value per time. It shouldn't be issue. I was thinking if we'd want more than 2 objects, we should create an array.. and then i wasn't sure, of objects ? or of pointers to objects ? anyway that is beyond this post.