// https://wokwi.com/projects/385841513818337281
// https://forum.arduino.cc/t/fader-pickup-catch-mode/1206227
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
void setup()
{
Serial.begin(31250);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Output Input");
}
bool pickupmode = true;
int midiVal;
const int FaderA = A0;
const int FaderB = A1;
int midiInVal = 127;
int temp = 127;
int threshold = 4;
void loop()
{
int valA = map(analogRead(FaderA),0,1023,0,127);
int valB = map(analogRead(FaderB),0,1023,0,127);
lcd.setCursor(0, 1);
lcd.print(midiVal);
lcd.print(" ");
lcd.print(midiInVal);
lcd.print(" ");
// In absolute mode (default behaviour)
if (!pickupmode){
midiVal = valA;
midiInVal = midiVal; // This line is just for the simulation
// Toggle to pickup mode (when received midi value is different to current midi out value)
if (valA != midiInVal) {
pickupmode = true;
}
if (valB != temp) {
pickupmode = true;
}
}
// In pick up/takeover mode
if (pickupmode){
midiInVal = valB; // This line is just for the simulation
temp = midiInVal;
// Toggle to absolute mode when value picked up
if (abs(valA - midiInVal) <= threshold) {
pickupmode = false;
}
}
Serial.print("valA = ");
Serial.print(valA);
Serial.print("\t");
Serial.print("valB = ");
Serial.print(valB);
Serial.print("\t");
Serial.print("Midi Out = ");
Serial.print(midiVal);
Serial.print("\t");
Serial.print("Midi In = ");
Serial.print(midiInVal);
Serial.print("\t");
Serial.print("Temp = ");
Serial.print(temp);
Serial.print("\t");
if (pickupmode){Serial.println("Pickup mode");
}
else { Serial.println("Absolute mode");
}
}
AAA
BBB