debouncing & send only 2 bytes on program change
This commit is contained in:
parent
414fbb94f3
commit
89aaa09453
1 changed files with 107 additions and 60 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
/* Pinout Config */
|
||||||
#define SEL_A_IN D2
|
#define SEL_A_IN D2
|
||||||
#define SEL_A_LED D3
|
#define SEL_A_LED D3
|
||||||
#define SEL_B_IN D4
|
#define SEL_B_IN D4
|
||||||
|
@ -10,6 +11,31 @@
|
||||||
#define SUST_IN D10
|
#define SUST_IN D10
|
||||||
#define EXPR_IN A0
|
#define EXPR_IN A0
|
||||||
|
|
||||||
|
/* MIDI codes */
|
||||||
|
#define NOTE_OFF 0x8
|
||||||
|
#define NOTE_ON 0x9
|
||||||
|
#define AFTERTOUCH 0xA
|
||||||
|
#define CONTROL_CHANGE 0xB
|
||||||
|
#define PATCH_CHANGE 0xC
|
||||||
|
#define CHANNEL_PRESSURE 0xD
|
||||||
|
#define PITCH_BEND 0xE
|
||||||
|
#define OTHER 0xF
|
||||||
|
|
||||||
|
void MIDImessage(int cmd_id, int data1, int data2) {
|
||||||
|
int channel = 0;
|
||||||
|
int cmd_byte = (cmd_id << 4) | (channel & 0xf);
|
||||||
|
Serial.write(cmd_byte);//send command byte
|
||||||
|
Serial.write(data1);//send data byte #1
|
||||||
|
Serial.write(data2);//send data byte #2
|
||||||
|
}
|
||||||
|
|
||||||
|
void MIDImessage2(int cmd_id, int data1) {
|
||||||
|
int channel = 0;
|
||||||
|
int cmd_byte = (cmd_id << 4) | (channel & 0xf);
|
||||||
|
Serial.write(cmd_byte);//send command byte
|
||||||
|
Serial.write(data1);//send data byte #1
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode( SEL_A_IN, INPUT_PULLUP );
|
pinMode( SEL_A_IN, INPUT_PULLUP );
|
||||||
pinMode( SEL_A_LED, OUTPUT );
|
pinMode( SEL_A_LED, OUTPUT );
|
||||||
|
@ -26,48 +52,53 @@ void setup() {
|
||||||
Serial.begin(31250);
|
Serial.begin(31250);
|
||||||
}
|
}
|
||||||
|
|
||||||
int last_expr_val = 0;
|
unsigned sust_stable_count = 0;
|
||||||
int last_sust_val = 0;
|
unsigned last_sust_val = 0;
|
||||||
|
float last_expr_val = 0.0;
|
||||||
#define NOTE_OFF 0x8
|
unsigned last_program_sel = -1;
|
||||||
#define NOTE_ON 0x9
|
|
||||||
#define AFTERTOUCH 0xA
|
|
||||||
#define CONTROL_CHANGE 0xB
|
|
||||||
#define PATCH_CHANGE 0xC
|
|
||||||
#define CHANNEL_PRESSURE 0xD
|
|
||||||
#define PITCH_BEND 0xE
|
|
||||||
#define OTHER 0xF
|
|
||||||
|
|
||||||
//send MIDI message
|
|
||||||
void MIDImessage(int cmd_id, int data1, int data2) {
|
|
||||||
int channel = 0;
|
|
||||||
int cmd_byte = (cmd_id << 4) | (channel & 0xf);
|
|
||||||
Serial.write(cmd_byte);//send command byte
|
|
||||||
Serial.write(data1);//send data byte #1
|
|
||||||
Serial.write(data2);//send data byte #2
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
delay(100);
|
delay(1);
|
||||||
|
|
||||||
|
|
||||||
|
/* Sustain
|
||||||
|
*/
|
||||||
|
|
||||||
if( digitalRead( SUST_IN ) == LOW ) {
|
if( digitalRead( SUST_IN ) == LOW ) {
|
||||||
if( last_sust_val != 1 ) {
|
if( last_sust_val == 1 ) {
|
||||||
last_sust_val = 1;
|
if( sust_stable_count == 10 ) {
|
||||||
|
// Sustain On
|
||||||
MIDImessage( CONTROL_CHANGE, 0x42, 0x7f );
|
MIDImessage( CONTROL_CHANGE, 0x42, 0x7f );
|
||||||
|
sust_stable_count ++;
|
||||||
|
} else if ( sust_stable_count < 10 ) {
|
||||||
|
sust_stable_count ++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if( last_sust_val != 0 ) {
|
// sustain pressed down
|
||||||
last_sust_val = 0;
|
last_sust_val = 1;
|
||||||
|
sust_stable_count = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if( last_sust_val == 0 ) {
|
||||||
|
if( sust_stable_count == 10 ) {
|
||||||
|
// Sustain Off
|
||||||
MIDImessage( CONTROL_CHANGE, 0x42, 0x00 );
|
MIDImessage( CONTROL_CHANGE, 0x42, 0x00 );
|
||||||
|
sust_stable_count ++;
|
||||||
|
} else if ( sust_stable_count < 10 ) {
|
||||||
|
sust_stable_count ++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// sustain released
|
||||||
|
last_sust_val = 0;
|
||||||
|
sust_stable_count = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int expr_val = analogRead( EXPR_IN );
|
/* Expression
|
||||||
if( expr_val != last_expr_val )
|
*/
|
||||||
{
|
|
||||||
last_expr_val = expr_val;
|
|
||||||
|
|
||||||
float expr_val_f = (float)(expr_val - 66) / 590.0;
|
int expr_val_in = analogRead( EXPR_IN );
|
||||||
|
float expr_val_f = (float)(expr_val_in - 66) / 590.0;
|
||||||
if( expr_val_f < 0.0 ){
|
if( expr_val_f < 0.0 ){
|
||||||
expr_val_f = 0.0;
|
expr_val_f = 0.0;
|
||||||
}
|
}
|
||||||
|
@ -75,37 +106,53 @@ void loop() {
|
||||||
expr_val_f = 1.0;
|
expr_val_f = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int val_b = (int)(expr_val_f * 128.0);
|
if( fabs(last_expr_val - expr_val_f) > 0.0086 ) {
|
||||||
|
last_expr_val = expr_val_f;
|
||||||
MIDImessage( CONTROL_CHANGE, 0x0b, val_b );
|
unsigned expr_val = (unsigned)(expr_val_f * 128.0);
|
||||||
|
MIDImessage( CONTROL_CHANGE, 0x0b, expr_val );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Program Select
|
||||||
|
*/
|
||||||
|
|
||||||
if( digitalRead( SEL_A_IN ) == LOW ) {
|
if( digitalRead( SEL_A_IN ) == LOW ) {
|
||||||
MIDImessage( PATCH_CHANGE, 0x0, 0x0 );
|
if( last_program_sel != 0 ) {
|
||||||
|
last_program_sel = 0;
|
||||||
|
MIDImessage2( PATCH_CHANGE, 0x0 );
|
||||||
digitalWrite( SEL_A_LED, HIGH );
|
digitalWrite( SEL_A_LED, HIGH );
|
||||||
digitalWrite( SEL_B_LED, LOW );
|
digitalWrite( SEL_B_LED, LOW );
|
||||||
digitalWrite( SEL_C_LED, LOW );
|
digitalWrite( SEL_C_LED, LOW );
|
||||||
digitalWrite( SEL_D_LED, LOW );
|
digitalWrite( SEL_D_LED, LOW );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if( digitalRead( SEL_B_IN ) == LOW ) {
|
if( digitalRead( SEL_B_IN ) == LOW ) {
|
||||||
MIDImessage( PATCH_CHANGE, 0x1, 0x1 );
|
if( last_program_sel != 1 ) {
|
||||||
|
last_program_sel = 1;
|
||||||
|
MIDImessage2( PATCH_CHANGE, 0x1 );
|
||||||
digitalWrite( SEL_A_LED, LOW );
|
digitalWrite( SEL_A_LED, LOW );
|
||||||
digitalWrite( SEL_B_LED, HIGH );
|
digitalWrite( SEL_B_LED, HIGH );
|
||||||
digitalWrite( SEL_C_LED, LOW );
|
digitalWrite( SEL_C_LED, LOW );
|
||||||
digitalWrite( SEL_D_LED, LOW );
|
digitalWrite( SEL_D_LED, LOW );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if( digitalRead( SEL_C_IN ) == LOW ) {
|
if( digitalRead( SEL_C_IN ) == LOW ) {
|
||||||
MIDImessage( PATCH_CHANGE, 0x2, 0x2 );
|
if( last_program_sel != 2 ) {
|
||||||
|
last_program_sel = 2;
|
||||||
|
MIDImessage2( PATCH_CHANGE, 0x2 );
|
||||||
digitalWrite( SEL_A_LED, LOW );
|
digitalWrite( SEL_A_LED, LOW );
|
||||||
digitalWrite( SEL_B_LED, LOW );
|
digitalWrite( SEL_B_LED, LOW );
|
||||||
digitalWrite( SEL_C_LED, HIGH );
|
digitalWrite( SEL_C_LED, HIGH );
|
||||||
digitalWrite( SEL_D_LED, LOW );
|
digitalWrite( SEL_D_LED, LOW );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if( digitalRead( SEL_D_IN ) == LOW ) {
|
if( digitalRead( SEL_D_IN ) == LOW ) {
|
||||||
MIDImessage( PATCH_CHANGE, 0x3, 0x3 );
|
if( last_program_sel != 3 ) {
|
||||||
|
last_program_sel = 3;
|
||||||
|
MIDImessage2( PATCH_CHANGE, 0x3 );
|
||||||
digitalWrite( SEL_A_LED, LOW );
|
digitalWrite( SEL_A_LED, LOW );
|
||||||
digitalWrite( SEL_B_LED, LOW );
|
digitalWrite( SEL_B_LED, LOW );
|
||||||
digitalWrite( SEL_C_LED, LOW );
|
digitalWrite( SEL_C_LED, LOW );
|
||||||
digitalWrite( SEL_D_LED, HIGH );
|
digitalWrite( SEL_D_LED, HIGH );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue