I have a MIDI project using switches, potentiometers and piezo-electrics paired with Teensy-LC. Things are going well.
I am using 74HCT4051 multiplexers to read these components, and I have had great results.
Previously, using Teensy 2.0, switches and piezos, I was generating MIDI Control Change values of 0 or 1 using the Bounce library.
I need to understand how to use the Bounce/Bounce2 library with the 74HCT4051.
Here's my example code, it does not work. The idea is to generate "1" when the buttons are pressed or the piezos generate a rising voltage, and a "0" when the voltage is falling. But I am getting neither 0 or 1.
I also realize that I need more practice working with arrays in Arduino.
I am using 74HCT4051 multiplexers to read these components, and I have had great results.
Previously, using Teensy 2.0, switches and piezos, I was generating MIDI Control Change values of 0 or 1 using the Bounce library.
I need to understand how to use the Bounce/Bounce2 library with the 74HCT4051.
Here's my example code, it does not work. The idea is to generate "1" when the buttons are pressed or the piezos generate a rising voltage, and a "0" when the voltage is falling. But I am getting neither 0 or 1.
I also realize that I need more practice working with arrays in Arduino.
Code:
#include <Bounce2.h>
# define BOUNCE_LOCK_OUT
// variable definition for 4051
int r0 = 0; // setting initial value of 4051 pin (s0)
int r1 = 0; // setting initial value of 4051 pin (s1)
int r2 = 0; // setting initial value of 4051 pin (s2)
int count = 0; // which pin we are selecting while reading 4051 inputs
int old_time = 0; // time since powered on
// asssign Input pin
int digitalPin = 3;
// initiate Bounce
Bounce debouncer_piezos[8] = Bounce();
// define the s0, s1, s2 pins on Teensy
int s0 = 0;
int s1 = 1;
int s2 = 2;
// setup arduino pins
void setup() {
pinMode(s0, OUTPUT); // s0
pinMode(s1, OUTPUT); // s1
pinMode(s2, OUTPUT); // s2
pinMode(digitalPin, INPUT); // set digitalPin to INPUT
debouncer_piezos[8].attach(digitalPin);
debouncer_piezos[8].interval(5);
}
void loop() {
for(count = 0; count <= 7; count++) { // "count = 0" initializes for loop,
// "count <= 7" tests,
// "count++" increments / decrements
debouncer_piezos[count].update();
// SELECT THE BIT
// reads bit of a number, in this case "count",
// it reads the binary equivalent of count (count = 1 --> r2r1r0 = 001)
// "0" tells bitRead to read the right most bit
// "1" tells bitRead to read the first bit form the right
// "2" tells bitRead to read the second bit form the right
r0 = bitRead(count, 0);
r1 = bitRead(count, 1);
r2 = bitRead(count, 2);
// WRITE TO DIGITAL PINS
digitalWrite(s0, r0); // teensy pin 0, s0 on 4051
digitalWrite(s1, r1); // teensy pin 1, s1 on 4051
digitalWrite(s2, r2); // teensy pin 2, s2 on 4051
delayMicroseconds(75); // once the pin is selected, allow some time
// for the signal to stabalize
// READ MULTIPLEXED PIN
if ( debouncer_piezos[count].rose() ) {
usbMIDI.sendControlChange(count+1, 1, 1);
}
if ( debouncer_piezos[count].fell() ) {
usbMIDI.sendControlChange(count+1, 0, 1);
}
// simple counter that confirms teensy is communicating with Pure Data
int new_time = millis();
if ( new_time - old_time >= 1000 ) {
usbMIDI.sendControlChange(100, new_time/1000, 1);
}
}
}