I have a (working) program which sends a midi signal and illumates 1 of 4 leds. I want one of them to act independantly, switching on for the first press, then off for the second, so I can tell if overdub is triggered.
The new (problem) code is lines 74-81 (case 1), where ovrLED should store the state and act accordingly. However, it switches the light on, then it stays on.
I hope this makes sense?
// midi controller for line 6 M13 to control looper
// dec 2018
#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?
#define BOUNCEDELAY 100
int switches[SWITCHES] = { SWITCH1, SWITCH2, SWITCH3, SWITCH4 };
int switchState[SWITCHES] = { HIGH, HIGH, HIGH, HIGH }; // Initial state of switch is high due to internal pullup
int leds[SWITCHES] = { LED1, LED2, LED3, LED4 };
int currentSwitch = 0;
int thisLed = 0;
int activeLed = 0;
int ovrLED = 0;
int currentProgram = 22; // current program - sent to the output
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++ )
{
for ( thisLed = 0; thisLed < SWITCHES; thisLed++ )
{
digitalWrite( leds[thisLed], HIGH );
delay(100);
digitalWrite( leds[thisLed], LOW );
}
}
}
void loop()
{
for ( currentSwitch = 0; currentSwitch < SWITCHES; currentSwitch++ )
{
if ( (digitalRead(switches[currentSwitch]) != switchState[currentSwitch] ) && (switchState[currentSwitch] == HIGH) )
{
switch ( currentSwitch )
{
case 0:
MIDI.sendControlChange(50, 122, 1); // rec
lightPin(currentSwitch);
break;
case 1:
MIDI.sendControlChange(50, 10, 1); // overdub
if (ovrLED == 0 ) {
lightPin(currentSwitch );
ovrLED = 1;
}
else {
analogWrite(currentSwitch, 0);
ovrLED = 0;
}
break;
case 2:
MIDI.sendControlChange(28, 122, 1); // play
lightPin(currentSwitch );
break;
case 3:
MIDI.sendControlChange(28, 22, 1); // stop
lightPin(currentSwitch );
break;
}
delay( BOUNCEDELAY );
}
switchState[currentSwitch] = digitalRead( switches[currentSwitch] );
}
}
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 );
}
}
}