MIDI control for 56 buttons on 7 4021 mux.


hi guys!
i'm writing code midi controller i'm building.
i'd have improve code, make faster , better, best way it's possible!
i have arduini midi out connection, sending out notes 56 buttons connected 7 4021 multiplexers.
the code made works , i've it's enough easy handle adding multiplexers or change notes.
but i'm asking if there's way make better.
thanks in advance coming!

code:
[size=10]
#include <midi.h>

// ******************************************
// variables definitions
// ******************************************

// *********************
// buttons multiplexing variables
// definition of pins
int buttonsmuxclockpin = 7;
int buttonsmuxlatchpin = 8;
int buttonsmuxdatapin = 9;

// define variables hold data
// each shift register.
byte muxbuttons1 = 0;
byte muxbuttons2 = 0;
byte muxbuttons3 = 0;
byte muxbuttons4 = 0;
byte muxbuttons5 = 0;
byte muxbuttons6 = 0;
byte muxbuttons7 = 0;

// general midi notes
char buttonsmuxnotes1[] = {0, 1, 2, 3, 4, 5, 6, 7};
char buttonsmuxnotes2[] = {8, 9, 10, 11, 12, 13, 14, 15};
char buttonsmuxnotes3[] = {0, 1, 2, 3, 4, 5, 6, 7};
char buttonsmuxnotes4[] = {93, 94, 95, 96, 97, 98, 99, 100};
char buttonsmuxnotes5[] = {0, 1, 2, 3, 4, 5, 6, 7};
char buttonsmuxnotes6[] = {75, 76, 77, 78, 79, 90, 91, 92};
char buttonsmuxnotes7[] = {8, 9, 10, 11, 12, 13, 14, 15};

// status of each button of mux
boolean buttonsmuxstatus1[] = {0,0,0,0,0,0,0,0};
boolean buttonsmuxstatus2[] = {0,0,0,0,0,0,0,0};
boolean buttonsmuxstatus3[] = {0,0,0,0,0,0,0,0};
boolean buttonsmuxstatus4[] = {0,0,0,0,0,0,0,0};
boolean buttonsmuxstatus5[] = {0,0,0,0,0,0,0,0};
boolean buttonsmuxstatus6[] = {0,0,0,0,0,0,0,0};
boolean buttonsmuxstatus7[] = {0,0,0,0,0,0,0,0};

// midi channel send notes each mux
int buttonsmuxmidichannel1 = 1;
int buttonsmuxmidichannel2 = 1;
int buttonsmuxmidichannel3 = 2;
int buttonsmuxmidichannel4 = 2;
int buttonsmuxmidichannel5 = 8;
int buttonsmuxmidichannel6 = 8;
int buttonsmuxmidichannel7 = 16;

// debounce settings
long muxbuttonstime = 0;         // last time output pin toggled
long muxbuttonsdebounce = 100;   // debounce time, increase if output flickers

// *********************
// set application
// *********************
void setup() {
 // standard serial begin
 // serial.begin(9600);
 // midi serial begin
 serial.begin(31250);

 // buttons mux
 // define pin modes
 pinmode(buttonsmuxlatchpin, output);
 pinmode(buttonsmuxclockpin, output);
 pinmode(buttonsmuxdatapin, input);
}

// ******************************************
// main loop
// ******************************************
void loop() {
 // butons mux
 // function handles digital mux buttons
 startreadingbuttonmux();
}

// ******************************************
// buttons mux
// ******************************************
// activate digital mux buttons
void startreadingbuttonmux() {
 //  pulse latch pin:
 //  set 1 collect parallel data
 digitalwrite(buttonsmuxlatchpin, high);
 // .... collecting data digital inputs of mux ... wait ...
 delaymicroseconds(20);
 // set 0 transmit data serially  
 digitalwrite(buttonsmuxlatchpin, low);

 // while shift register in serial mode
 //collect each shift register byte
 //the register attached chip comes in first
 muxbuttons1 = shiftin(buttonsmuxdatapin, buttonsmuxclockpin);
 muxbuttons2 = shiftin(buttonsmuxdatapin, buttonsmuxclockpin);
 muxbuttons3 = shiftin(buttonsmuxdatapin, buttonsmuxclockpin);
 muxbuttons4 = shiftin(buttonsmuxdatapin, buttonsmuxclockpin);
 muxbuttons5 = shiftin(buttonsmuxdatapin, buttonsmuxclockpin);
 muxbuttons6 = shiftin(buttonsmuxdatapin, buttonsmuxclockpin);
 muxbuttons7 = shiftin(buttonsmuxdatapin, buttonsmuxclockpin);

 analyzebuttonsmux(muxbuttons1, buttonsmuxnotes1, buttonsmuxstatus1, buttonsmuxmidichannel1);
 analyzebuttonsmux(muxbuttons2, buttonsmuxnotes2, buttonsmuxstatus2, buttonsmuxmidichannel2);
 analyzebuttonsmux(muxbuttons3, buttonsmuxnotes3, buttonsmuxstatus3, buttonsmuxmidichannel3);
 analyzebuttonsmux(muxbuttons4, buttonsmuxnotes4, buttonsmuxstatus4, buttonsmuxmidichannel4);
 analyzebuttonsmux(muxbuttons5, buttonsmuxnotes5, buttonsmuxstatus5, buttonsmuxmidichannel5);
 analyzebuttonsmux(muxbuttons6, buttonsmuxnotes6, buttonsmuxstatus6, buttonsmuxmidichannel6);
 analyzebuttonsmux(muxbuttons7, buttonsmuxnotes7, buttonsmuxstatus7, buttonsmuxmidichannel7);
}

// shiftin function
// needs location of data pin , clock pin
// returns byte each bit in byte corresponding
// pin on shift register. leftbit 7 = pin 7 / bit 0= pin 0
byte shiftin(int mydatapin, int myclockpin) {
 int i;
 int temp = 0;
  // int pinstate;
 byte mydatain = 0;

 pinmode(myclockpin, output);
 pinmode(mydatapin, input);

 // holding clock pin high 8 times (0,..,7) @ the
 // end of each time through loop

 // @ begining of each loop when set clock low, will
 // doing necessary low high drop cause shift
 // register's datapin change state based on value
 // of next bit in serial information flow.
 // register transmits information pins pin 7 pin 0
 // why our function counts down
 
 // loop pins of shift reg.
 for (i=7; i>=0; i--)
 {
   digitalwrite(myclockpin, low);
   delaymicroseconds(2);
   temp = digitalread(mydatapin);
   if (temp) {
     // pinstate = 1;
     // set bit 1 no matter what
     mydatain = mydatain | (1 << i);
   }
   else {
     //turn off -- necessary debuging
     //print statement since mydatain starts 0
     // pinstate = 0;
   }

   //debuging print statements
   // serial.print("valore pin: ");
   // serial.print(pinstate);
   // serial.print(" - ");
   //serial.println (datain, bin);
   digitalwrite(myclockpin, high);
 }
 //debuging print statements whitespace
 //serial.println();
 //serial.println(mydatain, bin);
 return mydatain;
}

// function analiyzes data mux
// parameters:
// 1. byte state of 8 pins
// 2. array of notes corresponfding 8 pins
// 3. array off boolean values indicates actual status of each not/pin
// 4. midi channel send midi data 8 notes of mux
void analyzebuttonsmux(byte incomingdata, char* muxnotes, boolean* muxstatus, int midichannel) {
 // loop thru byte
 for (int n=0; n<=7; n++)
 {
   // if 'n' value equals 1
   if (incomingdata & (1 << n) ){
     //print value of array location
     // pin "i" has value 1!
     // if note not playing.....
     if(muxstatus[n] == 0 && (millis() - muxbuttonstime > muxbuttonsdebounce) ) { // debouncing here
       // playmuxonnotes(n, muxnotes, midichannel);
       sendmidi(noteon, muxnotes[n], 120, midichannel);
       muxstatus[n] = 1;
       muxbuttonstime = millis(); // reset debouncing variables
     }
   } else {
     if(muxstatus[n] == 1 && (millis() - muxbuttonstime > muxbuttonsdebounce) ) { // if note playing .... debouncing here
       // playmuxoffnotes(n, muxnotes, midichannel);
       sendmidi(noteoff, muxnotes[n], 120, midichannel);
       muxstatus[n] = 0;
       muxbuttonstime = millis(); // reset debouncing variables
     }
   }
 }
}

// ******************************************
// midi functions
// ******************************************
// send midi note-on/off message.  
void sendmidi(char cmd, char data1, char data2, int midichannel) {
 // based on midi library
 midi.send(cmd, data1, data2, midichannel);
}[/size]






...ps
after have analog/pots part



Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > MIDI control for 56 buttons on 7 4021 mux.


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums