Sound Man
New member
Hi everyone.
I am putting together a Midi Keypad which has 3 push switches to send midi notes to a computer to operate software. The midi side of things is working ok but what I am having trouble with is getting the LED's which are built into the push switches to work as I would like them to. The push switches have built in RGB indicator LED's and my plan is to arrange the coding so that for example the first switch lights up Green normally. When the switch is pressed the Green LED will go off briefly and the Blue LED will flash about 3 times indicating that the midi note has been sent to the computer.
I have managed to get the Blue LED's to flash ok when the switch is pressed but what I am having trouble with is getting the Green LED to go Off while the Blue LED is flashing. I first tried doing it with the Delay () coding and found out that that causes the program to also delay. After searching the net I came across the following Timer Library here and swapped Delay () for t.pulse () to add the delay to the Green LED. The result is that the LED goes Off ok when the switch is pressed but sometimes it doesn't come back on and when it does it's not always when the Blue LED has stopped flashing.
I've come to the thinking that the best way to do this would be to put the coding together so that the Green LED goes off for the duration that the Blue LED is pulsing but I don't know how to do this. Any help with this would be very much appreciated. Sorry for the long winded description.
Below is the coding that I have put together so far:-
I am putting together a Midi Keypad which has 3 push switches to send midi notes to a computer to operate software. The midi side of things is working ok but what I am having trouble with is getting the LED's which are built into the push switches to work as I would like them to. The push switches have built in RGB indicator LED's and my plan is to arrange the coding so that for example the first switch lights up Green normally. When the switch is pressed the Green LED will go off briefly and the Blue LED will flash about 3 times indicating that the midi note has been sent to the computer.
I have managed to get the Blue LED's to flash ok when the switch is pressed but what I am having trouble with is getting the Green LED to go Off while the Blue LED is flashing. I first tried doing it with the Delay () coding and found out that that causes the program to also delay. After searching the net I came across the following Timer Library here and swapped Delay () for t.pulse () to add the delay to the Green LED. The result is that the LED goes Off ok when the switch is pressed but sometimes it doesn't come back on and when it does it's not always when the Blue LED has stopped flashing.
I've come to the thinking that the best way to do this would be to put the coding together so that the Green LED goes off for the duration that the Blue LED is pulsing but I don't know how to do this. Any help with this would be very much appreciated. Sorry for the long winded description.
Below is the coding that I have put together so far:-
Code:
/*
Palladium Remote Keypad with twin LED's
Created: 3 Dec 2016 by Sims Sound Systems
You must select MIDI from the "Tools > USB Type" menu
*/
#include <Timer.h>
#include <Bounce.h>
// the MIDI channel number to send messages
const int channel = 1;
Timer t;
int blinkNextCue = 0; // Button pressed
int blinkPreviousCue = 0;
int blinkFXaux = 0;
int NextCueLEDstate = LOW;
int PreviousCueLEDstate = LOW;
int FXauxLEDstate = LOW;
long previousNextCueMillis = 0;
long previousPreviousCueMillis = 0;
long previousFXauxMillis = 0;
long NextCueBlinkInt = 200;
long PreviousCueBlinkInt = 200;
long FXauxBlinkInt = 200;
int NextCueButtonLED = 8; // Green LED
int NextCueLED = 9; // Blue LED
int PreviousCueButtonLED = 6; // Red LED
int PreviousCueLED = 7; // Blue LED
int FXauxButtonLED = 5; // Amber LED (Red & Green LED's connected together)
int FXauxLED = 4; // Blue LED
// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce NextCueButton = Bounce(10, 5);
Bounce PreviousCueButton = Bounce(11, 5);
Bounce FXauxButton = Bounce(12, 5);
void setup() {
// Setup input pins for "active low"
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
// Setup output pins
pinMode(NextCueButtonLED, OUTPUT); // Button not pressed
pinMode(NextCueLED, OUTPUT); // Button pressed
pinMode(PreviousCueButtonLED, OUTPUT);
pinMode(PreviousCueLED, OUTPUT);
pinMode(FXauxButtonLED, OUTPUT);
pinMode(FXauxLED, OUTPUT);
// Set initial LED values.
// HIGH = LED OFF, LOW = LED ON (Common Anode LEDs)
digitalWrite(NextCueButtonLED, LOW);
digitalWrite(NextCueLED, HIGH);
digitalWrite(PreviousCueButtonLED, LOW);
digitalWrite(PreviousCueLED, HIGH);
digitalWrite(FXauxButtonLED, LOW);
digitalWrite(FXauxLED, HIGH);
}
void loop() {
// Update all the buttons.
NextCueButton.update();
PreviousCueButton.update();
FXauxButton.update();
// Check each button for "falling" edge.
// Send a 'MIDI Note On' message when each button is pressed
if (NextCueButton.fallingEdge()) {
usbMIDI.sendNoteOn(126, 99, channel); // Midi Channel 126 = F#10
t.pulse(NextCueButtonLED, 10 * 400, HIGH); // Green LED off delay
blinkNextCue = 6; // Blue LED flash rate
}
if (PreviousCueButton.fallingEdge()) {
usbMIDI.sendNoteOn(127, 99, channel); // Midi Channel 127 = G10
t.pulse(PreviousCueButtonLED, 10 * 400, HIGH); // Red LED off delay
blinkPreviousCue = 6; // Blue LED flash rate
}
if (FXauxButton.fallingEdge()) {
usbMIDI.sendNoteOn(0, 99, channel); // Midi Channel 0 = C0
t.pulse(FXauxButtonLED, 10 * 400, HIGH); // Amber LED off delay
blinkFXaux = 6; // Blue LED flash rate
}
t.update();
// Check each button for "rising" edge
// Send a 'MIDI Note Off' message when each button is released
if (NextCueButton.risingEdge()) {
usbMIDI.sendNoteOff(126, 0, channel); // Midi Channel 126 = F#10
}
if (PreviousCueButton.risingEdge()) {
usbMIDI.sendNoteOff(127, 0, channel); // Midi Channel 127 = G10
}
if (FXauxButton.risingEdge()) {
usbMIDI.sendNoteOff(0, 0, channel); // Midi Channel 0 = C0
}
// MIDI Controllers should discard incoming MIDI messages.
while (usbMIDI.read()) {
// Ignore incoming messages
}
unsigned long currentNextCueMillis = millis();
unsigned long currentPreviousCueMillis = millis();
unsigned long currentFXauxMillis = millis();
if ((blinkNextCue > 0) || (NextCueLEDstate == LOW)) {
if(currentNextCueMillis - previousNextCueMillis > NextCueBlinkInt) {
// save the last time you blinked the LED
previousNextCueMillis = currentNextCueMillis;
// if the LED is off turn it on and vice-versa:
if (NextCueLEDstate == LOW) {
NextCueLEDstate = HIGH;
} else {
NextCueLEDstate = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(NextCueLED, NextCueLEDstate);
blinkNextCue--;
}
}
if ((blinkPreviousCue > 0) || (PreviousCueLEDstate == LOW)) {
if(currentPreviousCueMillis - previousPreviousCueMillis > PreviousCueBlinkInt) {
// save the last time you blinked the LED
previousPreviousCueMillis = currentPreviousCueMillis;
// if the LED is off turn it on and vice-versa:
if (PreviousCueLEDstate == LOW) {
PreviousCueLEDstate = HIGH;
} else {
PreviousCueLEDstate = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(PreviousCueLED, PreviousCueLEDstate);
blinkPreviousCue--;
}
}
if ((blinkFXaux > 0) || (FXauxLEDstate == LOW)) {
if(currentFXauxMillis - previousFXauxMillis > FXauxBlinkInt) {
// save the last time you blinked the LED
previousFXauxMillis = currentFXauxMillis;
// if the LED is off turn it on and vice-versa:
if (FXauxLEDstate == LOW) {
FXauxLEDstate = HIGH;
} else {
FXauxLEDstate = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(FXauxLED, FXauxLEDstate);
blinkFXaux--;
}
}
}