Merging the "Smoothing" and a MIDI Controller sketch

Hey guys! :slight_smile:

I am building a MIDI Controller with 3 potentiometers. For that I am trying to merge two codes. One of them is the Smoothing code and the other one is with arrays for having multiple analog inputs, without declaring every variable separately.

Here you can see the code I found with the arrays:

int controlChange = 176; // MIDI Kanal 1

int potiPin[] =           {A0,A1,A2,A3,A4,A5};
int controllerNummer[] =  {20,21,22,23,24,25};
int controllerWert[] =    {0, 0, 0, 0, 0, 0};
int controllerWertAlt[] = {0, 0, 0, 0, 0, 0};
int potiWert[] =          {0, 0, 0, 0, 0, 0};

int i = 0;

void setup() {

Serial.begin(9600);
  
}


void loop () {
  for (i = 0; i < 6; i++) {

  potiWert[i] = analogRead(potiPin[i]);
  controllerWert[i] = map(potiWert[i],0,1023,0,127);

   if (controllerWert[i] != controllerWertAlt[i]) {
     Serial.write(controlChange);
     Serial.write(controllerNummer[i]);
     Serial.write(controllerWert[i]);
    }

     controllerWertAlt[i] = controllerWert[i];
     delay(20);
    }
  }

and here is my code with the smoothing code in it:

int controlChange = 176;        // MIDI Kanal 1
int readings[3];      // the readings from the analog input
int readIndex[] = {0,0,0};              // the index of the current reading
int total[] = {0,0,0};                  // the running total
int average[] = {0,0,0};                // the average
int inputPin[] = {A0,A1,A2};
int averageMIDI[] = {0,0,0};
int averageMIDIAlt[] = {0,0,0};
int controllerNummer[] = {20,21,22};
int i = 0;




void setup() {
  // initialize serial communication with computer:
  Serial.begin(9600);
  // initialize all the readings to 0:
  }


void loop() {
  for (i = 0; i < 3; i++) {
  // subtract the last reading:
  total[i] = total[i] - readings[readIndex[i]];
  // read from the sensor:
  readings[readIndex[i]] = analogRead(inputPin[i]);
  // add the reading to the total:
  total[i] = total[i] + readings[readIndex[i]];
  // advance to the next position in the array:
  readIndex[i] = readIndex[i] + 1;

  // if we're at the end of the array...
  if (readIndex[i] >= 3) {
    // ...wrap around to the beginning:
    readIndex[i] = 0;
  }

  // calculate the average:
  average[i] = total[i] / 3;
  averageMIDI[i] = map(average[i],0,1023,0,127);
    if (averageMIDI[i] != averageMIDIAlt[i]) {
      Serial.write(controlChange);
      Serial.write(controllerNummer[i]);
      Serial.write(averageMIDI[i]);

      }

      averageMIDIAlt[i] = averageMIDI[i];
    delay(5); 
      } 
}

I tried to test whether I get any MIDI Signals by using hairless-midiserial, but for some reason I get the message: Warning: got a status byte when we were expecting 2 more data bytes, sending possibly incomplete MIDI message 0x80. :frowning:

Could you maybe help me by pointing out the mistakes I did? Thank you for your help in advance! :slight_smile:

I tried to test

…using what code? I don’t see any merged code in your post.

By “merged” I mean that I tried to use the Smoothing sketch with the code I posted to get more constant values but for some reason I was not able to make it work with arrays instead of writing every value out in a separate way.

I want this code

/*
  Smoothing

  Reads repeatedly from an analog input, calculating a running average and
  printing it to the computer. Keeps ten readings in an array and continually
  averages them.

  The circuit:
  - analog sensor (potentiometer will do) attached to analog input 0

  created 22 Apr 2007
  by David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Smoothing
*/

// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
const int numReadings = 3;     // Anzahl der Readings
int controlChange = 176;        // MIDI Kanal 1
int readings0[numReadings];     // the readings from the analog input
int readings1[numReadings];
int readIndex0 = 0;   
int readIndex1 = 0; // the index of the current reading
int total0 = 0; 
int total1 = 0;   // the running total
int average0 = 0;   
int average1 = 0;   // the average
int inputPin0 = A0;
int inputPin1 = A1;
int averageMIDI0 = 0;
int averageMIDI1 = 0;
int averageMIDIAlt0 = 0;
int averageMIDIAlt1 = 0;
int controllerNummer0 = 20;
int controllerNummer1 = 21;




