Skip to content

Instantly share code, notes, and snippets.

@incredimike
Created June 11, 2018 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save incredimike/b798875138ae779fc26e1e281ee01138 to your computer and use it in GitHub Desktop.
Save incredimike/b798875138ae779fc26e1e281ee01138 to your computer and use it in GitHub Desktop.
// Minimidi Teensy Midi Controller
// Mike Walker (http://incredimike.com)
// Based on code originally by Philip Cunningham (http://philipcunningham.org)
// Smoothing hacked from http://www.arduino.cc/en/Tutorial/Smoothing
// Set midi channel to send to 1
int channel = 1;
// Smoothing amount
const int numReadings = 10;
int pots_total = 2;
int pots[2] = {16, 17}; // Total Pots
//int pots[2] = {PIN_A4,PIN_A5}; // Total Pots
// Set the readings from the analog input to 0
int readings[numReadings] = {0,0,0,0,0};
int pintotal[6] = {0,0,0,0,0,0};
int average[6] = {0,0,0,0,0,0}; // Pin average
int lastreading[6] = {0,0,0,0,0,0};
// The reading_index of the current reading
int reading_index = 0;
// Running total
int total = 0;
// Button tracking
int button_total = 2;
int button_pin[2] = {0,1};
int button_status[2] = {HIGH,HIGH};
int button_last[2] = {HIGH,HIGH};
// On-board LED
int led_board = 11;
void setup()
{
// Standard midi rate
Serial.begin(31250);
// Serial.begin(38400); // debuggin
// Button pins use internal pull-up resistor
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
// Turn on the LED on the teensy board
pinMode(led_board, OUTPUT);
digitalWrite(led_board, HIGH);
}
void loop()
{
//
// Pots - including Smoothing functionality
//
for (int i = 0; i < pots_total; i++ ) {
// Set reading_index to zero
reading_index = 0;
// Loop to create average reading
for (int j = 0; j < numReadings; j++ ){
// Subtract the last reading
total = total - readings[reading_index];
// Read from the sensor
readings[reading_index] = analogRead(pots[i]);
//Serial.println(readings[reading_index]);
// Add the reading to the total
total = total + readings[reading_index];
// Advance to the next position in the array
reading_index++;
pintotal[i] = total;
}
// Calculate the average
// Divide by 8 to make between 0-127
average[i] = (pintotal[i]/numReadings)/8;
// If reading is different from the previous
if (lastreading[i] != average[i]){
usbMIDI.sendControlChange(i, average[i], channel);
// Serial.print(i); // debugging
// Serial.print('-'); // debugging
// Serial.println(average[i]); // debugging
}
// Set last array value to current value
lastreading[i] = average[i];
}
//
// Buttons
//
for (int i = 0; i < button_total; i++ ) {
// Read in button status
button_status[i] = digitalRead(button_pin[i]);
// Check if current status is different than last check
if (button_status[i] != button_last[i]) {
// If different, set last check value to current.
button_last[i] = button_status[i];
// Send message to midi out. (start numbering buttons controls after pots)
usbMIDI.sendControlChange(10+button_pin[i], button_last[i], channel);
// Serial.println(button_pin[i]); // debugging
// Serial.println(button_last[i]); // debugging
}
}
// Delay output by 50ms
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment