Accordion Arduino MIDI Code
Accordion Arduino MIDI Code
ino
Thursday, 16 May 2019 10:01
#include <SFE_BMP180.h>
#include <Wire.h>
#include <MIDI.h>
#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
struct MySettings : public midi::DefaultSettings
{
//#define DEBUG//uncomment this line to print serial messages, comment to send MIDI data
//#define BLUETOOTH//uncomment this line to send MIDI data via bluetooth instead of USB
//#define BMP//Puncomment this line to use the BMP180 to add dynamics via bellows
#ifdef BLUETOOTH
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial1, MIDI, MySettings);
#else
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, MySettings);
#endif
char right_hand_pins[] = { 2, 3, 4, 5, 6, 7 };
// array to store up/down status of right keys
int RightKeysStatus[] = {
B0000000,
B0000000,
B0000000,
B0000000,
B0000000,
B0000000
};
//Note: Based on how the opto-interruptors are laid out,
//You will likely have to remap their pitch numbers (53-93)
const char right_notes_midi_numbers[][8] = {
{68,66,64,63,61,58,56,54},//2
{67,65,62,60,59,57,55,53},//3
{88,84,81,79,76,74,72,69},//4
{93,91,89,86,83,77,73,71},//5
{90,87,85,82,80,78,75,70},//6
{92,0,0,0,0,0,0,0} //7
};
void setup()
-1-
C:\Users\Schools_home\Google Drive\FLASH_DRIVE\Everything_Else\Arduino\Mailson_Souza_Lima_2019\MIDI_AccordionMailsonVavra\MIDI_Accordion.ino
Thursday, 16 May 2019 10:01
{
#ifdef DEBUG
Serial.begin(9600);
#else
MIDI.begin();
//If we're sending MIDI over Serial1, open Serial for additional debugging
#ifdef BLUETOOTH
Serial.begin(9600);
#endif
#endif
//Digital pins start turned off
for (int i=0; i<sizeof(left_hand_pins);i++){
pinMode(left_hand_pins[i],OUTPUT);
digitalWrite(left_hand_pins[i], LOW);
}
for (int i=0; i<sizeof(right_hand_pins);i++){
pinMode(right_hand_pins[i],OUTPUT);
digitalWrite(right_hand_pins[i], LOW);
}
#ifdef BMP
init_BMP();
#endif
}
void loop()
{
#ifdef BMP
//Read pressure from the BMP_180 and convert it to MIDI expression
int expression = get_expression(prev_expression);
#ifdef DEBUG
Serial.print("Expression Change: ");
Serial.println(expression);
#else
MIDI.sendControlChange(CC_Expression,expression,1);
//Don't let bass overpower melody
MIDI.sendControlChange(CC_Expression,constrain(expression-6,0,127),2);
-2-
C:\Users\Schools_home\Google Drive\FLASH_DRIVE\Everything_Else\Arduino\Mailson_Souza_Lima_2019\MIDI_AccordionMailsonVavra\MIDI_Accordion.ino
Thursday, 16 May 2019 10:01
//Don't let chords overpower melody
MIDI.sendControlChange(CC_Expression,constrain(expression-12,0,127),3);
#endif
prev_expression = expression;
e = 0;
}
else {
e = e + 1;
}
}
#endif
byte reg_values = 0;
//Check to see which bits have changed and send the appropriate midi message
//Instead of iterating the array from 0-7, use binary search to find the modified bits faster
void check_key(int reg, int group, boolean on, boolean left){
// saving 4 iterations, dividing byte by 2
if (reg & 0xF0) {
for(int i=0; i<4; i++){
if ((reg >> 4+i) & 1){
note_midi(group, i+4, on, left);
}
}
}
-3-
C:\Users\Schools_home\Google Drive\FLASH_DRIVE\Everything_Else\Arduino\Mailson_Souza_Lima_2019\MIDI_AccordionMailsonVavra\MIDI_Accordion.ino
Thursday, 16 May 2019 10:01
else if (reg & 0x0F) {
for(int i=0; i<4; i++){
if ((reg >> i) & 1){
note_midi(group, i, on, left);
}
}
}
}
if (left){
if (on){
LeftKeysStatus[group] |= (1 << position); //setting bit value
}
else {
LeftKeysStatus[group] &= ~(1 << position); //setting bit value
}
pitch = left_notes_midi_numbers[group][position];
if(pitch < 48) {
midi_vel = 120;//Don't let bass overpower melody
channel = 2;
}
else {
midi_vel = 110;//Don't let chords overpower melody
channel = 3;
}
}
else{
if(on) {
RightKeysStatus[group] |= (1 << position); //setting bit value
}
else {
RightKeysStatus[group] &= ~(1 << position); //setting bit value
}
pitch = right_notes_midi_numbers[group][position];
channel = 1;
}
if (pitch){
#ifdef DEBUG
Serial.print("Note ");
if(on){
Serial.print("on: ");
}
else {
Serial.print("off: ");
}
Serial.println(pitch);
#else
if(on) {
MIDI.sendNoteOn(pitch, midi_vel, channel);
}
else {
MIDI.sendNoteOff(pitch, midi_vel, channel);
}
#endif
}
-4-