//Cambiare solamente i campi commentati con "//*"
// SCEGLIERE LA SCHEDA E IL MICRO PROCESSORE
//
// "ATMEGA328" se stai usando ATmega328 - Uno, Mega, Nano...
// "ATMEGA32U4" se stai usando ATmega32U4 - Micro, Pro Micro, Leonardo...
// "TEENSY" se stai usando una Teensy board
// "DEBUG" se vuoi solamente il debug del codice nel monitor seriale
// dopo aver scelto la scheda, non sarà necessario commentare o decommentare le librerie che seguono
#define DEBUG 1 //* Metti qua l'uC (micro processore) che stai utilizzando, come compare nelle righe precedenti, seguito da "1", ad esempio "ATMEGA328 1", "DEBUG 1", etc.
// LIBRERIE DA INCLUDERE
// se stai usando ATmega328 - Uno, Mega, Nano...
#ifdef ATMEGA328
#include <MIDI.h> // by Francois Best
//MIDI_CREATE_DEFAULT_INSTANCE();
// se stai usando ATmega32U4 - Micro, Pro Micro, Leonardo...
#elif ATMEGA32U4
#include "MIDIUSB.h"
#endif
// PULSANTI
const int N_BUTTONS = 5; //* numero totale di pulsanti
const int BUTTON_ARDUINO_PIN[N_BUTTONS] = {2,3,4,5,6}; //* specificare i pin dei pulsanti connessi direttamente con Arduino
int buttonCState[N_BUTTONS] = {}; // memorizza il valore corrente del pulsante
int buttonPState[N_BUTTONS] = {}; // memorizza il valore precedentedel pulsante
//#define pin13 1 //* decommentare se si sta utilizzando il pin 13 (pin con LED integrato), oppure lasciare la riga sotto commento se non lo si sta usando
byte pin13index = 12; //* mette l'indice del pin 13 della resistenza del buttonPin[] se si sta usando, altrimenti lascia commentato
// debounce
unsigned long lastDebounceTime[N_BUTTONS] = {0}; // ultima volta che il pin in uscita è stato ingaggiato
unsigned long debounceDelay = 50; //* aumentare il valore di debounce se l'output sfarfalla
// POTENZIOMETRI
const int N_POTS = 2; //* numero totale di potenziometri (slide o rotativi)
const int POT_ARDUINO_PIN[N_POTS] = {A0, A1}; //* pins di ogni potenziometro connesso direttamente ad Arduino
int potCState[N_POTS] = {0}; // stato corrente del potenziometro
int potPState[N_POTS] = {0}; // stato precedente del potenziometro
int potVar = 0; // differenza tra lo stato corrente e quello precedente del potenziometro
int midiCState[N_POTS] = {0}; // stato corrente del valore MIDI
int midiPState[N_POTS] = {0}; // stato precedente del valore MIDI
const int TIMEOUT = 300; //* Amount of time the potentiometer will be read after it exceeds the varThreshold
const int varThreshold = 10; //* Threshold for the potentiometer signal variation
boolean potMoving = true; // If the potentiometer is moving
unsigned long PTime[N_POTS] = {0}; // Previously stored time
unsigned long timer[N_POTS] = {0}; // Stores the time that has elapsed since the timer was reset
// MIDI
byte midiCh = 1; //* MIDI channel to be used
byte note = 1; //* Lowest note to be used
byte cc = 1; //* Lowest MIDI CC to be used
// SETUP
void setup() {
// 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);
}
#ifdef pin13 // inicializa o pino 13 como uma entrada
pinMode(BUTTON_ARDUINO_PIN[pin13index], INPUT);
#endif
}
/////////////////////////////////////////////
// LOOP
void loop() {
buttons();
potentiometers();
}
/////////////////////////////////////////////
// BUTTONS
void buttons() {
for (int i = 0; i < N_BUTTONS; i++) {
buttonCState[i] = digitalRead(BUTTON_ARDUINO_PIN[i]); // read pins from arduino
#ifdef pin13
if (i == pin13index) {
buttonCState[i] = !buttonCState[i]; // inverts the pin 13 because it has a pull down resistor instead of a pull up
}
#endif
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 ATMEGA32U4
// use if using with ATmega32U4 (micro, pro micro, leonardo...)
noteOn(midiCh, note + i, 127); // channel, note, velocity
MidiUSB.flush();
#elif TEENSY
//do usbMIDI.sendNoteOn if using with Teensy
usbMIDI.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 ATMEGA32U4
// use if using with ATmega32U4 (micro, pro micro, leonardo...)
noteOn(midiCh, note + i, 0); // channel, note, velocity
MidiUSB.flush();
#elif TEENSY
//do usbMIDI.sendNoteOn if using with Teensy
usbMIDI.sendNoteOn(note + i, 0, midiCh); // note, velocity, channel
#elif DEBUG
Serial.print(i);
Serial.println(": button off");
#endif
}
buttonPState[i] = buttonCState[i];
}
}
}
}
/////////////////////////////////////////////
// POTENTIOMETERS
void potentiometers() {
for (int i = 0; i < N_POTS; i++) { // Loops through all the potentiometers
potCState[i] = analogRead(POT_ARDUINO_PIN[i]); // reads the pins from arduino
midiCState[i] = map(potCState[i], 0, 1023, 0, 127); // Maps the reading of the potCState to a value usable in midi
potVar = abs(potCState[i] - potPState[i]); // Calculates the absolute value between the difference between the current and previous state of the pot
if (potVar > varThreshold) { // Opens the gate if the potentiometer variation is greater than the threshold
PTime[i] = millis(); // Stores the previous time
}
timer[i] = millis() - PTime[i]; // Resets the timer 11000 - 11000 = 0ms
if (timer[i] < TIMEOUT) { // If the timer is less than the maximum allowed time it means that the potentiometer is still moving
potMoving = true;
}
else {
potMoving = false;
}
if (potMoving == true) { // If the potentiometer is still moving, send the change control
if (midiPState[i] != midiCState[i]) {
// Sends the MIDI CC accordingly to the chosen board
#ifdef ATMEGA328
// use if using with ATmega328 (uno, mega, nano...)
MIDI.sendControlChange(cc + i, midiCState[i], midiCh); // cc number, cc value, midi channel
#elif ATMEGA32U4
//use if using with ATmega32U4 (micro, pro micro, leonardo...)
controlChange(midiCh, cc + i, midiCState[i]); // (channel, CC number, CC value)
MidiUSB.flush();
#elif TEENSY
//do usbMIDI.sendControlChange if using with Teensy
usbMIDI.sendControlChange(cc + i, midiCState[i], midiCh); // cc number, cc value, midi channel
#elif DEBUG
Serial.print("Pot: ");
Serial.print(i);
Serial.print(" ");
Serial.println(midiCState[i]);
//Serial.print(" ");
#endif
potPState[i] = potCState[i]; // Stores the current reading of the potentiometer to compare with the next
midiPState[i] = midiCState[i];
}
}
}
}
/////////////////////////////////////////////
// if using with ATmega32U4 (micro, pro micro, leonardo...)
#ifdef ATMEGA32U4
// Arduino (pro)micro midi functions MIDIUSB Library
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
}
#endif