Go Down

Topic: Help with Arduino basic midi controller! (Read 1 time) previous topic - next topic

julijet60

Hello!, I am building a very simple midi controller, with 5 potentiometers (one for each analog in)
I have based on this:


and on this code:
Code: [Select]
void setup()
{
  Serial.begin(31250);       // Default speed of MIDI serial port
  pinMode(13, OUTPUT);       // Light LED on pin 13 to notify of readynes
  digitalWrite(13, HIGH);
}

int iAn0Val, iAn1Val;

void loop()
{
  //Slider X
  int iAn0ValPrev = iAn0Val; //Get the previous value of slider X
  iAn0Val = analogRead(0)/8;
  analogPinMidiTX(1,iAn0Val,iAn0ValPrev); //TX the value of slider X
 
  //Slider Y
  int iAn1ValPrev = iAn1Val; //Get the previous value of slider Y
  iAn1Val = analogRead(1)/8;
  analogPinMidiTX(2,iAn1Val,iAn1ValPrev); //TX the value of slider Y
}

void analogPinMidiTX(int iChan, int iVal, int iValPrev)
{  
 //only TX the value over midi if it is changed, as to prevent spamming the midi port and thus confusing the receiving application in learning mode
 if(iValPrev != iVal)
 {
   MidiTX(176,iChan,iVal);
 }
}

void MidiTX(unsigned char MESSAGE, unsigned char CONTROL, unsigned char VALUE) //pass values out through standard Midi Command
{
  Serial.print(MESSAGE);
  Serial.print(CONTROL);
  Serial.print(VALUE);
}


The problem is that the code is made for been used with 2 potentiometers, and I want to connect 5. How can I fix the code for using it with 5 potentiometers?

Sorry for my bad english

Grag38

just modify the code and add :

Code: [Select]


int iAn0Val, iAn1Val;
int iAn2Val, iAn3Val, iAn4Val;

void loop()
{
   //Slider X
   int iAn0ValPrev = iAn0Val; //Get the previous value of slider X
   iAn0Val = analogRead(0)/8;
   analogPinMidiTX(1,iAn0Val,iAn0ValPrev); //TX the value of slider X
   
   //Slider Y
   int iAn1ValPrev = iAn1Val; //Get the previous value of slider Y
   iAn1Val = analogRead(1)/8;
   analogPinMidiTX(2,iAn1Val,iAn1ValPrev); //TX the value of slider Y

// pot#3
   int iAn2ValPrev = iAn2Val; //Get the previous value of pot#3
   iAn2Val = analogRead(2)/8;
   analogPinMidiTX(3,iAn2Val,iAn2ValPrev); //TX the value of pot#3
// pot #4
   int iAn3ValPrev = iAn3Val; //Get the previous value of pot#4
   iAn3Val = analogRead(3)/8;
   analogPinMidiTX(4,iAn3Val,iAn3ValPrev); //TX the value of pot#4
// pot #5
   int iAn4ValPrev = iAn4Val; //Get the previous value of pot#5
   iAn4Val = analogRead(4)/8;
   analogPinMidiTX(2,iAn4Val,iAn4ValPrev); //TX the value of pot#5

}



But the best is to use array of values to code it, like :

Code: [Select]

int iAnVal[5];
int ianValPrev[5];

void loop()
{
   byte i;
     
   for(i=0; i<5; i++)
   {
     iAnValPrev[i] = iAnVal[i]; //Get the previous value of pot number i
     iAnVal[i] = analogRead(i)/8;   // Read the value of pot number i
     analogPinMidiTX(i+1,iAnVal[i],iAnValPrev[i]); //TX the value of pot number i to channel i
   }
}



Go Up