I'm lost on this. Here is my code, so I'd appreciate some help with this:
//#define DEBUG
#include <ShiftRegisterDataCollect.h>
//number of shift registers
#define NUMBER_OF_SHIFT_REGISTERS 5
//shift register pins
#define CLOCK_PIN 7
#define LATCH_PIN 8
#define DATA_PIN 9
//array of first 8 basic 2^n values
int arrayOfBasicValues[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
//array of sum of basic values
int arrayOfSum[8*NUMBER_OF_SHIFT_REGISTERS] = { 0 };
//array of previous values
boolean arrayOfButtonChange[8*NUMBER_OF_SHIFT_REGISTERS] = { false };
//array of MIDI note values
int arrayOfMIDInotes[8] = { 30, 31, 32, 33, 34, 35, 36, 37 };
//for loop variable
int i = 0;
//Define variables to hold the data for each shift register.
byte switchVar[NUMBER_OF_SHIFT_REGISTERS] = {0};
ShiftRegisterDataCollect shiftData(LATCH_PIN, CLOCK_PIN, DATA_PIN);
void setup() {
//start serial
Serial.begin(9600);
}
void loop() {
shiftData.latchPin();
for (i=0; i<NUMBER_OF_SHIFT_REGISTERS; i++) {
//get values from shift register
switchVar[i] = shiftData.shiftInFunc();
//break result to set of basic values and then generate MIDI notes
resultBreak(switchVar[i], i);
}
}
int resultBreak (int readData, int shiftRegisterNumber) {
int j = 0;
int n = 0;
for (j=0; j<8; j++) {
//first part of the loop. get basic values
if (readData >= arrayOfBasicValues[j]) {
arrayOfSum[j+8*shiftRegisterNumber] = arrayOfBasicValues[j];
readData -= arrayOfBasicValues[j];
} else arrayOfSum[j+8*shiftRegisterNumber] = 0;
//second part of the loop, send midi values
if (arrayOfSum[j+8*shiftRegisterNumber] != 0 && arrayOfButtonChange[j+8*shiftRegisterNumber] == false) {
sendMIDI(arrayOfSum[j+8*shiftRegisterNumber], shiftRegisterNumber);
//true indicates that the button is pressed so that the code sends only one MIDI note while button is pressed
arrayOfButtonChange[j+8*shiftRegisterNumber] = true;
}
//if the button isn't pressed anymore, set value in arrayOfButtonState to false
if (arrayOfSum[j+8*shiftRegisterNumber] == 0) arrayOfButtonChange[j+8*shiftRegisterNumber] = false;
}
}
void sendMIDI (int value, int shiftRegisterNumber) {
int i;
char note;
for (i=0; i<8; i++) {
//find the correct value in array of basic values and generate note code
if (value == arrayOfBasicValues[i]) { note = arrayOfMIDInotes[i] + (8*shiftRegisterNumber); break; }
}
#ifdef DEBUG
Serial.print("button: ");
Serial.print (value);
Serial.print("\n");
Serial.print("register number: ");
Serial.print (shiftRegisterNumber + 1);
Serial.print("\n");
#else
Serial.write(0x90);
Serial.write(note);
Serial.write(0x45);
#endif
}