Working Code for 8pins into 1 using 4051 Mux and teensy 2.0 - MIDI notes via Buttons

Status
Not open for further replies.
Here is a code i got working thanks to PaulStoffregen and nlecaude :D

Its basically 8 pushbuttons sending separate midi notes through a 4051 mux using pin 10 on Teensy 2.0

U can see the wiring here :https://www.pjrc.com/teensy/td_midi.html

Button 1 to pin 1 on 4051
Button 2 to pin 2 on 4051
Button 3 to pin 4 on 4051
Button 4 to pin 5 on 4051
Button 5 to pin 12 on 4051
Button 6 to pin 13 on 4051
Button 7 to pin 14 on 4051
Button 8 to pin 15 on 4051


Code:
int ax = 3; //address pins
int bx = 2; //address pins
int cx = 1; //address pins

int b0 = 0; //button values
int b1 = 0; //button values
int b2 = 0; //button values
int b3 = 0; //button values
int b4 = 0; //button values
int b5 = 0; //button values
int b6 = 0; //button values
int b7 = 0; //button values


int b0_prev = 1; // previous button values
int b1_prev = 1; // previous button values
int b2_prev = 1; // previous button values
int b3_prev = 1; // previous button values
int b4_prev = 1; // previous button values
int b5_prev = 1; // previous button values
int b6_prev = 1; // previous button values
int b7_prev = 1; // previous button values


void setup(){
Serial.begin(9600);

pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(10, INPUT_PULLUP);

}

void loop(){



digitalWrite(ax, HIGH);
digitalWrite(bx, LOW);
digitalWrite(cx, LOW);
delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
b1 = digitalRead(10);

if (b1 == LOW && b1_prev == HIGH) {
  // signal just went low
  usbMIDI.sendNoteOn(60, 110, 1);
  
}
if (b1 == HIGH && b1_prev == LOW) {
  // signal just went high
  usbMIDI.sendNoteOff(60, 110, 1);
 
}
 b1_prev = b1;

digitalWrite(ax, HIGH);
digitalWrite(bx, HIGH);
digitalWrite(cx, LOW);
delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
b3 = digitalRead(10);

if (b3 == LOW && b3_prev == HIGH) {
  // signal just went low
  usbMIDI.sendNoteOn(61, 110, 1);
 
}
if (b3 == HIGH && b3_prev == LOW) {
  // signal just went high
  usbMIDI.sendNoteOff(61, 110, 1);
  
}
 b3_prev = b3;

digitalWrite(ax, HIGH);
digitalWrite(bx, HIGH);
digitalWrite(cx, HIGH);
delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
b7 = digitalRead(10);

if (b7 == LOW && b7_prev == HIGH) {
  // signal just went low
  usbMIDI.sendNoteOn(62, 110, 1);
 
}
if (b7 == HIGH && b7_prev == LOW) {
  // signal just went high
  usbMIDI.sendNoteOff(62, 110, 1);
  
}
 b7_prev = b7;

digitalWrite(ax, HIGH);
digitalWrite(bx, LOW);
digitalWrite(cx, HIGH);
delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
b5 = digitalRead(10);

if (b5 == LOW && b5_prev == HIGH) {
  // signal just went low
  usbMIDI.sendNoteOn(63, 110, 1);
 
}
if (b5 == HIGH && b5_prev == LOW) {
  // signal just went high
  usbMIDI.sendNoteOff(63, 110, 1);
  
}
 b5_prev = b5;
 

digitalWrite(ax, LOW);
digitalWrite(bx, HIGH);
digitalWrite(cx, HIGH);
delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
b6 = digitalRead(10);

if (b6 == LOW && b6_prev == HIGH) {
  // signal just went low
  usbMIDI.sendNoteOn(64, 110, 1);
 
}
if (b6 == HIGH && b6_prev == LOW) {
  // signal just went high
  usbMIDI.sendNoteOff(64, 110, 1);
  
}
 b6_prev = b6;
 


digitalWrite(ax, LOW);
digitalWrite(bx, LOW);
digitalWrite(cx, LOW);
delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
b0 = digitalRead(10);

if (b0 == LOW && b0_prev == HIGH) {
  // signal just went low
  usbMIDI.sendNoteOn(65, 110, 1);
 
}
if (b0 == HIGH && b0_prev == LOW) {
  // signal just went high
  usbMIDI.sendNoteOff(65, 110, 1);

  
}
 b0_prev = b0;

digitalWrite(ax, LOW);
digitalWrite(bx, HIGH);
digitalWrite(cx, LOW);
delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
b2 = digitalRead(10);

if (b2 == LOW && b2_prev == HIGH) {
  // signal just went low
  usbMIDI.sendNoteOn(66, 110, 1);
 
}
if (b2 == HIGH && b2_prev == LOW) {
  // signal just went high
  usbMIDI.sendNoteOff(66, 110, 1);
 
}
 b2_prev = b2;


digitalWrite(ax, LOW);
digitalWrite(bx, LOW);
digitalWrite(cx, HIGH);
delayMicroseconds(50);  // allow a brief time for the signal to charge/discharge the input pin through the 4051's resistance
b4 = digitalRead(10);

if (b4 == LOW && b4_prev == HIGH) {
  // signal just went low
  usbMIDI.sendNoteOn(67, 110, 1);
 
}
if (b4 == HIGH && b4_prev == LOW) {
  // signal just went high
  usbMIDI.sendNoteOff(67, 110, 1);
  
}
 b4_prev = b4; 
 


while (usbMIDI.read()); // read and discard any incoming MIDI messages
delay(25); 

}

If there are mistakes in the code its because i am very much a beginner, and I welcome any good advice to make the coding easier or better.

I hope this helps someone out there so they don't have to sit awake all night like i just did ! LOL
 
Status
Not open for further replies.
Back
Top