void setup() {
  // initialize serial communication with computer:
  Serial.begin(9600);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings0[thisReading] = 0;
    readings1[thisReading] = 0;
  }
}

void loop() {
  // subtract the last reading:
  total0 = total0 - readings0[readIndex0];
  total1 = total1 - readings1[readIndex1];
  // read from the sensor:
  readings0[readIndex0] = analogRead(inputPin0);
  readings1[readIndex1] = analogRead(inputPin1);
  // add the reading to the total:
  total0 = total0 + readings0[readIndex0];
  total1 = total1 + readings1[readIndex1];
  // advance to the next position in the array:
  readIndex0 = readIndex0 + 1;
  readIndex1 = readIndex1 + 1;


  // if we're at the end of the array...
  if (readIndex0 >= numReadings) {
    // ...wrap around to the beginning:
    readIndex0 = 0;
  }

    // if we're at the end of the array...
  if (readIndex1 >= numReadings) {
    // ...wrap around to the beginning:
    readIndex1 = 0;
  }

  // calculate the average:
  average0 = total0 / numReadings;
  averageMIDI0 = map(average0,0,1023,0,127);
    if (averageMIDI0 != averageMIDIAlt0) {
      Serial.write(controlChange);
      Serial.write(controllerNummer0);
      Serial.write(averageMIDI0);

      }

      averageMIDIAlt0 = averageMIDI0;
  

      average1 = total1 / numReadings;
  averageMIDI1 = map(average1,0,1023,0,127);
    if (averageMIDI1 != averageMIDIAlt1) {
      Serial.write(controlChange);
      Serial.write(controllerNummer1);
      Serial.write(averageMIDI1);

      }

      averageMIDIAlt1 = averageMIDI1;
    delay(5); 
      }

to express with arrays.

My failed try:

/*
  Smoothing

  Reads repeatedly from an analog input, calculating a running average and
  printing it to the computer. Keeps ten readings in an array and continually
  averages them.

  The circuit:
  - analog sensor (potentiometer will do) attached to analog input 0

  created 22 Apr 2007
  by David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Smoothing
*/

// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
// const int numReadings = 3;     // Anzahl der Readings
int controlChange = 176;        // MIDI Kanal 1
int readings[3];      // the readings from the analog input
int readIndex[] = {0,0,0};              // the index of the current reading
int total[] = {0,0,0};                  // the running total
int average[] = {0,0,0};                // the average
int inputPin[] = {A0,A1,A2};
int averageMIDI[] = {0,0,0};
int averageMIDIAlt[] = {0,0,0};
int controllerNummer[] = {20,21,22};
int i = 0;




void setup() {
  // initialize serial communication with computer:
  Serial.begin(9600);
  // initialize all the readings to 0:
  }


void loop() {
  for (i = 0; i < 3; i++) {
  // subtract the last reading:
  total[i] = total[i] - readings[readIndex[i]];
  // read from the sensor:
  readings[readIndex[i]] = analogRead(inputPin[i]);
  // add the reading to the total:
  total[i] = total[i] + readings[readIndex[i]];
  // advance to the next position in the array:
  readIndex[i] = readIndex[i] + 1;

  // if we're at the end of the array...
  if (readIndex[i] >= 3) {
    // ...wrap around to the beginning:
    readIndex[i] = 0;
  }

  // calculate the average:
  average[i] = total[i] / 3;
  averageMIDI[i] = map(average[i],0,1023,0,127);
    if (averageMIDI[i] != averageMIDIAlt[i]) {
      Serial.write(controlChange);
      Serial.write(controllerNummer[i]);
      Serial.write(averageMIDI[i]);

      }

      averageMIDIAlt[i] = averageMIDI[i];
    delay(5); 
      } 
}

I don’t know where I made the mistake as I tried to change everything to arrays. I hope it makes more sense…