Potentiometers, rotary encoder and HDD motor to midi

Hello everyone.
I'm a mechanical engineering from Greece and im really new to all this. I have small experience with coding and i would like your help.

I'm trying to build a Midi Controller for DAW (Cubase).
The functions I want to use will consist of:

  • 2 potentiometers (1 linear 500K and 1 slide 10K)
  • 1 rotary encoder
  • 6 to 8 push buttons
  • 1 HDD motor

I'm using a chinese arduino nano (ATmega168) and those softwares:

  • Loopbe1
  • Hairless MIDI

So far i have managed to operate and function with the potentiometers code and the rotary encoder code but as individual codes (Loopbe1 -> Hairless MIDI -> Mapping Cubase -> fully functional).

Also i have managed HDD motor to work but i don't know how to customize the code to "give" Midi outs to be able to work with Loopbe1 and Hairless MIDI.

I have not yet dealt with the push buttons although from a little research I have done it seems to be an easier case.

The help i need is to customise the HDD motor code so it can "communicate" with midi.
Also, as i told at first place, i'm a newbie. How do we merge all codes into one?
Last but not least, can you advise me for an easy to customise push button code for midi operation?

potentiometers code:

[int v0 = 0;
int v0_ = 0;
int v1 = 0;
int v1_ = 0;
int threshold = 5;


int int_to_midi_ratio = 1024 / 128;
void SendMidiToSerial (unsigned char word0, unsigned char word1, unsigned char word2) {
  Serial.write(word0);
  Serial.write(word1);
  Serial.write(word2);
}
void setup () {
  Serial.begin(115200);
}
void loop () {
  v0 = analogRead(0) / int_to_midi_ratio;
  v1 = analogRead(1) / int_to_midi_ratio;
 
  if (v0 - v0_ >= threshold || v0_ - v0 >= threshold) {
    v0_ = v0;
    SendMidiToSerial(176, 42, v0);
  }
 
  if (v1 - v1_ >= threshold || v1_ - v1 >= threshold) {
    v1_ = v1;
    SendMidiToSerial(176, 43, v1);
  }
}/code]



Rotary encoder code:
[code]
int pinA = 2;
int pinB = 3;
int pinC = 4;


int int_to_midi_ratio = 1024 / 128;
void SendMidiToSerial (unsigned char word0, unsigned char word1, unsigned char word2) {
  Serial.write(word0);
  Serial.write(word1);
  Serial.write(word2);
}


volatile int previous = 0;
volatile int counter = 0;

void setup() {

  Serial.begin(57600);

 
  pinMode(pinA, INPUT_PULLUP);
  pinMode(pinB, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(pinA), changed, CHANGE);
  attachInterrupt(digitalPinToInterrupt(pinB), changed, CHANGE);
 
 
}

void changed() {
  int A = digitalRead(pinA);
  int B = digitalRead(pinB);

  int current = (A << 1) | B;
  int combined  = (previous << 2) | current;
   
  if(combined == 0b0010 ||
     combined == 0b1011 ||
     combined == 0b1101 ||
     combined == 0b0100) {
    counter++;
  }
   
  if(combined == 0b0001 ||
     combined == 0b0111 ||
     combined == 0b1110 ||
     combined == 0b1000) {
    counter--;
  }

  previous = current;
}

void loop()
{
  if(digitalRead(4)==LOW)
  {
    //Send the message
    SendMidiToSerial(176, 47, pinC);
  }
  if(counter >= 1) {
    SendMidiToSerial(176, 44, pinA);
    counter -= 1;
  } else if(counter <= -1) {
    SendMidiToSerial(176, 45, pinB);

    counter += 1;
  }
}

HDD motor code:

#define L1  A2
#define L2  A3
#define L3  A4

int CW = 0;
int CCW = 0;



boolean L1Val = 0;
const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
 
void setup() {
  ADCSRA &= ~PS_128;
  ADCSRA |= (1 << ADPS1) | (1 << ADPS0);
  Serial.begin(115200);
  analogReference(INTERNAL);
  pinMode(L1, INPUT);
  pinMode(L2, INPUT);
  pinMode(L3, INPUT);

}

void loop() {


   if ((analogRead(L1) > 80) && (L1Val == 0)) {
    L1Val = 1;
  }

  if ((L1Val == 1) && (analogRead(L2) > 60)) {
    CW = L2 + 10;
    Serial.println(CW);
    L1Val = 0;
    delay(10);
 
  }


 
  if ((L1Val == 1) && (analogRead(L3) > 60)) {
    CCW = L3 - 10;
    Serial.println(CCW);
    L1Val = 0;
    delay(10);

  }


 
}

Also a small demonstration:

Thank you

  • 2 potentiometers (1 linear 500K and 1 slide 10K)

500k is a large value for analogRead, I'd suggest adding a 100nF capacitor to ground to its
wiper to reduce the impedance seen by the ADC.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.