Last active
June 12, 2020 20:00
-
-
Save ubidefeo/6098d95332b6529f1b0599bfd443ebae to your computer and use it in GitHub Desktop.
Arduino MIDI controller Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Arduino analog to USB MIDI | |
Ubi de Feo @ Bar Arduino | |
https://www.youtube.com/watch?v=0zNmt_IKwRg | |
supported boards: | |
- Arduino MKR Family | |
- Arduino Nano 33 IoT | |
**** Sketch for BLE MIDI (Nano BLE and Nano BLE Sense) **** | |
**** can be found here https://gist.github.com/ubidefeo/3e7362235cee5317cec4a36f07585e29 **** | |
**** Thank you Larry Bank for pointing out the lack of a BLE version **** | |
*/ | |
#include "MIDIUSB.h" | |
void controlChange(byte channel, byte control, byte value) { | |
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value}; | |
MidiUSB.sendMIDI(event); | |
} | |
const int SAMPLES_COUNT = 20; | |
// FIFO | |
int samplesBuffer[SAMPLES_COUNT] = {0}; | |
int sampleIndex = 0; | |
const int knobInputPin = A1; | |
const int changeThreshold = 20; | |
int lastSensorValue; | |
int lastMappedValue; | |
unsigned long samplesSum = 0; | |
void setup() { | |
Serial.begin(57600); | |
delay(5000); | |
Serial.println("SETUP"); | |
} | |
void loop() { | |
int knobReadOut = filterAnalogValue(knobInputPin); | |
int mappedValue = map(knobReadOut, 0, 1023, 0, 127); | |
if(lastMappedValue != mappedValue){ | |
// Serial.println("************ CHANGE ***************"); | |
// Serial.print(">>> "); | |
// Serial.print(mappedValue); | |
// Serial.println(" <<<"); | |
controlChange(0, 4, mappedValue); | |
} | |
lastMappedValue = mappedValue; | |
} | |
int filterAnalogValue(int _inputPin){ | |
samplesSum = 0; | |
int analogValue = analogRead(_inputPin); | |
//int valueDelta = abs(analogValue - lastSensorValue); | |
// Serial.print(analogValue); | |
// Serial.print(" - "); | |
// Serial.println(lastSensorValue); | |
// if(valueDelta < changeThreshold){ | |
// analogValue = lastSensorValue; | |
// } | |
if(sampleIndex < SAMPLES_COUNT){ | |
samplesBuffer[sampleIndex] = analogValue; | |
sampleIndex++; | |
}else{ | |
for(int samplePosition = 0; samplePosition < sampleIndex - 1; samplePosition++){0; | |
samplesBuffer[samplePosition] = samplesBuffer[samplePosition + 1]; | |
} | |
samplesBuffer[SAMPLES_COUNT - 1] = analogValue; | |
} | |
for(int samplePosition = 0; samplePosition < sampleIndex; samplePosition++){0; | |
samplesSum += samplesBuffer[samplePosition]; | |
} | |
//lastSensorValue = analogValue; | |
if(samplesSum == 0) return 0; | |
unsigned int filteredValue = samplesSum / sampleIndex; | |
return filteredValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey @bitbank2
you can find the BLE Sketch here https://gist.github.com/ubidefeo/3e7362235cee5317cec4a36f07585e29
I've also added a comment to thank you for pointing it out :)