code

////////////////////////////////////////////////////
// mHzKontrol //
// designed and developed by Mo H. Zareei //
// www.m-h-z.net copyright © 2013 m H z //
////////////////////////////////////////////////////

 

#include <MIDI.h> //// see <http://www.dimitridiakopoulos.com/hiduino>

long previousMillis = 0;
long interval= 60;
boolean MapMode;

//———- ANALOG SENSORS —————–

int AnaPins[] = {
A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15};
int LedPins[] = {
10,11,12,13};

int AnaPinsNum = 16;
int AnaIn[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

//————— LOW-PASS FILTER ————

int AnaInZ[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int AnaLP[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int ZAnaLP[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

//————— DIGITAL SENSORS ————

int AllButtons[32];
int ZAllButtons[32];

int BpsPins[] = {
22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52};
int BplPins[] = {
23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53};

int BpsIn[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int BplIn[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

int ButPins[] = {
2,3,4,5};
int ButIn[] = {
0,0,0,0};

int BsPins[] = {
14,15,16,17,18,19,20,21};
int BsIn[] = {
0,0,0,0,0,0,0,0};

int BnPins[] = {
6,7,8,9};
int BnIn[] = {
0,0,0,0};
void setup()
{
MIDI.begin(MIDI_CHANNEL_OMNI);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);

for (int i = 0; i < 16; i++)
{

if (i < 4)
{
pinMode(BnPins[i], INPUT);
}
if (i < 8)
{
pinMode(BsPins[i], INPUT);
}
pinMode(BpsPins[i], INPUT);
pinMode(BplPins[i], OUTPUT);
}

///////// SETTING UP MAPPING MODE FOR START UP /////////

if ( digitalRead(2) == HIGH && digitalRead(5) == HIGH && digitalRead(6) == HIGH && digitalRead(9) == HIGH)
{
MapMode = true;
}
else
{
MapMode = false;
}
}

void loop()
{
if (MapMode == true)
{
unsigned long currentMillis = millis();

if (currentMillis – previousMillis > interval)
{
for (int i = 0; i < AnaPinsNum; i++)
{
AnaIn[i] = analogRead(AnaPins[i]);
if (i < 8)
{
AnaIn[i] = map(AnaIn[i], 10, 1010, 0, 127);
AnaIn[i] = constrain(AnaIn[i], 0, 127);
}
else if (i < 12 && i > 7)
{
AnaIn[i] = map(AnaIn[i], 5, 1020, 0, 127);
AnaIn[i] = constrain(AnaIn[i], 0, 127);
}
else
{
AnaIn[i] = map(AnaIn[i], 50, 550, 0, 127);
AnaIn[i] = constrain(AnaIn[i], 0, 127);
}

AnaLP[i] = (AnaIn[i] + ZAnaLP[i])*0.5;
if (ZAnaLP[i] > AnaLP[i]+1 || ZAnaLP[i]< AnaLP[i]-1)
{
MIDI.sendControlChange(i, AnaLP[i], 1);
}

//############# IR SENSOR LEDs #############
if (i > 11 )
{
analogWrite(i-2, AnaLP[i]);
}
previousMillis = currentMillis;
ZAnaLP[i] = AnaLP[i];
}
}

}
else if (MapMode == false)
{
for (int i = 0; i < AnaPinsNum; i++)
{
AnaIn[i] = analogRead(AnaPins[i]);
if (i < 8)
{
AnaIn[i] = map(AnaIn[i], 10, 1010, 0, 127);
AnaIn[i] = constrain(AnaIn[i], 0, 127);
}
else if (i < 12 && i > 7)
{
AnaIn[i] = map(AnaIn[i], 5, 1020, 0, 127);
AnaIn[i] = constrain(AnaIn[i], 0, 127);
}
else
{
AnaIn[i] = map(AnaIn[i], 50, 550, 0, 127);
AnaIn[i] = constrain(AnaIn[i], 0, 127);
}

AnaLP[i] = (AnaIn[i] + ZAnaLP[i])*0.5;
if (ZAnaLP[i] != AnaLP[i])
{
MIDI.sendControlChange(i, AnaLP[i], 1);
}

//############# IR SENSOR LEDs #############
if (i > 11 )
{
analogWrite(i-2, AnaLP[i]);
}

ZAnaLP[i] = AnaLP[i];
}
}

//###### BUTTONS #######

for (int j = 0; j < 32; j++)
{
if (j < 4)
{
AllButtons[j] = BnIn[j];
}
else if (j < 8)
{
AllButtons[j] = ButIn[j-4];
}
else if (j < 16)
{
AllButtons[j] = BsIn[j-8];
}
else
{
AllButtons[j] = BpsIn[j-16];
}
if (j < 4) //////// 4 NAVIGATING BUTTONS/////////////
{
BnIn[j] = digitalRead(BnPins[j]);
}

if (j > 3 && j < 8) //////// 4 PUSH BUTTONS //////////
{
ButIn[j-4] = digitalRead(ButPins[j-4]);
}
if (j > 7 && j < 16) //////// 8 SLIDER BUTTONS ///////
{
BsIn[j-8] = digitalRead(BsPins[j-8]);
}

if (j > 15) /////////// 16 BUTTON PADS ////////////////
{
BpsIn[j-16] = digitalRead(BpsPins[j-16]);

if (BpsIn[j-16] == LOW)
{
digitalWrite(BplPins[j-16], HIGH);
}
else
{
digitalWrite(BplPins[j-16], LOW);
}
}

//——————– SENDING MIDI———————–

if (AllButtons[j] == HIGH && ZAllButtons[j] == LOW)
{
MIDI.sendControlChange(j+16, 127, 1);
}

if (AllButtons[j] == LOW && ZAllButtons[j] == HIGH)
{
MIDI.sendControlChange(j+16, 0, 1);
}

ZAllButtons[j] = AllButtons[j];
}
}