The following code controls a looping pedal via midi and works as intended, except that there's a slight delay between pressing the pedal and the midi message being sent, meaning that I can't accurately loop in time. I can't see a debounce value of 20 millis being the cause, the midi message is the first thing inside each "case", is there anything else slowing it down?
// midi controller for line 6 M13 to control looper - solo OD
// apr 19
#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
MIDI_CREATE_DEFAULT_INSTANCE();
// Constants
#define SWITCH1 2 //rec
#define SWITCH2 3 // stop/ OD
#define SWITCH3 4 // play
#define SWITCH4 5 // stop
# define LED1 8
# define LED2 9
# define LED3 7
# define LED4 6
#define SWITCHES 4 // how many switches?
int switches[SWITCHES] = { SWITCH1, SWITCH2, SWITCH3, SWITCH4 };
int switchState[SWITCHES] = { LOW, LOW, LOW, LOW }; // Initial state of switch
int leds[SWITCHES] = { LED1, LED2, LED3, LED4 };
int currentSwitch = 0;
int thisLED = 0;
int activeLED = 0;
int ovrLED = 0;
int recording = 0; // state or recording
int ovr = 0; // state of overdub
int solo = 0; // state of solo
int play = 0; // state of play
unsigned long SwitchMillis = 0; // when button was released
unsigned long DebounceMillis = 20; //
unsigned long CurrentMillis = 0;
void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
MIDI.begin(1);
// Setup Switches and activation LEDs
for ( currentSwitch = 0; currentSwitch < SWITCHES; currentSwitch++ )
{
pinMode( switches[currentSwitch], INPUT ); // Set pin for switch
digitalWrite( switches[currentSwitch], HIGH ); // Turn on internal pullup
pinMode( leds[currentSwitch], OUTPUT ); // Set pin for LED
}
for ( int i = 0; i < 4; i++ ) // show it's ready
{
for ( thisLED = 0; thisLED < SWITCHES; thisLED++ )
{
digitalWrite( leds[thisLED], HIGH );
delay(100);
digitalWrite( leds[thisLED], LOW );
}
}
}
void loop()
{
CurrentMillis = millis(); // get the time at the start of this loop()
if (CurrentMillis - SwitchMillis >= DebounceMillis) //is it time to check the switches?
{
SwitchMillis = millis(); //re-initilize Timer
for ( currentSwitch = 0; currentSwitch < SWITCHES; currentSwitch++ )
{
if ( (digitalRead(switches[currentSwitch]) != switchState[currentSwitch] ) && (switchState[currentSwitch] == HIGH) )
{
switch ( currentSwitch )
{
case 0: // rec or play
if (recording == 0)
{
MIDI.sendControlChange(50, 122, 1);
lightPin(currentSwitch);
recording = 1;
}
else
{
MIDI.sendControlChange(28, 122, 1); // play
lightPin(2); // switch play led on
recording = 0;
play = 1;
}
break;
case 1: // solo or off
if (solo == 0)
{
MIDI.sendControlChange(28, 122, 1); // play
MIDI.sendControlChange(11, 122, 1); // OD on
lightPin(1);
solo = 1;
play = 1;
}
else
{
MIDI.sendControlChange(28, 22, 1); // stop
MIDI.sendControlChange(11, 0, 1); // OD off
kill();
solo = 0;
play = 0;
}
break;
case 2: // stop or play
if (play == 0)
{
MIDI.sendControlChange(28, 122, 1); // play
lightPin(2); // switch play led on
play = 1;
solo = 0;
}
else
{
MIDI.sendControlChange(28, 22, 1); // stop
kill();
recording = 0; // if recording, switch it off
play = 0;
solo = 0;
}
break;
case 3: // ovr or play
if (ovr == 0)
{
MIDI.sendControlChange(50, 10, 1); // overdub
lightPin(currentSwitch ); // switch stop led on
ovr = 1;
}
else
{
MIDI.sendControlChange(28, 122, 1); // play
lightPin(2); // switch play led on
ovr = 0;
}
break;
}
} // end if changed
switchState[currentSwitch] = digitalRead( switches[currentSwitch] );
} // go thru switches
} // end check switches
} // end loop
void lightPin( int activeLED ) // switch chosen LED on, others off
{
for ( thisLED = 0; thisLED < SWITCHES; thisLED++ )
{
if (activeLED == thisLED)
{
digitalWrite( leds[thisLED], HIGH );
}
else
{
digitalWrite( leds[thisLED], LOW );
}
}
}
void kill() // switch all LEDs off
{
for ( thisLED = 0; thisLED < SWITCHES; thisLED++ )
{
digitalWrite( leds[thisLED], LOW );
}
}