Combining 2 sketches of code together

Hello people, i have developed code for my MIDI controller which receives inputs from rotary potentiometers and piezo transducers…however…the code for each input type is on a different sketch for each. The potentiometer code receives inputs from 2 pots and sends MIDI control change information. The piezo transducer code receives inputs from 4 piezo sensors and sends MIDI note on/off information. Both sketches work as intended individually, however i am new to programming and have no idea how i would correctly incorporate both within the same sketch. I tried just copying and pasting one on to the other but when monitoring in MIDI-OX monitoring software the pots were only sending information once the piezo code set threshold had been met. I hope that makes sense.

Any help would be much appreciated!

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); 

}

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
  }
  

  
}

Piezo Code

#include <Bounce.h>

//code takes inputs from 4 piezos and mapps velocity

// PIEZOS

//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 200 means we've hit the piezo

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

void loop() {
  
  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);
    }
  }
  
}

How about posting you attempt at combining the two ?

Heres what i used to combine the two…

#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

//POTENTIOMETERS

// 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() {
  
  //PIEZOS 
  
  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);
    }
  }

  //POTTS
  
 // 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
  }


  
}

You appear to have pots and piezos wired to at least two analogue inputs

This section of code expects to find piezo transducers on analogue pins A0, A1, A2 and A3

  //PIEZOS 
  
  for(byte i = 0; i < sizeof(PiezoPins); i++){
    int sensorReading = analogRead(PiezoPins[i]);

This section of code expects to find pots on analogue pins A0 and A1

  //POTTS
  
 // loop trough active inputs for knobs
  for (i=0;i<analogInputs;i++){
   
    // read current value at i-th input
    inputAnalog[i] = analogRead(i);

They can’t both be right.

Which pins are the pots and transducers attached to ?

Thanks for your reply, the piezos are connected to pins A0, A1, A2, A3 while the pots are connected to pins A4 & A5

So you need to fix that in your code.

I am unsure how to do that. Would i just change the 0 in the For loop iteration?

To something like this:

// loop trough active inputs for knobs
  for (i=4;i<analogInputs;i++){

Cheers

for (i=4;i<analogInputs;i++)

Plug the values in and see how long it run’s.

I’d be inclined to put the pin number in an array, and iterate through it.

how would i put the pin number in an array please?

const byte potPins [] = {A4, A5};

Thank you very much, the following code seems to be working :slight_smile:

const byte PiezoPins[] = {A2, A3, A4, A5,};
//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

//POTENTIOMETERS

const byte potPins [] = {A0, A1};

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

void loop() {
  
  //PIEZOS 
  
  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);
    }
  }
  
 for(byte i = 0; i < sizeof(potPins); i++){
    int sensorReading = analogRead(potPins[i]);
    if(sensorReading >= sensorThreshold){
      usbMIDI.sendControlChange(49, map(sensorReading, 0, 1023, 0, 127), 1);
      delay(10);
    }
  }


}

Be very careful with the sizeof operator and arrays.
What you have is OK for byte arrays, but won't work for "int" arrays. sizeof(array) /sizeof (array[0]) is safer