Created
March 30, 2013 22:20
-
-
Save niko-h/5278597 to your computer and use it in GitHub Desktop.
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
/** | |
* Author: Edward Vayne | |
* | |
* Modular Midi-Controller for Arduino | |
*/ | |
#include <MIDI.h> | |
// Variables: | |
const int switches[] = {4, 5, 6, 7, 8, 9, 10}; // pins for buttons | |
const int switchesAmount = sizeof(switches) / sizeof(int); | |
int switchStats[switchesAmount]; | |
int currentSwitchStats[switchesAmount]; | |
const int potis[] = {0, 1, 2}; // pins for potis | |
const int potisAmount = sizeof(potis) / sizeof(int); | |
int potiStats[potisAmount]; | |
int currentPotiStats[potisAmount]; | |
int midiCCSelects[potisAmount]; | |
void setup() { | |
MIDI.begin(4); // MIDI channel is 4 | |
MIDI.turnThruOff(); | |
pinMode(LEDpin, OUTPUT); | |
for(int i = 0; i < switchesAmount; i++) { // set the states of the switch pins | |
pinMode(switches[i], INPUT); | |
digitalWrite(switches[i], HIGH); // for arduino-internal pulloff-resistor | |
switchStats[i] = LOW; | |
currentSwitchStats[i] = LOW; | |
} | |
for(int i = 0; i < potisAmount; i++) { // set the states of the poti pins | |
potiStats[i] = 0; | |
currentPotiStats[i] = 0; | |
midiCCSelects[i] = 20+i; | |
} | |
} | |
void loop() { | |
for(int i = 0; i < switchesAmount; i++) { midiOnSwitch(i); } // compute each switch | |
for(int i = 0; i < potisAmount; i++) { midiOnPoti(i); } // compute each poti | |
} | |
void midiOnSwitch(int switchId) { | |
currentSwitchStats[switchId] = digitalRead(switches[switchId]); | |
if (currentSwitchStats[switchId] == HIGH && switchStats[switchId] == LOW) { // press | |
MIDI.sendNoteOn(switchId, 127, 1); // Send a Note (pitch i, velocity 127 on channel 1) | |
} | |
if (currentSwitchStats[switchId] == LOW && switchStats[switchId] == HIGH) { // release | |
MIDI.sendNoteOn(switchId, 0, 1); | |
} | |
switchStats[switchId] = currentSwitchStats[switchId]; | |
} | |
void midiOnPoti(int potiId) { | |
currentPotiStats[potiId] = analogRead(potis[potiId]) / 8; // n/8 for midi conform 0-127 | |
if ( currentPotiStats[potiId] != potiStats[potiId] ) { // if analog input has changed | |
MIDI.sendControlChange(midiCCSelects[potiId], currentPotiStats[potiId], 1); //send on cc#i | |
} | |
potiStats[potiId] = currentPotiStats[potiId]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment