Hi,
I'm unable to use the mux WCMCU-4051 with the teensyLC (tried with 2 different board).
Works perfectly with a Teensy 4.0 or 4.1.
What am I missing ?
Thank you for your help.
I'm unable to use the mux WCMCU-4051 with the teensyLC (tried with 2 different board).
Works perfectly with a Teensy 4.0 or 4.1.
What am I missing ?
Thank you for your help.
Code:
#include <Bounce.h>
const byte DEBOUNCE_TIME = 50;
const byte MULTIPLEXER_SWITCH_CHANNEL_DELAY = 50;
const byte MULTIPLEXER_IO_COUNT = 8;
const byte PIN_MULTIPLEXER_PIN_CHANNEL_1 = 0;
const byte PIN_MULTIPLEXER_PIN_CHANNEL_2 = 1;
const byte PIN_MULTIPLEXER_PIN_CHANNEL_3 = 2;
const byte PIN_MULTIPLEX_BUTTONS = 3;
const byte PIN_BUTTON_A = 4;
Bounce buttons[MULTIPLEXER_IO_COUNT] = {
Bounce (PIN_MULTIPLEX_BUTTONS, DEBOUNCE_TIME), //BUTTON 1
Bounce (PIN_MULTIPLEX_BUTTONS, DEBOUNCE_TIME), //BUTTON 2
Bounce (PIN_MULTIPLEX_BUTTONS, DEBOUNCE_TIME), //BUTTON 3
Bounce (PIN_MULTIPLEX_BUTTONS, DEBOUNCE_TIME), //BUTTON 4
Bounce (PIN_MULTIPLEX_BUTTONS, DEBOUNCE_TIME), //BUTTON 5
Bounce (PIN_MULTIPLEX_BUTTONS, DEBOUNCE_TIME), //BUTTON 6
Bounce (PIN_MULTIPLEX_BUTTONS, DEBOUNCE_TIME), //BUTTON 7
Bounce (PIN_MULTIPLEX_BUTTONS, DEBOUNCE_TIME) //BUTTON 8
};
elapsedMicros multiplexerSwitchChannelTimeEllapsed;
byte currentMultiplexerChannel = 0;
Bounce buttonA = Bounce(PIN_BUTTON_A, DEBOUNCE_TIME * 2);
void setup() {
Serial.begin(31250);
pinMode(PIN_MULTIPLEX_BUTTONS, INPUT_PULLUP);
pinMode(PIN_BUTTON_A, INPUT_PULLUP);
}
void loop() {
if (multiplexerSwitchChannelTimeEllapsed > MULTIPLEXER_SWITCH_CHANNEL_DELAY) {
checkMultiplexButton(currentMultiplexerChannel);
selectNextPinMultiplexChannel();
}
checkButtonA();
}
void checkMultiplexButton(byte multiplexerChannel) {
buttons[multiplexerChannel].update();
if (buttons[multiplexerChannel].fallingEdge()) {
print("PRESSED : " + String(multiplexerChannel));
} else if (buttons[multiplexerChannel].risingEdge()) {
print("RELEASED : " + String(multiplexerChannel));
}
}
void selectNextPinMultiplexChannel() {
currentMultiplexerChannel += 1;
if (currentMultiplexerChannel > MULTIPLEXER_IO_COUNT) {
currentMultiplexerChannel = 0;
}
digitalWrite(PIN_MULTIPLEXER_PIN_CHANNEL_3, HIGH && (currentMultiplexerChannel & B00000100));
digitalWrite(PIN_MULTIPLEXER_PIN_CHANNEL_2, HIGH && (currentMultiplexerChannel & B00000010));
digitalWrite(PIN_MULTIPLEXER_PIN_CHANNEL_1, HIGH && (currentMultiplexerChannel & B00000001));
multiplexerSwitchChannelTimeEllapsed = 0;
}
void checkButtonA() {
buttonA.update();
if (buttonA.fallingEdge()) {
print("BUTTON A PRESSED");
} else if (buttonA.risingEdge()) {
print("BUTTON A RELEASED");
}
}
void print(String s) {
Serial.println(s);
}