Help With Arrays

Hi Guys

Ive got the code below which takes inputs from 4 piezos and sends usb data, however id like to make the code more efficient and have come across the use of an array, does anyone know how i would do this for the code below?

cheers

#include <Bounce.h>

//code takes inputs from 4 piezos and mapps velocity

// PIEZOS 

const int InputCount = 4;
int piezo[InputCount] = {
  A0, A1, A2, A3,};
int note[InputCount] = {
  50, 51, 52, 53,};


const int threshold = 200; //anything over fifty means we've hit the piezo


void setup() {
  // MIDI rate
  Serial.begin(31250);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
}

void loop() {
  
  //DRUMPADS 
  
  int sensorReading;

  sensorReading = analogRead(piezo[0]);
  if (sensorReading >= threshold) {
    int maxPiezoVal = getMaxVal(piezo[0], sensorReading);
    byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
    usbMIDI.sendNoteOn(note[0], velocity, 1);
    delay(10);
    usbMIDI.sendNoteOff(note[0], 0, 1);
    delay(100);
  }

  sensorReading = analogRead(piezo[1]);
  if (sensorReading >= threshold) {
    int maxPiezoVal = getMaxVal(piezo[1], sensorReading);
    byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
    usbMIDI.sendControlChange(note[1], velocity, 1);
    delay(100);
  }

  sensorReading = analogRead(piezo[2]);
  if (sensorReading >= threshold) {
    int maxPiezoVal = getMaxVal(piezo[2], sensorReading);
    byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
    usbMIDI.sendNoteOn(note[2], velocity, 1);
    delay(10);
    usbMIDI.sendNoteOff(note[2], 0, 1);
    delay(100);
  }

  sensorReading = analogRead(piezo[3]);
  if (sensorReading >= threshold) {
    int maxPiezoVal = getMaxVal(piezo[3], sensorReading);
    byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
    usbMIDI.sendNoteOn(note[3], velocity, 1);
    delay(10);
    usbMIDI.sendNoteOff(note[3], 0, 1);
    delay(100);
  }


}

int getMaxVal(int pin, int lastVal) {
  int currentVal = analogRead(pin);
  while (currentVal>lastVal){
    lastVal = currentVal;
    currentVal = analogRead(pin);
  }
  return lastVal;
}

It is a good idea to use names that make sense. piezo doesn't. piezePins does.

  Serial.begin(31250);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);

Pins 0 and 1 are the hardware serial pins. The HardwareSerial class has already taken care of setting the pin mode correctly.

I wonder if you could use a for loop for that?
I wonder if you could use a for loop for that?
I wonder if you could use a for loop for that?
I wonder if you could use a for loop for that?
I wonder if you could use a for loop for that?
I wonder if you could use a for loop for that?

I wonder if you could use a for loop for other repetitive stuff you are doing?

News flash, you are already using array’s!

But you can indeed make the code more compact by using a for loop to go over each element.

#include <Bounce.h>

//code takes inputs from 4 piezos and mapps velocity

// PIEZOS
//size element not needed
//never changing => const, byte is big enough
const byte PiezoPins[] = {A0, A1, A2, A3,};
//same
const byte note[sizeof(PiezoPins)] = {50, 51, 52, 53,};

//name change, threshold of what?
const int sensorThreshold = 200; //anything over fifty means we've hit the piezo


void setup() {
  // MIDI rate
  Serial.begin(31250);
  /* why do all this?
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  */
}

void loop() {
 
  //DRUMPADS
  
  for(byte i = 0; i < sizeof(PiezoPins); i++){
    int sensorReading = analogRead(PiezoPins[i]);
    if(sensorReading >= sensorThreshold){
      usbMIDI.sendNoteOn(note[i], map(sensorReading, 0, 1023, 0, 127), 1);
      delay(10);
      usbMIDI.sendNoteOff(note[i], 0, 1);
      delay(100)
    }
  }
}

I must say I didn’t use the getMaxVal(), what’s the use of that? Do you expect it to still raise?

Also, I ignored the difference in element 1 with the sendControlChange(). But it shows how to do it.

Cheers for that bare with me as im pretty new to programming so apologies if i miss some of the more obvious stuff, i was using the getmaxval() to get the piezo value and map it to get velocity. i have separate sketch which reads analogue potentiometer input and sends midi control change, however i am trying to insert it in the same code as above so the sketch reads both piezos to send note on/off and pots to send controlchange, but am having difficulty as the pot code uses an array as well. Ive tried putting the code together but every time i hit the piezo it sends note on, note off and control change blank values which is obviously not correct.

#include <Bounce.h>

