Digital inputs send a binary signal to the computer, which we can think of as ON or OFF. Digital Inputs are good for triggering events (like a sound or video file) or playing a key (like on a keyboard) or changing the state of something. Here we will connect a pushbutton and a switch, the most common types of digital inputs.
This picture above shows how to a momentary button. The button pictured above is a pretty standard button with two legs, but buttons and switches come in all kinds of shapes and forms — the connection method remains the same! Because the button is simply making and breaking a connection, it doesn’t matter which wire goes to which leg. Just attach one leg to ground (GND) and one leg to pin 5. Here is what the code looks like for a digital input…
// Instantiate an array of NoteButton objects that send
// MIDI note events when a push button or toggle is pressed/released
NoteButton buttons[]{
{5, MIDI_Notes::C(4)}, // Push button on pin 5
{6, MIDI_Notes::D(4)}, // Toggle switch on pin 6
};
In the code, first we create a NoteButton object and then we say that we are going to create a bunch of them by creating a button array — buttons[]. All of this initial code will stay the same, regardless of how many buttons, switches, or other digital inputs you add! On the next line we specify a pin number, 5, and tell the arduino what to do when pin 5 is connected to ground, meaning the button is pressed. In this case, the arduino sends a message to play MIDI note C4. So, if you have a midi track enabled in a DAW, you will hear note C4. In Max, you can use this note information to play a note, but you can also use it to start a process or to turn something on and off!
This next example shows how to connect a button and a toggle switch, both digital inputs. Note that the toggle switch has 3 legs, not two like the button. By toggling the switch back and forth, you are connecting the middle leg to one or the other outer legs. So, when you connect a toggle switch, connect one wire to the middle leg and one wire to one of the outside legs. Also, not that instead of connecting both components directed to ground, I have connected the ground legs together, so that I only need to connect to ground on the board once!
The code above works with both of these digital inputs — the outer leg of the toggle switch is connected to pin 6, and the middle leg is connected to ground through the ground leg on the button. Try to add more digital inputs to your interface. Also try other kinds of digital switches — tilt switches are fun, but you could also simply connect and disconnect the two wires, allowing for all kinds of sculptural possibilities.