Where to start advice. | Making a macro key switch board with toggle switches.

Hello there!

I would love to jump straight into my inquiry, but to hopefully make this easier to understand I am first going to give you some backstory. I have recently gotten into Arduino, for a variety of projects, however, I have very little experience with Arduino or coding in general, though I have a decent knowledge of electrical systems. That in mind, if you want to help, you are going to need some patience with me.

Ok, so moving onto my problem. I do all my work on my pc and dabble in flight simulators here and there, so I need a way to make macro shortcuts, but I much prefer the tactile feel of toggle switches as opposed to keyboard strokes. So what I have done, is I have a panel of 12 momentary buttons and 16 toggle switches. (I feel it is important to mention I am working with an Arduino Nano.) All I want to do is set up a matrix that would allow me to have the buttons assigned to various keys on the keyboard. Also, I have done some research and I have discovered that it is possible to assign functions to toggle switch position updates, so as to make a toggle switch virtually press a button whenever its position is changed (on/off). If someone could point me in the right direction, that would be absolutely wonderful!

I also wanted to mention that I am by no means knowledgeable to come up with the code all on my own, and I am not sure how long it would take for me to learn up to the point required to write the code, but I hope you can all understand that I don't want to learn Arduino ide for months just for two or three projects, that does not mean I'm not willing to learn what I would need to do for this project though. :slight_smile:

Thanks so much in advance, and sorry for the short novel, just wanted to be as detailed as possible! Let me know if you need any more information.

~Blake

Google "arduino switch matrix" for many tutorials. For switches wired as in a typical keypad matrix, there is a specific library that handles the scanning and assigns values to keys.

It sounds like you want messages to go to the PC. For that, you can have the Arduino emulate a PC keyboard, and as you might have guessed, google "arduino emulate PC keyboard" for examples.

Or just send messages using Serial.print() and have a program running on the PC to handle the serial port input.

If you want to do keyboard emulation to send the shortcut keys directly to your computer just as if the Arduino was a keyboard, the Nano is not well suited. Instead, I recommend you to use an Arduino board that has native USB capabilities like the Pro Micro, Leonardo, Micro, MKR boards, Teensy boards.

If you want your Nano to act as a keyboard, you're basically using the wrong Arduino. Have a look at the Arduino Micro or e.g. Sparkfun Pro Micro or Pololu A-Star 32U4 Micro.

As mentioned by jremington, the alternative is to write code for the PC to handle serial and convert to keystrokes. I did find Download Serial2Keyboard from SourceForge.net, but I can't easily find the source code; I did not test it.

Note
Just running that exe without checking can be dangerous :wink:

Here's another free open source option for mapping serial input to keyboard emulation:
http://eventghost.net/
You should also be able to do it with AutoHotkey:
https://autohotkey.com/
They're both Windows-only but I'm sure there are equivalents for any OS.

I'd much rather just use the correct hardware for the job though. Otherwise you need to rely on the software on your computer running, and running correctly. It's especially annoying when Windows decides to re-enumerate your virtual COM port, breaking your system until you go and update the COM port number in the software.

Look at the "StateChangeDetection" example in your Arduino examples folder. (in 02. Digital)

I echo the comments that the Nano is the wrong tool for this. But it depends on your simulator too. The Teensy can directly interface with flight simulators too. (I'm an xPlane guy, I don't know about others.)

Alright!

I have ordered an Arduino Micro, and have looked into keyboard emulation a bit more.

I will keep you all updated when I mock-up a prototype :slight_smile:

Hello Again!

My Arduino Pro Micro has arrived! I plugged it in, uploaded the code below and had no problems.

/*
 * Arduino Keyboard Emulation
 * learnelectronics
 * 13 FEB 2017
 * 
 * www.youtube.com/c/learnelectronics
 */

#include <Keyboard.h>    // This is a "built-in" library no need to install

//---------------------------------------------------------
//                           Setup
//---------------------------------------------------------



void setup() {
pinMode(3,INPUT_PULLUP);  // sets pin 3 to input & pulls it high w/ internal resistor
pinMode(4,INPUT_PULLUP);  // sets pin 4 to input & pulls it high w/ internal resistor
pinMode(5,INPUT_PULLUP);  // sets pin 5 to input & pulls it high w/ internal resistor

Serial.begin(9600);       // begin serial comms for debugging

}

//---------------------------------------------------------
//                           Loop
//---------------------------------------------------------

void loop() {
  
  
 Keyboard.begin();         //begin keyboard 
 if (digitalRead(3) == 0)  // if buton 3 is pushed
  {
    Keyboard.write('A');  // send single character "A"
    delay(200);           // delay so you don't get 20 A's
  }
  else if (digitalRead(4) == 0){  // if button 4 is pressed
    Keyboard.print("The rain in Spain falls mainly on the plain"); // send string
    delay(200);
}
  else if (digitalRead(5) == 0){  //if button 5 is pressed
    Keyboard.write(0xB0);         // send Hex value for RETURN key
    delay(200);
  }
  Keyboard.end();                 //stops keybord
}

