Professional Documents
Culture Documents
ino
Thursday, 16 May 2019 09:54
/*******************************************************************************
MIDI accordion for Mega
Jason Bugeja 2014
based on a projects by
* AccordionMega
* Dimon Yegorenkov 2011
*
* USB-MIDI to Legacy Serial MIDI converter
* Copyright 2012-2013 Yuuichi Akagawa
*
* Idea from LPK25 USB-MIDI to Serial MIDI converter
* by Collin Cunningham - makezine.com, narbotic.com
*
* for use with USB Host Shield 2.0 from Circuitsathome.com
* https://github.com/felis/USB_Host_Shield_2.0
*
* This is sample program. Do not expect perfect behavior.
*******************************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*******************************************************************************/
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//#include <WProgram.h> //++
//#include <LiquidCrystal.h> //++
//++
// this part is needed for reading BMP085 sensor //++
#include <Wire.h> //++
#define BMP085_ADDRESS 0x77 // I2C address of BMP085 //++
const unsigned char OSS = 0; //oversampling for measurement //++
//const unsigned char pressure_waittime[4] = { 5, 8, 14, 26 }; //++
//++
//#define MIDI_PORT Serial //++
HardwareSerial & MIDI_PORT = Serial; //++
HardwareSerial & BT_CC_PORT = Serial2; //++
HardwareSerial & BT_MENU_PORT = Serial3; //++
//++
boolean RIGHT_VEL_ON = false; //++
boolean RIGHT_EXPR_OFF = false; //++
boolean LEFT_EXPR_OFF = false; //++
boolean SETUP_MODE = true; //<---turn to "true" to enable Bass_Setup //++
boolean PAGETURN_MODE = true; //++
int maxvel = 100; // maximum velocity for left chords and bass - not to trigger
fortissimo sample layers
unsigned long t1;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//************************PageFlip********************************
//====================================================== variables
// The switches MUST be on Arduino pin 2 and 3 respectively
int ch15PitchBnd = 2;
int ch16PitchBnd = 3;
volatile int ch15PitchBnd_transition = 0;
volatile int ch16PitchBnd_transition = 0;
/////////////////////////////////////////////////////////////////////////////
#include <Usb.h> //
#include <usbh_midi.h> //
//
////////////////////////////// //
// MIDI Pin assign // //
// 2 : GND // //
// 4 : +5V(Vcc) with 220ohm // //
// 5 : TX // //
////////////////////////////// //
//
USB Usb; //
USBH_MIDI Midi(&Usb); //
//
void MIDI_poll(); //
void doDelay(unsigned long t1, unsigned long t2, unsigned long delayTime); //
/////////////////////////////////////////////////////////////////////////////
//************************PageFlipFlag****************************
// All this does is set a flag on a switch transition.
// The loop function looks for the flag.
void ch15PitchBnd_switch_transition(void)
{
ch15PitchBnd_transition = 1;
}
void ch16PitchBnd_switch_transition(void)
{
ch16PitchBnd_transition = 1;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void setup(){
//////////////////////////////////
//Workaround for non UHS2.0 Shield //
pinMode(7,OUTPUT); //
digitalWrite(7,HIGH); //
//
if (Usb.Init() == -1) { //
while(1); //halt //
}//if (Usb.Init() == -1... //
delay( 200 ); //
//////////////////////////////////
} // End of Setup
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void loop(){
//********************************************************************************************
**********
BassSide_poll(); // NEEDS TO BE FIRST BECAUSE OF BASS SETUP MODE
if (!SETUP_MODE){
BlueCC_poll();
PageTurn_poll();
//********************************************************************************************
**********
// BT_Menu_poll();
////////////////////////////////////////////////////
//
//
Usb.Task(); //
// t1 = micros(); //
if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) //
{ //
MIDI_poll(); //
} //
//delay(1ms) //
doDelay(t1, micros(), 1000); //
//
//
////////////////////////////////////////////////////
} //End of "if not setup mode" statement
}
///////////////////////////////////////////////////////////////////
/////////////////// - F U N C T I O N S - /////////////////////////
///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Poll USB MIDI Controller and send to serial MIDI //
void MIDI_poll() //
{ //
-4-
C:\Users\Schools_home\Google Drive\FLASH_DRIVE\Everything_Else\Arduino\Arduino_New\AccordionMega_USB_Keyb\AccordionMega_USB_Keyb.ino
Thursday, 16 May 2019 09:54
byte outBuf[ 3 ]; //
uint8_t size; //
//
do { //
if( (size=Midi.RcvData(outBuf)) > 0 ){ //
//MIDI Output
if ((outBuf[0]==0x90)|(outBuf[0]==0x80)){ //
outBuf[1]=(outBuf[1]+octave_higher)+transposer; //
if(!RIGHT_VEL_ON){ //
outBuf[2]=100; // to disable velocity less than 127 not //
// trigger Fortissimo sample layer //
} //
// midi_vel = outBuf[2];
} //
MIDI_PORT.write(outBuf, size); //
BT_CC_PORT.write(outBuf, size); //
} //
}while(size>0); //
} //
// Delay time (max 16383 us) //
void doDelay(unsigned long t1, unsigned long t2, unsigned long delayTime) //
{ //
unsigned long t3; //
//
if( t1 > t2 ){ //
t3 = (4294967295 - t1 + t2); //
}else{ //
t3 = t2 - t1; //
} //
//
if( t3 < delayTime ){ //
delayMicroseconds(delayTime - t3); //
} //
} //
////////////////////////////////////////////////////////////////////////////
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void BassSide_poll( void ){
if (!SETUP_MODE){
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
getDeltaPressure();
velocity = delta_pressure;
if (curr_velocity != velocity) {
if (!RIGHT_EXPR_OFF){
noteOn(chan_1,cc_descriptor,velocity);
}
if (!LEFT_EXPR_OFF){
noteOn(chan_2,cc_descriptor,velocity);
noteOn(chan_3,cc_descriptor,velocity);
}
-5-
C:\Users\Schools_home\Google Drive\FLASH_DRIVE\Everything_Else\Arduino\Arduino_New\AccordionMega_USB_Keyb\AccordionMega_USB_Keyb.ino
Thursday, 16 May 2019 09:54
curr_velocity = velocity;
}
}
else {
curr_velocity = 127;
}
if (bass){
pitch = bass_notes_midi_numbers[group][position];
}
else{
pitch = chord_notes_midi_numbers[group][position];
-6-
C:\Users\Schools_home\Google Drive\FLASH_DRIVE\Everything_Else\Arduino\Arduino_New\AccordionMega_USB_Keyb\AccordionMega_USB_Keyb.ino
Thursday, 16 May 2019 09:54
}
//MIDI output
if (bass){
notes_to_play = bass_notes_midi_numbers[group][position];
}
else{
notes_to_play = chord_notes_midi_numbers[group][position];
}
BT_CC_PORT.write(outBuf[0]);
BT_CC_PORT.write(outBuf[1]);
BT_CC_PORT.write(outBuf[2]);
}
else
mode_switch = notes_to_play;
getBassSetup();
bass_setup_i++;
if (bass_setup_i > 2){
SETUP_MODE = false;
}
}
}
}
void getZero(){
zero_pressure = 0;
for (int i=0; i<32; i++){
temperature = bmp085GetTemperature(bmp085ReadUT());
zero_pressure += bmp085GetPressure(bmp085ReadUP());
delayMicroseconds(500);
}
zero_pressure = zero_pressure/32;
-7-
C:\Users\Schools_home\Google Drive\FLASH_DRIVE\Everything_Else\Arduino\Arduino_New\AccordionMega_USB_Keyb\AccordionMega_USB_Keyb.ino
Thursday, 16 May 2019 09:54
void getDeltaPressure(){
if (pressure <= zero_pressure){
delta_pressure = int(float((pow((zero_pressure - pressure),1.4)+280)/45));
if (delta_pressure>127){
delta_pressure = 127;
}
}
else if (pressure > zero_pressure){
delta_pressure = int(float((pow((pressure - zero_pressure),1.4)+280)/40));
if (delta_pressure>127){
delta_pressure = 127;
}
}
}
MIDI_PORT.write(midi_cmd);
MIDI_PORT.write(pitch);
-8-
C:\Users\Schools_home\Google Drive\FLASH_DRIVE\Everything_Else\Arduino\Arduino_New\AccordionMega_USB_Keyb\AccordionMega_USB_Keyb.ino
Thursday, 16 May 2019 09:54
MIDI_PORT.write(midi_vel);
BT_CC_PORT.write(midi_cmd);
BT_CC_PORT.write(pitch);
BT_CC_PORT.write(midi_vel);
-9-