/*
Creation: 5 Aug 2017
©-2017 Patrick-Gilles Maillot
TtapMIDI-X32: a Stomp pedal for sending Tempo TAP delay as SYSEX MIDI messages to an X32 digital mixer from
Behringer (or any compatible MIDI devive).
The program waits for a HW pin to becon=me active (when the user presses a switch). These events
are used to generate a delay sent as SYSEX MIDI array to the connected device.
This project started from an Arduino project/sketch file: MIDI note player / created 13 Jun 2006
The circuit for MIDI out:
Leonardo digital pin 1 connected to MIDI jack pin 5
MIDI jack pin 2 connected to ground
MIDI jack pin 4 connected to +5V through 220-ohm resistor
Attach a MIDI cable to the jack, then to a MIDI synth, and play music.
The circuit for the switch:
Pushbutton attached to Leonardo digital pin pin 2 and to ground
*/
unsigned char S_array[] = { // MIDI SYSEX or command array data
0xF0, 0x00, 0x20, 0x32, 0x32,
0x2F, 0x66, 0x78, 0x2F,
0x30,
0x2F, 0x70, 0x61, 0x72, 0x2F,
0x30, 0x32,
0x20,
0x33, 0x30, 0x30, 0x30,
0xF7
};
//
const int S_size = sizeof(S_array) / sizeof(char); // MIDI SYSEX or command array size
const int B_pin = 5; // Switch pin (in)
const int L_pin = 7; // LED pin (out)
int D_delay = 20; // Debounce time; increase if needed
int S_state; // state of the switch
int L_state; // state of the LED
unsigned long Time1; // Start time
unsigned long Dtime; // Delta time
int Tcount; // Indicate first or secont press for timer calculation
void setup() {
Serial.begin(9600);
// called once at program start
// Set MIDI baud rate:
//Serial1.begin(31250);
// Set pins directions
pinMode(B_pin, INPUT_PULLUP);
pinMode(L_pin, OUTPUT);
S_state = digitalRead(B_pin);
digitalWrite(L_pin, HIGH);
Time1 = Dtime = Tcount = 0;
Serial.println("Hello World");
delay(5000);
}
void loop()
{
// start main loop
// read the state of the switch, check for state change:
S_state = digitalRead(B_pin);
// wait a few milliseconds
delay(D_delay);
// stop LED if needed
if (L_state) {
digitalWrite(L_pin, HIGH);
L_state = 0;
}
//
// only send MIDI data if the new button state is LOW
if ((digitalRead(B_pin) == 1) && (S_state == 0)) {
if (Tcount == 0) {
// first time press: Get time
Serial.print("push 1");
Time1 = millis();
Tcount = 1;
} else {
// second time press: calculate time difference and send data
// calculate Dtime in milliseconds
Dtime = millis() - Time1;
Serial.print(Dtime);
// Dtime as valid X32 data 0 to 3000 ms
if (Dtime < 1) Dtime = 0;
if (Dtime > 3000) Dtime = 3000;
// set value of FX number in SYSEX buffer
Serial.println(Dtime);
S_array[9] = 0x32;
// set value of Ftime into SYSEX buffer
S_array[18] = 0x30 + Dtime / 1000;
S_array[19] = 0x30 + (Dtime - (Dtime/1000) * 1000) / 100;
S_array[20] = 0x30 + (Dtime - (Dtime/100) * 100) / 10;
S_array[21] = 0x30 + (Dtime - (Dtime/10) * 10);
// reset time press counter
Tcount = 0;
// play all bytes from SYSEX data array:
for (int i = 0; i < S_size; i ++) {
Serial.print(S_array[i]);
}
// LED on
digitalWrite(L_pin, LOW);
L_state = 1;
}
}
}