// define how many pots are active up to number of available analog inputs
#define analogInputs 2
// make arrays for input values and lagged input values
int inputAnalog[analogInputs];
int iAlag[analogInputs];
// make array of cc values
int ccValue[analogInputs];
// index variable for loop
int i;

void setup() {
  // MIDI rate
  Serial.begin(31250);

}

void loop() {
  // loop trough active inputs for knobs
  for (i=0;i<analogInputs;i++){
    // read current value at i-th input
    inputAnalog[i] = analogRead(i);
    // if magnitude of difference is 8 or more...
    if (abs(inputAnalog[i] - iAlag[i]) > 7){
      // calc the CC value based on the raw value
      ccValue[i] = inputAnalog[i]/8;
      // send the MIDI
      usbMIDI.sendControlChange(i, ccValue[i], 3);
      // set raw reading to lagged array for next comparison
      iAlag[i] = inputAnalog[i];
    }
  delay(5); // limits MIDI messages to reasonable number
  }
  

  
}

I hope you don't code like you write. The compiler is case sensitive and it does care a lot where the statement/sentence ends... If you never/rarely use capitals and line ends (full stop/period) in text it's just hard to read. A compiler will just give up and trow an error...

So it's not really clear what you want. (But that might just be me because of my limited MIDI knowledge...) Can you discribe it more clearly? Post both sketches for example etc.

Piezo code

#include <Bounce.h>

//code takes inputs from 4 piezos and mapps velocity

// PIEZOS 

const int InputCount = 4;
int piezo[InputCount] = {
  A0, A1, A2, A3,};
int note[InputCount] = {
  50, 51, 52, 53,};


const int threshold = 200; //anything over fifty means we've hit the piezo


void setup() {
  // MIDI rate
  Serial.begin(31250);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
}

void loop() {
  
  //DRUMPADS 
  
  int sensorReading;

  sensorReading = analogRead(piezo[0]);
  if (sensorReading >= threshold) {
    int maxPiezoVal = getMaxVal(piezo[0], sensorReading);
    byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
    usbMIDI.sendNoteOn(note[0], velocity, 1);
    delay(10);
    usbMIDI.sendNoteOff(note[0], 0, 1);
    delay(100);
  }

  sensorReading = analogRead(piezo[1]);
  if (sensorReading >= threshold) {
    int maxPiezoVal = getMaxVal(piezo[1], sensorReading);
    byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
    usbMIDI.sendNoteOn(note[2], velocity, 1);
    delay(10);
    usbMIDI.sendNoteOff(note[2], 0, 1);
    delay(100);
  }

  sensorReading = analogRead(piezo[2]);
  if (sensorReading >= threshold) {
    int maxPiezoVal = getMaxVal(piezo[2], sensorReading);
    byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
    usbMIDI.sendNoteOn(note[2], velocity, 1);
    delay(10);
    usbMIDI.sendNoteOff(note[2], 0, 1);
    delay(100);
  }

  sensorReading = analogRead(piezo[3]);
  if (sensorReading >= threshold) {
    int maxPiezoVal = getMaxVal(piezo[3], sensorReading);
    byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
    usbMIDI.sendNoteOn(note[3], velocity, 1);
    delay(10);
    usbMIDI.sendNoteOff(note[3], 0, 1);
    delay(100);
  }


}

int getMaxVal(int pin, int lastVal) {
  int currentVal = analogRead(pin);
  while (currentVal>lastVal){
    lastVal = currentVal;
    currentVal = analogRead(pin);
  }
  return lastVal;
}

potentiometer code

#include <Bounce.h>

// define how many pots are active up to number of available analog inputs
#define analogInputs 2
// make arrays for input values and lagged input values
int inputAnalog[analogInputs];
int iAlag[analogInputs];
// make array of cc values
int ccValue[analogInputs];
// index variable for loop
int i;

void setup() {
  // MIDI rate
  Serial.begin(31250);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
}

void loop() {
  // loop trough active inputs for knobs
  for (i=0;i<analogInputs;i++){
    // read current value at i-th input
    inputAnalog[i] = analogRead(i);
    // if magnitude of difference is 8 or more...
    if (abs(inputAnalog[i] - iAlag[i]) > 7){
      // calc the CC value based on the raw value
      ccValue[i] = inputAnalog[i]/8;
      // send the MIDI
      usbMIDI.sendControlChange(i, ccValue[i], 3);
      // set raw reading to lagged array for next comparison
      iAlag[i] = inputAnalog[i];
    }
  delay(5); // limits MIDI messages to reasonable number
  }
  

  
}

So you just want to combine them?

Then the delays are going to cause problems. So as always, have a look at blink without delay. Key is to also flag and note the time when you start a note and just also look if a note is playing that's done and then send the corresponding noteOff. So start with the piezo sketch and try to rebuild it but this time without delay(). The rest is not that hard after that.