Hi, I am trying to infterface 4 - 4021 shift registers that are connected to switches I am using for a 32 note pedalboard. I am not a programmer but I do understand some stuff. I found this code that used 7 - 4021's and I am trying to get it to work for me. can anyone help?
#include <MIDI.h>
// ******************************************
// Variables definitions
// ******************************************// *********************
// Buttons Multiplexing Variables
// Definition of pins
int buttonsMuxClockPin = 7;
int buttonsMuxLatchPin = 8;
int buttonsMuxDataPin = 9;// Define variables to hold the data
// for each shift register.
byte muxButtons1 = 0;
byte muxButtons2 = 0;
byte muxButtons3 = 0;
byte muxButtons4 = 0;// General midi notes
char buttonsMuxNotes1[] = {36, 37, 38, 39, 40, 41, 42, 43};
char buttonsMuxNotes2[] = {44, 45, 46, 47, 48, 49, 50, 51};
char buttonsMuxNotes3[] = {52, 53, 54, 55, 56, 57, 58, 59};
char buttonsMuxNotes4[] = {60, 61, 62, 63, 64, 65, 66, 67};// Status of each button of the 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};// Midi channel where to send notes for each mux
int buttonsMuxMidiChannel1 = 3;
int buttonsMuxMidiChannel2 = 3;
int buttonsMuxMidiChannel3 = 3;
int buttonsMuxMidiChannel4 = 3;// Debounce settings
long muxButtonsTime = 0; // the last time the output pin was toggled
long muxButtonsDebounce = 100; // the debounce time, increase if the output flickers// *********************
// Set up 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 that handles digital mux for buttons
startReadingButtonMux();
}// ******************************************
// Buttons Mux
// ******************************************
// Activate Digital Mux for Buttons
void startReadingButtonMux() {
// Pulse the latch pin:
// Set it to 1 to collect parallel data
digitalWrite(buttonsMuxLatchPin, HIGH);
// .... collecting data from digital inputs of MUX ... wait ...
delayMicroseconds(20);
// Set it to 0 to transmit data serially
digitalWrite(buttonsMuxLatchPin, LOW);// While the shift register is in serial mode
//collect each shift register into a byte
//the register attached to the chip comes in first
muxButtons1 = shiftIn(buttonsMuxDataPin, buttonsMuxClockPin);
muxButtons2 = shiftIn(buttonsMuxDataPin, buttonsMuxClockPin);
muxButtons3 = shiftIn(buttonsMuxDataPin, buttonsMuxClockPin);
muxButtons4 = shiftIn(buttonsMuxDataPin, buttonsMuxClockPin);analyzeButtonsMux(muxButtons1, buttonsMuxNotes1, buttonsMuxStatus1, buttonsMuxMidiChannel1);
analyzeButtonsMux(muxButtons2, buttonsMuxNotes2, buttonsMuxStatus2, buttonsMuxMidiChannel2);
analyzeButtonsMux(muxButtons3, buttonsMuxNotes3, buttonsMuxStatus3, buttonsMuxMidiChannel3);
analyzeButtonsMux(muxButtons4, buttonsMuxNotes4, buttonsMuxStatus4, buttonsMuxMidiChannel4);}
// ShiftIn function
// Just needs the location of the data pin and the clock pin
// it returns a byte with each bit in the byte corresponding
// to a pin on the 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);// we will be holding the clock pin high 8 times (0,..,7) at the
// end of each time through the for loop// at the begining of each loop when we set the clock low, it will
// be doing the necessary low to high drop to cause the shift
// register's DataPin to change state based on the value
// of the next bit in its serial information flow.
// The register transmits the information about the pins from pin 7 to pin 0
// so that is why our function counts down// Loop for all the pins of the shift reg.
for (i=7; i>=0; i--)
{
digitalWrite(myClockPin, LOW);
delayMicroseconds(2);
temp = digitalRead(myDataPin);
if (temp) {
// pinState = 1;
// Set the bit to 1 no matter what
myDataIn = myDataIn | (1 << i);
}
else {
//turn it off -- only necessary for debuging
//print statement since myDataIn starts as 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 that analiyzes the data from the Mux
// Parameters:
// 1. Byte with the state of the 8 pins
// 2. Array of notes corresponfding to the 8 pins
// 3. Array off boolean values indicates the actual status of each not/pin
// 4. Midi channel to send the midi data for the 8 notes of the mux
void analyzeButtonsMux(byte incomingData, char* muxNotes, boolean* muxStatus, int midiChannel) {
// Loop thru the Byte
for (int n=0; n<=7; n++)
{
// if 'n' value equals 1
if (incomingData & (1 << n) ){
//print the value of the array location
// Pin "i" has value 1!
// If that note was not playing.....
if(muxStatus[n] == 0 && (millis() - muxButtonsTime > muxButtonsDebounce) ) { // Some debouncing here
// playMuxOnNotes(n, muxNotes, midiChannel);
sendMidi(NoteOn, muxNotes[n], 120, midiChannel);
muxStatus[n] = 1;
muxButtonsTime =