Teensy MIDI Controller

After some deliberation, I have removed most of this blog post. If you want to make improvements, or make suggestions, please make a request on Github. Doing so makes things better for everyone, not just you. The way of fork and pull is the only way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Teensy Midi Controller
// Philip Cunningham
// http://philipcunningham.org
// Smoothing hacked from
// http://www.arduino.cc/en/Tutorial/Smoothing

// Smoothing amount
const int numReadings = 30;

// Set the readings from the analog input to 0
int readings[numReadings] = {0,0,0,0,0};

// The number of pots
int pots = 6;

// The index of the current reading
int index = 0;

// Running total
int total = 0;

// Pin total
int pintotal[6] = {0,0,0,0,0};

// Pin average
int average[6] = {0,0,0,0,0};

// Set midi channel to send to 1
int channel = 1;

// Set the last readings to 0
int lastreading[6] = {0,0,0,0,0};

void setup()
{
  // Standard midi rate
  Serial.begin(31250);
}

void loop()
{
  // Get reading for all potentiometers
  for (int i = 0; i < pots; i++ ) {

    // Set index to zero
    index = 0;

    // Loop to create average reading
    for (int j = 0; j < numReadings; j++ ){
      // Subtract the last reading
      total = total - readings[index];
      // Read from the sensor
      readings[index] = analogRead(i);
      // Add the reading to the total
      total = total + readings[index];
      // Advance to the next position in the array
      index = 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]){

   // Send midi data
   usbMIDI.sendControlChange(i, average[i], channel);
  }
  // Set last array value to current value
  lastreading[i] = average[i];
  }
  // Delay output by 5ms
  delay(5);
}