This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
2.DigitalReadSerial.ino

2.DigitalReadSerial

This is an example of the AnalogMultiplex class. It prints the states of all 16 switches connected to a multiplexers to the serial monitor.

Boards:
AVR, AVR USB, Teensy 3.x, ESP32

Connections

Optionally you can connect the enable pin as well, this is useful if you want to use multiple multiplexers with the same address lines and the same analog input. Otherwise, just connect the enable pin to ground.

If you are using a 3-bit multiplexer, like the CD74HC4051, you can uncomment the code specific to this multiplexer, and use only three address pins.

Connect a switch or push button between each input pin of the multiplexer and ground. A pull-up resistor is not necessary, because we'll use the internal one.

Behavior

Open the serial monitor (CTRL+SHIFT+M) or the serial plotter (CTRL+SHIFT+L), and press some buttons, you should see all 16 signals printed or plotted.

Mapping

None.

Written by Pieter P, 2019-08-08
https://github.com/tttapa/Control-Surface

#include <Control_Surface.h> // Include the Control Surface library
// Instantiate a multiplexer
CD74HC4067 mux = {
2, // input pin
{3, 4, 5, 6}, // Address pins S0, S1, S2, S3
// 7, // Optionally, specify the enable pin
};
// Alternatively, if you have a 3-bit mux:
// CD74HC4051 mux = {
// 2,
// {3, 4, 5},
// // 7, // Optional
// };
void setup() {
Serial.begin(115200);
mux.begin(); // Initialize multiplexer
mux.pinMode(0, INPUT_PULLUP); // Set the pin mode (setting it for one pin of
// the multiplexers sets it for all of them)
}
void loop() {
for (pin_t pin = 0; pin < mux.getLength(); ++pin) {
Serial.print(mux.digitalRead(pin));
Serial.print('\t');
}
Serial.println();
}
// Or if you're a cool kid, use a range-based for loop
void loop2() {
for (pin_t pin : mux.pins()) {
Serial.print(digitalRead(pin));
Serial.print('\t');
}
Serial.println();
}
// Okay, it's a bit slower, because it has to look up what ExtIO device the pin
// belongs to, but hey, it's nice to have it anyway
INPUT_PULLUP
const uint8_t INPUT_PULLUP
Definition: ExtendedInputOutput.hpp:39
pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Def.hpp:17
AnalogMultiplex::digitalRead
int digitalRead(pin_t pin) override
Read the digital state of the given input.
Definition: AnalogMultiplex.hpp:161
Control_Surface.h
The main header file that includes all Control-Surface header files.
ExtendedIOElement::getLength
pin_t getLength() const
Get the number of pins this IO element has.
Definition: ExtendedIOElement.cpp:43
AnalogMultiplex
A class for reading multiplexed analog inputs.
Definition: AnalogMultiplex.hpp:25
AnalogMultiplex::begin
void begin() override
Initialize the multiplexer: set the pin mode of the address pins and the enable pin to output mode.
Definition: AnalogMultiplex.hpp:178
ExtIO::digitalRead
int digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:56
AnalogMultiplex::pinMode
void pinMode(UNUSED_PARAM pin_t pin, uint8_t mode) override
Set the pin mode of the analog input pin.
Definition: AnalogMultiplex.hpp:156
StaticSizeExtendedIOElement::pins
Array< pin_t, N > pins() const
Get an array containing all pins of the element.
Definition: StaticSizeExtendedIOElement.hpp:24