#include "MIDIUSB.h"
#include <CD74HC4067.h> // Biblioteka do obsługi multiplexera CD74HC4067
// Arduino (pro)micro midi functions MIDIUSB Library
// Deklaracje funkcji
void noteOn(byte channel, byte pitch, byte velocity);
void controlChange(byte channel, byte control, byte value);
CD74HC4067 multiplexer(5, 4, 3, 2); // Inicjalizacja multiplexera z odpowiednimi pinami sterującymi
// BUTTONS (Two buttons as per your original code)
const int N_BUTTON = 2;
byte buttonPin[N_BUTTON] = {6, 7};
byte buttonState[N_BUTTON] = {0};
byte buttonPState[N_BUTTON] = {0};
int debounceDelay = 10;
unsigned long lastDebounceTime[N_BUTTON] = {0};
unsigned long debounceTimer[N_BUTTON] = {0};
// POTENTIOMETERS
const int N_POT = 11; // Number of faders
byte midiState[N_POT] = {0};
byte midiPState[N_POT] = {0};
int varThreshold = 300;
unsigned long potTimer[N_POT] = {80};
unsigned long lastPotTime[N_POT] = {0};
const int POT_TIMEOUT = 300;
boolean potGate[N_POT] = {false};
byte MIDI_CH = 0;
byte CC[N_POT] = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}; // Assign different CC numbers for each fader
void setup() {
Serial.begin(115200);
for (int i = 0; i < N_BUTTON; i++) {
pinMode(buttonPin[i], INPUT_PULLUP);
}
multiplexer.channel(A0); // Wybierz pierwszy kanał na początek
}
void loop() {
buttons();
potentiometers();
}
void buttons() {
// Your button handling code here
// (Same as your existing code)
}
void potentiometers() {
for (int i = 0; i < N_POT; i++) {
// Select the appropriate channel on the multiplexer
multiplexer.channel(i);
// Read the value from the potentiometer
int potState = analogRead(A0); // Wykorzystaj A0 jako pin analogowy do odczytu wartości potencjometra
// Map the value to the MIDI range
byte midiValue = map(potState, 0, 1023, 0, 127);
int potVar = abs(potState - midiPState[i]);
if (potVar > varThreshold) {
lastPotTime[i] = millis();
}
potTimer[i] = millis() - lastPotTime[i];
if (potTimer[i] < POT_TIMEOUT) {
potGate[i] = true;
} else {
potGate[i] = false;
}
if (potGate[i] == true) {
if (midiValue != midiPState[i]) {
controlChange(MIDI_CH, CC[i], midiValue);
MidiUSB.flush();
Serial.print("Fader ");
Serial.print(i);
Serial.print(": ");
Serial.print(midiValue);
Serial.println();
midiPState[i] = midiValue;
}
}
}
}