debouncing & send only 2 bytes on program change

This commit is contained in:
Michael Sippel 2024-12-08 05:45:50 +01:00
parent 414fbb94f3
commit 89aaa09453
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -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,86 +52,107 @@ 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 ) {
if( sust_stable_count == 10 ) {
// Sustain On
MIDImessage( CONTROL_CHANGE, 0x42, 0x7f );
sust_stable_count ++;
} else if ( sust_stable_count < 10 ) {
sust_stable_count ++;
}
} else {
// sustain pressed down
last_sust_val = 1; last_sust_val = 1;
MIDImessage( CONTROL_CHANGE, 0x42, 0x7f ); sust_stable_count = 0;
} }
} else { } else {
if( last_sust_val != 0 ) { if( last_sust_val == 0 ) {
if( sust_stable_count == 10 ) {
// Sustain Off
MIDImessage( CONTROL_CHANGE, 0x42, 0x00 );
sust_stable_count ++;
} else if ( sust_stable_count < 10 ) {
sust_stable_count ++;
}
} else {
// sustain released
last_sust_val = 0; last_sust_val = 0;
MIDImessage( CONTROL_CHANGE, 0x42, 0x00 ); 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 );
if( expr_val_f < 0.0 ){ float expr_val_f = (float)(expr_val_in - 66) / 590.0;
expr_val_f = 0.0; if( expr_val_f < 0.0 ){
} expr_val_f = 0.0;
if( expr_val_f > 1.0 ){
expr_val_f = 1.0;
}
int val_b = (int)(expr_val_f * 128.0);
MIDImessage( CONTROL_CHANGE, 0x0b, val_b );
} }
if( expr_val_f > 1.0 ){
expr_val_f = 1.0;
}
if( fabs(last_expr_val - expr_val_f) > 0.0086 ) {
last_expr_val = expr_val_f;
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 ) {
digitalWrite( SEL_A_LED, HIGH ); last_program_sel = 0;
digitalWrite( SEL_B_LED, LOW ); MIDImessage2( PATCH_CHANGE, 0x0 );
digitalWrite( SEL_C_LED, LOW ); digitalWrite( SEL_A_LED, HIGH );
digitalWrite( SEL_D_LED, LOW ); digitalWrite( SEL_B_LED, LOW );
digitalWrite( SEL_C_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 ) {
digitalWrite( SEL_A_LED, LOW ); last_program_sel = 1;
digitalWrite( SEL_B_LED, HIGH ); MIDImessage2( PATCH_CHANGE, 0x1 );
digitalWrite( SEL_C_LED, LOW ); digitalWrite( SEL_A_LED, LOW );
digitalWrite( SEL_D_LED, LOW ); digitalWrite( SEL_B_LED, HIGH );
digitalWrite( SEL_C_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 ) {
digitalWrite( SEL_A_LED, LOW ); last_program_sel = 2;
digitalWrite( SEL_B_LED, LOW ); MIDImessage2( PATCH_CHANGE, 0x2 );
digitalWrite( SEL_C_LED, HIGH ); digitalWrite( SEL_A_LED, LOW );
digitalWrite( SEL_D_LED, LOW ); digitalWrite( SEL_B_LED, LOW );
digitalWrite( SEL_C_LED, HIGH );
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 ) {
digitalWrite( SEL_A_LED, LOW ); last_program_sel = 3;
digitalWrite( SEL_B_LED, LOW ); MIDImessage2( PATCH_CHANGE, 0x3 );
digitalWrite( SEL_C_LED, LOW ); digitalWrite( SEL_A_LED, LOW );
digitalWrite( SEL_D_LED, HIGH ); digitalWrite( SEL_B_LED, LOW );
digitalWrite( SEL_C_LED, LOW );
digitalWrite( SEL_D_LED, HIGH );
}
} }
} }