/*
Forum: https://forum.arduino.cc/t/midi-joystick-controller/1448889/4
Wokwi: https://wokwi.com/projects/467173218873121793
ec2021
2026/06/18
Added a print function for the real raw value ...
*/
// Include the Control Surface library
#include <Control_Surface.h>
// Instantiate a MIDI over USB interface.
USBMIDI_Interface midi;
// Create a new instance of the class `CCPotentiometer`, called `potentiometer`,
// on pin A0, that sends MIDI messages with controller 7 (channel volume)
// on channel 1.
CCPotentiometer potentiometer {A0, {MIDI_CC::Channel_Volume, Channel_1}};
// The maximum value that can be measured (usually 16383 = 2¹⁴-1)
constexpr analog_t maxRawValue = CCPotentiometer::getMaxRawValue();
// The filtered value read when potentiometer is at the 0% position
constexpr analog_t minimumValue = maxRawValue / 64;
// The filtered value read when potentiometer is at the 100% position
constexpr analog_t maximumValue = maxRawValue - maxRawValue / 64;
// A mapping function to eliminate the dead zones of the potentiometer:
// Some potentiometers don't output a perfect zero signal when you move them to
// the zero position, they will still output a value of 1 or 2, and the same
// goes for the maximum position.
analog_t mappingFunction(analog_t raw) {
// make sure that the analog value is between the minimum and maximum
raw = constrain(raw, minimumValue, maximumValue);
// map the value from [minimumValue, maximumValue] to [0, maxRawValue]
return map(raw, minimumValue, maximumValue, 0, maxRawValue);
}
void setup() {
// Add the mapping function to the potentiometer
potentiometer.map(mappingFunction);
// Initialize everything
Control_Surface.begin();
Serial.begin(115200);
}
void loop() {
// Update the Control Surface (check whether the potentiometer's
// input has changed since last time, if so, send the new value over MIDI).
Control_Surface.loop();
// Use this to find minimumValue and maximumValue: it prints the raw value
// of the potentiometer, without the mapping function
int v = analogRead(A0);
Serial.print(v);
Serial.print("\t");
Serial.println(potentiometer.getRawValue());
}