Ughhh that one is a little harder to me! I tested it, and although it's pretty straight forward, I don't get how putting something like it in my code would help. Guess I have to study the concept a little more.
Anyway, on my way to try it, I noticed that my Arduino app sometimes shuts down the Serial Monitor just after the upload - sometimes it's enough time to write a first sentence, like myCounter=1, in this case, and then it goes off, all greyed. So I have to restart the app in order for it working again. Anyways, no big deal.
So, just a little update:
Thanks for that! I'm already applying some of the concepts, not only about functions. I changed some stuff to make them easier, like getting some repetitive values and instead of writing them all, creating arrays, it at least leave stuff more organized.
Reading it I got what gcjr said back there:
So I'm trying to take stuff out o loop and just call the functions, but didn't succeeded so far. I'm basically creating a void DIAL1 { code that was previously in the loop} and, inside the loop, changing it for DIAL1. Does that sound right? Anyway, didn't work, but I'm still reading stuff to figure it out.
This also sounds a lot more clear to me, although I still gotta go on learning, but yeah, makes a lot of sense and it's now my goal.
The other thing I did was changing the way I was doing the switch, and it finally worked! So now I could turn the knob to change a parameter in Lightroom, click the button and start changing another parameter. Then I got to a whole new problem.
Let me try to explain.
Turning the knob in the case 1 will send different values of a particular control value to Ligthroom, from 0 to 127, starting in 64 (which is the mid point, as all of the Lightroom sliders I'm controlling have pos and neg values, starting on 0). The case 2 does the same, but in another control value. So far, so good! The problem is, if I turn the knob to a value 80 in case 1, when i switch to case 2, its value will be on 80, which makes me assume that the encoder itself is storing its literal position. If this is true, I'd probably have to change something in the library, but it's complicated for me. So maybe I'll have to try another different approach, unless any of you have a suggestion!
I don't know if that suggestion would work in this case, but I'd still have to read and study a bit to be able to apply it.
Anyways, that's already a lot! Thank you very much for your help, my friends. I send you my best!!
Sending the whole new code here, in case you wanna take a look!
// MIDI Controller with X knobs
// WARNING: works only with 32u4 and SAMD based boards (Leonardo, Micro etc.)
// Libraries
// https://www.arduino.cc/reference/en/language/functions/usb/keyboard/
#include <Arduino.h>
#include <RotaryEncoder.h>
#include "Keyboard.h"
#include "MIDIUSB.h"
// Liquid Crystal Pins
#include <LiquidCrystal.h>
const int RS = A5, EN = A4, D4 = A3, D5 = A2, D6 = A1, D7 = A0;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
int lastPos[4] = { -1, -1, -1, -1};
bool lastButtonState[5] = {0, 0, 0, 0, 0};
bool buttonState1 = 0;
int value = 0;
// Dial 1
#define OUTPUT_B 3 //CLK
#define OUTPUT_A 2 //DT
#define BUTTON 1
// Lr Dial1 Switch Modes
int modeDial0 = 1;
int modeDial0Max = 3;
// Dial 2
#define OUTPUT_B1 6 //CLK
#define OUTPUT_A1 5 //DT
#define BUTTON1 4
// Lr Dial2 Switch Modes
int modeDial1 = 1;
int modeDial1Max = 3;
// Dial 3
#define OUTPUT_B2 9 //CLK
#define OUTPUT_A2 8 //DT
#define BUTTON2 7
// Lr Dial3 Switch Modes
int modeDial2 = 1;
int modeDial2Max = 3;
// Dial 4
#define OUTPUT_B3 12 //CLK
#define OUTPUT_A3 11 //DT
#define BUTTON3 10
// Lr Dial4 Switch Modes
int modeDial3 = 1;
int modeDial3Max = 3;
// Encoders parameters
#define ROTARYSTEPS 1
#define ROTARYMIN 0
#define ROTARYMAX 127
// Setup a RotaryEncoder with 4 steps per latch for the 2 signal input pins:
RotaryEncoder encoder0(OUTPUT_A, OUTPUT_B, RotaryEncoder::LatchMode::FOUR3);
RotaryEncoder encoder1(OUTPUT_A1, OUTPUT_B1, RotaryEncoder::LatchMode::FOUR3);
RotaryEncoder encoder2(OUTPUT_A2, OUTPUT_B2, RotaryEncoder::LatchMode::FOUR3);
RotaryEncoder encoder3(OUTPUT_A3, OUTPUT_B3, RotaryEncoder::LatchMode::FOUR3);
// Mode Button with 2 modes
#define MODE 13 // pin 13
int mode = 1;
int modeMax = 3;
// MIDI CONFIG
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);
}
long lastClickTime = 0;
long tempCount = 0;
// - - - - - - - - - - - - - - - - - SETUP - - - - - - - - - - - - - - - - - - - //
void setup() {
Serial.begin(115200);
// Enable Keyboard Library
Keyboard.begin();
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
// Rotary Encoders positions
encoder0.setPosition(64 / ROTARYSTEPS);
encoder1.setPosition(64 / ROTARYSTEPS);
encoder2.setPosition(64 / ROTARYSTEPS);
encoder3.setPosition(64 / ROTARYSTEPS);
// Dial 1 pins
pinMode(OUTPUT_A, INPUT);
pinMode(OUTPUT_B, INPUT);
pinMode(BUTTON, INPUT_PULLUP);
//Dial 2 pins
pinMode(OUTPUT_A1, INPUT);
pinMode(OUTPUT_B1, INPUT); pinMode(OUTPUT_B1, INPUT);
pinMode(BUTTON1, INPUT_PULLUP);
//Dial 3 pins
pinMode(OUTPUT_A2, INPUT);
pinMode(OUTPUT_B2, INPUT); pinMode(OUTPUT_B1, INPUT);
pinMode(BUTTON2, INPUT_PULLUP);
//Dial 4 pins
pinMode(OUTPUT_A3, INPUT);
pinMode(OUTPUT_B3, INPUT); pinMode(OUTPUT_B1, INPUT);
pinMode(BUTTON3, INPUT_PULLUP);
// Mode trigger pin
pinMode(MODE, INPUT_PULLUP);
}
// - - - - - - - - - - - - - - - - - LOOP - - - - - - - - - - - - - - - - - - - //
void loop() {
static int pos[4] = {0, 0, 0, 0};
int newPos[4] = {encoder0.getPosition() * ROTARYSTEPS, encoder1.getPosition() * ROTARYSTEPS, encoder2.getPosition() * ROTARYSTEPS, encoder3.getPosition() * ROTARYSTEPS};
encoder0.tick();
encoder1.tick();
encoder2.tick();
encoder3.tick();
// MODE SWITCH - - - - - - - - - - - - - - //
if (digitalRead(MODE) == LOW) {
if (lastButtonState[4] == HIGH) {
mode ++;
lcd.setCursor(0, 1);
lcd.print(" ");
if (mode == modeMax) {
mode = 1;
}
}
delay(500);
Keyboard.releaseAll();
Serial.println(mode);
lastClickTime = millis();
lastButtonState[4] = LOW;
}
else {
lastButtonState[4] = HIGH;
}
// END OF MODE SWITCH - - - - - - - - - - - //
// CASES - TOOGLE BETWEEN PREMIERE AND LIGHTROOM //
switch (mode) {
case 1: // PREMIERE
lcd.setCursor(0, 0);
lcd.print("Premiere ");
// Dial 1 - - - - - - - - - - - - - - - - - - - - - - -
if (pos[0] != newPos[0]) {
if ((int)(encoder0.getDirection()) == 1) {
Keyboard.press(KEY_RIGHT_ARROW);
delay(8);
}
else {
Keyboard.press(KEY_LEFT_ARROW);
delay(8);
}
Keyboard.releaseAll();
pos[0] = newPos[0];
}
// Dial 1 trigger
if (digitalRead(BUTTON) == LOW) {
if (lastButtonState[0] == HIGH) {
Keyboard.print(" ");
delay(8);
}
lastButtonState[0] = LOW;
}
else {
lastButtonState[0] = HIGH;
}
// END DIAL 1 - - - - - - - - - - - - - - - - - - - - - -
// Dial 2- - - - - - - - - - - - - - - - - - - - - - -
if (pos[1] != newPos[1]) {
if ((int)(encoder1.getDirection()) == 1) {
Keyboard.press(KEY_UP_ARROW);
delay(8);
}
else {
Keyboard.press(KEY_DOWN_ARROW);
delay(8);
}
Keyboard.releaseAll();
pos[1] = newPos[1];
}
// Dial 2 trigger
if (digitalRead(BUTTON1) == LOW) {
if (lastButtonState[1] == HIGH) {
Keyboard.press(KEY_LEFT_GUI); // This is Mac Command key
Keyboard.press('k');
delay(100);
Keyboard.releaseAll();
}
lastButtonState[1] = LOW;
} else {
lastButtonState[1] = HIGH;
}
// END DIAL 2 - - - - - - - - - - - - - - - - - - - - - - - -
// Dial 3- - - - - - - - - - - - - - - - - - - - - - -
if (pos[2] != newPos[2]) {
if ((int)(encoder2.getDirection()) == 1) {
Keyboard.press(KEY_UP_ARROW);
delay(8);
}
else {
Keyboard.press(KEY_DOWN_ARROW);
delay(8);
}
Keyboard.releaseAll();
pos[2] = newPos[2];
}
// Dial 2 trigger
if (digitalRead(BUTTON2) == LOW) {
if (lastButtonState[2] == HIGH) {
Keyboard.press('k');
delay(100);
Keyboard.releaseAll();
}
lastButtonState[2] = LOW;
} else {
lastButtonState[2] = HIGH;
}
// END DIAL 3 - - - - - - - - - - - - - - - - - - - - - - - -
// Dial 4- - - - - - - - - - - - - - - - - - - - - - -
if (pos[3] != newPos[3]) {
if ((int)(encoder3.getDirection()) == 1) {
Keyboard.press(KEY_UP_ARROW);
delay(8);
}
else {
Keyboard.press(KEY_DOWN_ARROW);
delay(8);
}
Keyboard.releaseAll();
pos[3] = newPos[3];
}
// Dial 4 trigger
if (digitalRead(BUTTON3) == LOW) {
if (lastButtonState[3] == HIGH) {
Keyboard.press('p');
delay(100);
Keyboard.releaseAll();
}
lastButtonState[3] = LOW;
} else {
lastButtonState[3] = HIGH;
}
// END DIAL 4 - - - - - - - - - - - - - - - - - - - - - - - -
break;
// = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = CASE 1 / PREMIERE - END //
case 2: // LIGHTROOM
lcd.setCursor(0, 0);
lcd.print("Lightroom ");
buttonState1 = digitalRead(BUTTON);
if (buttonState1 != lastButtonState[1]) {
if (buttonState1 == LOW) {
value = 1 - value;
}
delay(50);
lastButtonState[1] = buttonState1;
}
switch (value) {
case 0:
lcd.setCursor(0, 1);
lcd.print("Exposure ");
if (newPos[0] < ROTARYMIN) {
encoder0.setPosition(ROTARYMIN / ROTARYSTEPS);
newPos[0] = ROTARYMIN;
} else if (newPos[0] > ROTARYMAX) {
encoder0.setPosition(ROTARYMAX / ROTARYSTEPS);
newPos[0] = ROTARYMAX;
} // if
if (lastPos[0] != newPos[0]) {
controlChange(0, 101, newPos[0]);
MidiUSB.flush();
delay(2);
lastPos[0] = newPos[0];
}
break;
case 1:
lcd.setCursor(0, 1);
lcd.print("Contrast ");
if (newPos[0] < ROTARYMIN) {
encoder1.setPosition(ROTARYMIN / ROTARYSTEPS);
newPos[0] = ROTARYMIN;
} else if (newPos[0] > ROTARYMAX) {
encoder1.setPosition(ROTARYMAX / ROTARYSTEPS);
newPos[0] = ROTARYMAX;
} // if
if (lastPos[0] != newPos[0]) {
controlChange(0, 100, newPos[0]);
MidiUSB.flush();