Now to move on with the project at hand. I have two questions. First and what I believe to be the most simple, how do I implement the previously mentioned “StateChangeDectection” into this code? My desired effect is when a toggle switch is on, it inputs to press a key, for example, “a”, once, then when turned off, it sends the input again writing “a” once. Secondly, how would I go about making a matrix with a total of 12 momentary push buttons and 16 toggle switches?

Again, thanks so much!
If you need to know anything else, just ask :slight_smile:

~Blake

(p.s. Attached is a photo of my switches. In red, are the momentary switches and in green are the toggle switches. The small blue toggle switches are on/off/on but I would only wire them for one of the inputs, (on/off).)

Secondly, how would I go about making a matrix with a total of 12 momentary push buttons and 16 toggle switches?

That is 28 switches total, so a 7x4 matrix would be convenient. That could be 7 outputs and 4 inputs, or vice versa. Decide and draw out the grid and wire it up, following one of the examples on line.

Example, with three outputs (top) and three inputs (bottom) with INPUT_PULLUP resistors:

One tutorial and another

WARNING: If more than one switch is to be closed at any one time, some circuits may not detect that condition properly. The above one, using the diodes, will detect a situation with two or more switches closed.

jremington, thanks for the response!
I understand the basic concept of a matrix, the problem I have is that I want Only my 16 toggle switches to use a "StateChangeDetection" system, while the momentary switches to use INPUT_PULLUP in order to only send an input when pressed down, whereas the "StateChangeDetection" would input once when the button is pushed down, and once when released. I am not sure if this would work on a normal matrix setup.

~Blake

I am not sure if this would work on a normal matrix setup.

Your particular plan may not work, but the use of a matrix does not constrain you in any way, while dramatically reducing the number of I/O pins required.

Your program could simply take different courses of action for different groups of buttons or switches, when press or release is detected.

Hello, jremington!

I did some research and came across this code:

Now this is just about perfect for what I need, however, I still don't know how to modify it so that A.) I can replace the encoders with switches, as I have no encoders on my board. And B.) How to split the matrix into two groups, toggle switches, and momentary buttons, so that I can find some way to have the toggle switches act the way I want them to. Is this even possible?

Thanks so much,
~Blake

Toggle switches are no different in principle from pushbuttons, except that you don't have to keep pushing a toggle switch.

The first step is to describe in English exactly what you want to happen when either type of switch is activated, remains activated, or is deactivated.

If you are using Microsoft FS, there is a neat little program that connects Arduino to FS9 or FSX.

http://www.jimspage.co.nz/intro.html

It is as easy as:

To raise the gear

Serial.println("C01");

To lower the gear

Serial.println("C02");

jremington:
The first step is to describe in English exactly what you want to happen when either type of switch is activated, remains activated, or is deactivated.

My bad, I keep getting ahead of my self.

When a momentary switch is pushed, I want it to do this:

Keyboard.write('A');  // send single character "A"
    delay(200);           // delay so you don't get 20 A's

That much is solved.

My problem is, I don't know how to make a toggle switch do this once when activated, and once when deactivated. When the toggle switch is activated, I want it to send the character once, without repeating while remaining active. Then when it is deactivated, I want it to send the same character again, once, without repeating.

Thanks, hope this is more clear! :slight_smile:

The toggle switches are easy. Just associate a state with each switch, e.g. a byte or boolean variable named swA_state, that has the value true (1) or false (0) for closed or open.

Each time you scan the keyboard, check the switch, compare the actual state with the stored state, and take appropriate action if there is a change. Action would include updating the stored state.
'
You do not want to use delay(), ever, as that will interfere with keyboard scanning and make the switches seem unresponsive.

You instead need to learn how to use millis() to time events, and there are several tutorials on this site (check the programming section) for how to do that. A good starting point is to study the Blink without Delay programming example.

You will have to forgive me, but while I understand the concept of what you are saying, I have no clue where to start implementing this into code.

So for instance, is it possible to take the previously mentioned code and modify it?

Blake-Cobalt:
I did some research and came across this code:
http://docs.wixstatic.com/ugd/f163af_04c319f77bd146d69c596f44832f4edf.pdf

Or would I have to completely scrap this and start fresh?

Thanks!

Or would I have to completely scrap this and start fresh?

That is by far the best course of action, and you will (1) end up with exactly what you want and (2) learn a great deal in the process.

One step at a time.

First learn to blink an LED without using delay(), then you might move on to implementing a simple keypad, using the standard matrix wiring and the Arduino keypad library. Your pushbutton array would be fine as the "keypad".

Thank you jremington!

I guess I have a lot of work to do then!

~Blake