No - you were literally turning them on and then off in your code:
- Code: Select all
void readSensors(int analogPin){
//This for loop is used to scroll through and store the 16 inputs on the FIRST multiplexer
for (int i=0; i<16; i++){
digitalWrite(CONTROL0, (i&15)>>3);
digitalWrite(CONTROL1, (i&7)>>2);
digitalWrite(CONTROL2, (i&3)>>1);
digitalWrite(CONTROL3, (i&1));
//Read and store the input value at a location in the array
if(analogPin==0){
mux0array[i] = analogRead(analogPin);
}
if(analogPin==1){
mux1array[i] = analogRead(analogPin);
}
if(analogPin==2){
mux2array[i] = analogRead(analogPin);
}
if ((PadNote[i] >= 60) && (PadNote[i] <= 75)) {
mcp.digitalWrite(PadNote[i]-60, HIGH);
}
if ((PadNote[i] >= 60) && (PadNote[i] <= 75)) {
mcp.digitalWrite(PadNote[i]-60, LOW);
}
}
}
So, if PadNote[ i] was, say, 65, it would pass the first test and turn on the light. Then it will immediately pass the next test and turn off the light. So your lights flicker.
PadNote is a contant array - the values in it never change, and it has
nothing to do with the current input. The current input is in mux0array. That's what readSensors() does - it reads your drumpads and puts the current analog value of each pad into muxarray.
The code in checkSensors() really doesn't make any sense to me. Did you get this out of some example code? It looks like it wants to do some sort of average of 8 readings on each pad to try to calculate some sort of touch velocity. After that, it sends the note and velocity to MIDI_TX.
I don't think the hitavg calculation is doing what you want it to do. It doesn't look right to me. It is NOT calculating an average of 8 consecutive readings.
After that, you seem to be trying to compute an ON time for the note. Again, I don't think you're doing this the right way. However, you
do set activePad[i] to true to indicate that the note is playing. Later on, when you think the note has timed out, you send a different midi command and set activePad[i] to false to indicate that the note is no longer playing.
All I did was to tie the digitalWrite HIGH/LOW calls to the state of activePad[i]. When activePad[i] is true, the light is on. When activePad[i] is false, the light is off.
However, your notes are NEVER going to time out, because your loop ALWAYS sets PinPlayTime[pad] = 0; Every time you go through the loop, you reset the time count, so it will never ever be greater than MaxPlayTime[pad].
And, I don't think you want to be ONLY checking against MaxPlayTime. Don't you want your notes to have different durations?
Bottom line: There is nothing wrong with the LED code in here. It is not your problem. Your problem is that you aren't doing the right things with muxarray, hitavg, and PinPlayTime