Hello again,
I'm building a little midi controller for iOS to send command on a light application Luminair.
(5x buttons and 4 potentiometers)
my code is finished but I would like to optimise it a bit more.
Could anyone give me some advices to improve my skills and save memory/space in the code ?
Regards
Bib
#include <Bounce.h> // Library BOUNCE.h (EXISTE BOUNCE_2.h..)
#include <EEPROM.h> // Library EEPROM pour TOGGLE OU MOMENTARY
const int channel = 1; // the MIDI channel number to send messages
Bounce button1 = Bounce(1, 5); // 5 = 5 ms debounce time
Bounce button2 = Bounce(2, 5); // which is appropriate for good
Bounce button3 = Bounce(3, 5); // quality mechanical pushbuttons
Bounce button4 = Bounce(4, 100); // TOGGLE plus long
Bounce button5 = Bounce(5, 100); // TOGGLE plus long
unsigned int ValuePOT1 = 0; // value read from the potentiometer
unsigned char MIDIValuePOT1 = 0; // value en MIDI du POT1
unsigned char PourcentagePOT1 = 0; // value en Pourcentage du MIDI du POT1
unsigned int ValuePOT2 = 0;
unsigned char MIDIValuePOT2 = 0;
unsigned char PourcentagePOT2 = 0;
unsigned int ValueFAD1 = 0; // value read from the fader
unsigned char MIDIValueFAD1 = 0;
unsigned char PourcentageFAD1 = 0;
unsigned int ValueFAD2 = 0;
unsigned char MIDIValueFAD2 = 0;
unsigned char PourcentageFAD2 = 0;
char previousPOT1 = -1; // store previously sent values, to detect changes
char previousPOT2 = -1;
char previousFAD1 = -1;
char previousFAD2 = -1;
elapsedMillis msec = 0;
const bool adress = 0; // Adresse de EEPROM
void setup() {
Serial.begin(9600); Serial.print(" FINAL_V5 // ");
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP); // CABLE JAUNE TOGGLE
pinMode(5, INPUT_PULLUP); // CABLE BLANC TOGGLE
if ((EEPROM.read(adress) == 0) && (digitalRead(3) == LOW)) { // Question pour TOGGLE OU MOMENTARY de 4 et 5
EEPROM.write(adress,1); }
else if ((EEPROM.read(adress) == 1) && (digitalRead(3) == LOW)) {
EEPROM.write(adress,0); }
if (EEPROM.read(adress) == 1) { Serial.println("TOGGLE"); }
else { Serial.println("MOMENTARY"); }
Serial.println();
} // Fin de Setup
void loop() {
if (msec >= 20) {
msec = 0;
potentiometers();
faders();
} // Fin de if msec
buttons();
// MIDI Controllers should discard incoming MIDI messages.
while (usbMIDI.read()) { // ignore incoming messages
} // Fin de while
} // Fin de Loop
/////////////////////////////////////////////////////
//FONCTIONS//
/////////////////////////////////////////////////////
void potentiometers() //////////////////////////////
{
ValuePOT1 = analogRead(A0);
MIDIValuePOT1 = ValuePOT1/8; // 1024/8 = 128 parfait pour le MIDI moins de place...
ValuePOT2 = analogRead(A1);
MIDIValuePOT2 = ValuePOT2/8;
if (MIDIValuePOT1 != previousPOT1) {
usbMIDI.sendControlChange(11, MIDIValuePOT1, channel);
previousPOT1 = MIDIValuePOT1;
serial(); // a commenter pour le produit final
}
if (MIDIValuePOT2 != previousPOT2) {
usbMIDI.sendControlChange(12, MIDIValuePOT2, channel);
previousPOT2 = MIDIValuePOT2;
serial(); // a commenter pour le produit final
}
} // Fin de Potentiometers
void faders() //////////////////////////////
{
ValueFAD1 = analogRead(A2);
MIDIValueFAD1 = ValueFAD1/8;
ValueFAD2 = analogRead(A3);
MIDIValueFAD2 = ValueFAD2/8;
if (MIDIValueFAD1 != previousFAD1) {
usbMIDI.sendControlChange(13, MIDIValueFAD1, channel);
previousFAD1 = MIDIValueFAD1;
serial(); // a commenter pour le produit final
}
if (MIDIValueFAD2 != previousFAD2) {
usbMIDI.sendControlChange(14, MIDIValueFAD2, channel);
previousFAD2 = MIDIValueFAD2;
serial(); // a commenter pour le produit final
}
} // Fin de Faders
void buttons() /////////////////////////////
{
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
if (button1.fallingEdge()) { // Attendre la pression des butons
usbMIDI.sendControlChange(1, 127, channel);
}
if (button2.fallingEdge()) {
usbMIDI.sendControlChange(2, 127, channel);
}
if (button3.fallingEdge()) {
usbMIDI.sendControlChange(3, 127, channel);
}
if (button4.fallingEdge()) {
usbMIDI.sendControlChange(4, 127, channel);
}
if (button5.fallingEdge()) {
usbMIDI.sendControlChange(5, 127, channel);
}
if (button1.risingEdge()) { // Attendre la remonté des boutons
usbMIDI.sendControlChange(1, 0, channel);
}
if (button2.risingEdge()) {
usbMIDI.sendControlChange(2, 0, channel);
}
if (button3.risingEdge()) {
usbMIDI.sendControlChange(3, 0, channel);
}
if (EEPROM.read(adress) == 1) { // TOGGLE pour Luminair
if (button4.risingEdge()) {
usbMIDI.sendControlChange(4, 0, channel);
}
if (button5.risingEdge()) {
usbMIDI.sendControlChange(5, 0, channel);
}
} // Fin de if TOOGLE OU MOMENTARY
} // Fin de buttons
void serial() ////////////////////////////// POUR DEBUG UNIQUEMENT
{
PourcentagePOT1 = map(MIDIValuePOT1, 0, 127, 0, 100);
Serial.print(" POT1 = "); Serial.print(ValuePOT1);
Serial.print(" // ");
Serial.print(MIDIValuePOT1); Serial.print(" // ");
Serial.print(PourcentagePOT1); Serial.println("%");
PourcentagePOT2 = map(MIDIValuePOT2, 0, 127, 0, 100);
Serial.print(" POT2 = "); Serial.print(ValuePOT2);
Serial.print(" // ");
Serial.print(MIDIValuePOT2); Serial.print(" // ");
Serial.print(PourcentagePOT2); Serial.println("%");
PourcentageFAD1 = map(MIDIValueFAD1, 0, 127, 0, 100);
Serial.print(" FAD1 = "); Serial.print(ValueFAD1);
Serial.print(" // ");
Serial.print(MIDIValueFAD1); Serial.print(" // ");
Serial.print(PourcentageFAD1); Serial.println("%");
PourcentageFAD2 = map(MIDIValueFAD2, 0, 127, 0, 100);
Serial.print(" FAD2 = "); Serial.print(ValueFAD2);
Serial.print(" // ");
Serial.print(MIDIValueFAD2); Serial.print(" // ");
Serial.print(PourcentageFAD2); Serial.println("%");
Serial.println("");
} // Fin de Serial