Need Help with Smoothing Analog Input for Midi

Hi there,
So far I have set up code that allows my arduino work as a usb midi device (Using HIDUINO). However, the analog inputs are constantly sending data which makes it impossible to assign them within DAWs such as Logic Pro. I have no idea how to do this and how to add it to my code :confused:

Here is the code, there are 6 analog potentiometer inputs I am using:

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

int mem = { 0, 0, 0, 0, 0, 0, };

// Variables:
int cc = 0;
int AnalogValue = 0; // define variables for the controller data
int lastAnalogValue = 0; // define the “lastValue” variables
int inVal = analogRead(0);
int outVal = map(inVal, 94, 555, 0, 127);

void setup()
{
MIDI.begin(MIDI_CHANNEL_OMNI);

Serial.begin(9600);

pinMode(1, INPUT_PULLUP);

}

void loop() {

AnalogValue = analogRead(0);
// convert to a range from 0 to 127:
cc = AnalogValue;
// check if analog input has changed
if (lastAnalogValue != cc) {
MIDI.sendControlChange(16,cc,1);
// update lastAnalogValue variable
lastAnalogValue = cc;

AnalogValue = analogRead(1);
// convert to a range from 0 to 127:
cc = AnalogValue/8;
// check if analog input has changed
if (lastAnalogValue != cc) {
MIDI.sendControlChange(17,cc,1);
// update lastAnalogValue variable
lastAnalogValue = cc;

AnalogValue = analogRead(2);
// convert to a range from 0 to 127:
cc = AnalogValue/8;
// check if analog input has changed
if (lastAnalogValue != cc) {
MIDI.sendControlChange(18,cc,1);
// update lastAnalogValue variable
lastAnalogValue = cc;
} // endif

AnalogValue = analogRead(3);
// convert to a range from 0 to 127:
cc = AnalogValue/8;
// check if analog input has changed
if (lastAnalogValue != cc) {
MIDI.sendControlChange(19,cc,1);
// update lastAnalogValue variable
lastAnalogValue = cc;
} // endif

AnalogValue = analogRead(4);
// convert to a range from 0 to 127:
cc = AnalogValue/8;
// check if analog input has changed
if (lastAnalogValue != cc) {
MIDI.sendControlChange(20,cc,1);
// update lastAnalogValue variable
lastAnalogValue = cc;
} // endif

AnalogValue = analogRead(5);
// convert to a range from 0 to 127:
cc = AnalogValue/8;
// check if analog input has changed
if (lastAnalogValue != cc) {
MIDI.sendControlChange(21,cc,1);
// update lastAnalogValue variable
lastAnalogValue = cc;
} // endif
}
}
}

You're using one variable for all of the inputs and cc outputs. The reason it keeps resending is that you're not checking if e.g. the value of A2 has changed since you last read it you're checking if A2 has the same value as the last pin you read which would have been A1.

You need some arrays so you can check changes against the the last values FOR THE SAME INPUT PIN.

Steve

Hi Steve, Could you help me out with an example? Sorry but I'm really new to coding.

first of all declare an Array

#define NUM_AN_VAL

lastanalogvalue[NUM_AN_VAL]

Create a function that reads, reduces to 7 bit and compares and sends

void MidiCheck(uint8_t nrvalue) { 
  uint16_t readvalue = analogRead(nrvalue); 
  uint8_t  cc = readvalue>>3;                               //  convert to a range from 0 to 127:     
  if (lastanalogvalue[nrvalue] != cc) {                    // check if analog input has changed
    MIDI.sendControlChange(nrvalue+16,cc,1);       // send midi    
    lastAnalogValue[nrvalue = cc;                          // update lastAnalogValue variable
  }
}

and call the function from a loopfor (uint8_t i=0; i<NUM_AN_VAL; i++) MidiCheck(i);