I am trying to make a Midi controller on an Arduino Uno R3 with 4 buttons, 3 of which which are lit by an LED Pattern that will be triggered/switched by button state.
I have the code for 3 current Buttons and each LED Pattern and Midi working in their own sketches.
I am looking for guidance in merging my 2 LED Patterns and having them triggered by a button while retaining the midi functionality of my project.
Thank You in Advance.
LED Pattern #1
#include <MIDI.h>
#define ATMEGA328 1
#ifdef ATMEGA328
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
#endif
// BUTTONS
// BUTTON LIGHTS
int LED1 = 9;
int LED2 = 10;
int LED3 = 11;
//PHYSICAL BUTTONS
const int N_BUTTONS = 3; //* total numbers of buttons
const int BUTTON_ARDUINO_PIN[N_BUTTONS] = { 2, 3, 4 }; //* pins of each button connected straight to the Arduino
int buttonCState[N_BUTTONS] = {}; // stores the button current value
int buttonPState[N_BUTTONS] = {}; // stores the button previous value
// DEBOUNCE
unsigned long lastDebounceTime[N_BUTTONS] = { 0 }; // the last time the output pin was toggled
unsigned long debounceDelay = 50; //* the debounce time; increase if the output flickers
// MIDI
byte midiCh = 1; //* MIDI channel to be used
byte note = 36; //* Lowest note to be used
byte cc = 1; //* Lowest MIDI CC to be used
// SETUP
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
// Baud Rate
// use if using with ATmega328 (uno, mega, nano...)
// 31250 for MIDI class compliant | 115200 for Hairless MIDI
Serial.begin(115200); //*
#ifdef DEBUG
Serial.println("Debug mode");
Serial.println();
#endif
// Buttons
// Initialize buttons with pull up resistors
for (int i = 0; i < N_BUTTONS; i++) {
pinMode(BUTTON_ARDUINO_PIN[i], INPUT_PULLUP);
}
}
// LOOP
void loop() {
// Fade in
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(LED1, brightness);
analogWrite(LED2, brightness);
analogWrite(LED3, brightness);
delay(10); // Adjust this delay to control the fading speed
}
// Stay at maximum brightness for 2 seconds
delay(2000);
// Fade out
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(LED1, brightness);
analogWrite(LED2, brightness);
analogWrite(LED3, brightness);
delay(10); // Adjust this delay to control the fading speed
}
buttons();
}
/////////////////////////////////////////////
// BUTTONS
void buttons() {
for (int i = 0; i < N_BUTTONS; i++) {
buttonCState[i] = digitalRead(BUTTON_ARDUINO_PIN[i]); // read pins from arduino
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (buttonPState[i] != buttonCState[i]) {
lastDebounceTime[i] = millis();
if (buttonCState[i] == LOW) {
// Sends the MIDI note ON accordingly to the chosen board
#ifdef ATMEGA328
// use if using with ATmega328 (uno, mega, nano...)
MIDI.sendNoteOn(note + i, 127, midiCh); // note, velocity, channel
#elif DEBUG
Serial.print(i);
Serial.println(": button on");
#endif
} else {
// Sends the MIDI note OFF accordingly to the chosen board
#ifdef ATMEGA328
// use if using with ATmega328 (uno, mega, nano...)
MIDI.sendNoteOn(note + i, 0, midiCh); // note, velocity, channel
#elif DEBUG
Serial.print(i);
Serial.println(": button off");
#endif
}
buttonPState[i] = buttonCState[i];
}
}
}
}
LED Pattern #2
#include <MIDI.h>
#define ATMEGA328 1
#ifdef ATMEGA328
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
#endif
// BUTTONS
// BUTTON LIGHTS
int LED1 = 9;
int LED2 = 10;
int LED3 = 11;
//PHYSICAL BUTTONS
const int N_BUTTONS = 3; //* total numbers of buttons
const int BUTTON_ARDUINO_PIN[N_BUTTONS] = { 2, 3, 4 }; //* pins of each button connected straight to the Arduino
int buttonCState[N_BUTTONS] = {}; // stores the button current value
int buttonPState[N_BUTTONS] = {}; // stores the button previous value
// DEBOUNCE
unsigned long lastDebounceTime[N_BUTTONS] = { 0 }; // the last time the output pin was toggled
unsigned long debounceDelay = 50; //* the debounce time; increase if the output flickers
// MIDI
byte midiCh = 1; //* MIDI channel to be used
byte note = 36; //* Lowest note to be used
byte cc = 1; //* Lowest MIDI CC to be used
// SETUP
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
// Baud Rate
// use if using with ATmega328 (uno, mega, nano...)
// 31250 for MIDI class compliant | 115200 for Hairless MIDI
Serial.begin(115200); //*
#ifdef DEBUG
Serial.println("Debug mode");
Serial.println();
#endif
// Buttons
// Initialize buttons with pull up resistors
for (int i = 0; i < N_BUTTONS; i++) {
pinMode(BUTTON_ARDUINO_PIN[i], INPUT_PULLUP);
}
}
// LOOP
void loop() {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
delay(250);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(250);
buttons();
}
/////////////////////////////////////////////
// BUTTONS
void buttons() {
for (int i = 0; i < N_BUTTONS; i++) {
buttonCState[i] = digitalRead(BUTTON_ARDUINO_PIN[i]); // read pins from arduino
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (buttonPState[i] != buttonCState[i]) {
lastDebounceTime[i] = millis();
if (buttonCState[i] == LOW) {
// Sends the MIDI note ON accordingly to the chosen board
#ifdef ATMEGA328
// use if using with ATmega328 (uno, mega, nano...)
MIDI.sendNoteOn(note + i, 127, midiCh); // note, velocity, channel
#elif DEBUG
Serial.print(i);
Serial.println(": button on");
#endif
} else {
// Sends the MIDI note OFF accordingly to the chosen board
#ifdef ATMEGA328
// use if using with ATmega328 (uno, mega, nano...)
MIDI.sendNoteOn(note + i, 0, midiCh); // note, velocity, channel
#elif DEBUG
Serial.print(i);
Serial.println(": button off");
#endif
}
buttonPState[i] = buttonCState[i];
}
}
}
}