Hi lloyddean,
First of all, I can not thank you enough for helping me out with this. I definitely owe you some favors.
Here is where I am at.
The revised code gave me all kinds of errors and would not trigger any notes whatsoever.
I think that it had something to do with the Serial.write(midi_message<< 4 | MIDI_CHANNEL); // BYTE 1
line of code.
In any case, I went back to the original one you provided, and I updated the mappings from 36-83 and here is the result.
Here is the code:
// <http://forum.arduino.cc/index.php?topic=366478.0>
#define COUNT_ENTRIES(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))
const uint8_t pinCONTROL0 = 5;
const uint8_t pinCONTROL1 = 4;
const uint8_t pinCONTROL2 = 3;
const uint8_t pinCONTROL3 = 2;
struct pad_t
{
uint32_t active : 1; // 1 false/true
uint32_t note : 7; // 8 0 - 127
uint32_t velocity : 10; // 18 0 - 1023
uint32_t play_time : 7; // 25 0 - 127
uint32_t play_timeMAX: 7; // 32 0 - 127
};
struct pad_t pads[][3] =
{
// ADC0 ADC1 ADC2
{ { false, 51, 400, 0, 90 }, { false, 67, 400, 0, 90 }, { false, 83, 400, 0, 90 } } // PAD 15, 31, 47
, { { false, 50, 400, 0, 90 }, { false, 66, 400, 0, 90 }, { false, 82, 400, 0, 90 } } // PAD 14, 30, 46
, { { false, 49, 400, 0, 90 }, { false, 65, 400, 0, 90 }, { false, 81, 400, 0, 90 } } // PAD 13, 29, 45
, { { false, 48, 400, 0, 90 }, { false, 64, 400, 0, 90 }, { false, 80, 400, 0, 90 } } // PAD 12, 28, 44
, { { false, 47, 400, 0, 90 }, { false, 63, 400, 0, 90 }, { false, 79, 400, 0, 90 } } // PAD 11, 27, 43
, { { false, 46, 400, 0, 90 }, { false, 62, 400, 0, 90 }, { false, 78, 400, 0, 90 } } // PAD 10, 26, 42
, { { false, 45, 400, 0, 90 }, { false, 61, 400, 0, 90 }, { false, 77, 400, 0, 90 } } // PAD 9, 25, 41
, { { false, 44, 400, 0, 90 }, { false, 60, 400, 0, 90 }, { false, 76, 400, 0, 90 } } // PAD 8, 24, 40
, { { false, 43, 400, 0, 90 }, { false, 59, 400, 0, 90 }, { false, 75, 400, 0, 90 } } // PAD 7, 23, 39
, { { false, 42, 400, 0, 90 }, { false, 58, 400, 0, 90 }, { false, 74, 400, 0, 90 } } // PAD 6, 22, 38
, { { false, 41, 400, 0, 90 }, { false, 57, 400, 0, 90 }, { false, 73, 400, 0, 90 } } // PAD 5, 21, 37
, { { false, 40, 400, 0, 90 }, { false, 56, 400, 0, 90 }, { false, 72, 400, 0, 90 } } // PAD 4, 20, 36
, { { false, 39, 400, 0, 90 }, { false, 55, 400, 0, 90 }, { false, 71, 400, 0, 90 } } // PAD 3, 19, 35
, { { false, 38, 400, 0, 90 }, { false, 54, 400, 0, 90 }, { false, 70, 400, 0, 90 } } // PAD 2, 18, 34
, { { false, 37, 400, 0, 90 }, { false, 53, 400, 0, 90 }, { false, 69, 400, 0, 90 } } // PAD 1, 17, 33
, { { false, 36, 400, 0, 90 }, { false, 52, 400, 0, 90 }, { false, 68, 400, 0, 90 } } // PAD 0, 16, 32
};
// MIDI channel from 0 to 15 ( 1-16 in "real world")
const uint8_t MIDI_CHANNEL = 0;
// Velocity ON (true) or OFF (false)
bool fVelocity = true;
void midi_send(uint8_t midi_message, uint8_t pitch, uint8_t midi_velocity)
{
Serial.write(midi_message + MIDI_CHANNEL);
Serial.write(pitch);
Serial.write(midi_velocity);
}
void do_channel(uint8_t const pinADC, struct pad_t& pad)
{
int velocity = analogRead(pinADC);
if ( velocity > pad.velocity )
{
if ( ! pad.active )
{
pad.active = true;
pad.play_time = 0;
velocity = fVelocity ? ((velocity / 8) - 1) : 127;
midi_send(144, pad.note, velocity)
;}
else
{
++pad.play_time;
}
}
else if ( pad.active )
{
if ( ++pad.play_time > pad.play_timeMAX )
{
pad.active = false;
midi_send(144, pad.note, 0);
}
}
}
void loop()
{
const int ROWS = COUNT_ENTRIES(pads);
const int COLUMNS = COUNT_ENTRIES(pads[0]);
for ( size_t iROW = 0; iROW < ROWS; iROW++ )
{
// ... MULTIPLEX SELECTOR ...
// digitalWrite(pinCONTROL0, (iROW & 0x1111) >> 3);
// digitalWrite(pinCONTROL1, (iROW & 0x0111) >> 2);
// digitalWrite(pinCONTROL2, (iROW & 0x0011) >> 1);
// digitalWrite(pinCONTROL3, (iROW & 0x0001) >> 0);
digitalWrite(pinCONTROL0, (iROW&15)>>3);
digitalWrite(pinCONTROL1, (iROW&7)>>2);
digitalWrite(pinCONTROL2, (iROW&3)>>1);
digitalWrite(pinCONTROL3, (iROW&1));
// ... 3 CHANNELS A0, A1 and A2 ...
const uint8_t pinsADC[COLUMNS] = { A0, A1, A2 };
for ( size_t iCOLUMN = 0; iCOLUMN < COLUMNS; iCOLUMN++ )
{
do_channel(pinsADC[iCOLUMN], pads[iROW][iCOLUMN]);
}
}
}
void setup()
{
pinMode(pinCONTROL0, OUTPUT);
pinMode(pinCONTROL1, OUTPUT);
pinMode(pinCONTROL2, OUTPUT);
pinMode(pinCONTROL3, OUTPUT);
Serial.begin(115200);
}
When played softly, all the notes play properly. However they play a lot more than one note when played at a normal strength amount. I know that you are thinking that the Piezo discs are triggering each other somehow, but I have proven that is not the case by isolating a single disc, which still triggers multiple notes and incorrect notes when played harder.
Anyway, I think that this is my last hurdle here. I am certain that something in the code is causing the triggering of multiple notes being played when a single Piezo is pressed hard, but I just don't know what it is.
Thanks for all your help here!!!!
Justin
moderator added code tags