midi note/cc mess

hello,

i'm making a midi controller but i have some struggles (programmer noob)

i have a combination of buttons and pots, pots seem to send cc data as expected. but i have some troubles with switches.
the switch repeatedly spits note data when pushing but i want to have this happening only once. i tried lots of things but can't seem to pull the code out of the void loop() part without errors

also when i hit the switch it initially outputs note data. but when i turn the cc pots and i push the switch afterwards it sends cc instead of note data (but with note data into the cc message?) clearly the switch also triggers cc part at the end of the code, but i don't really get it how it does it and how to keep the processes separated..

 #include <MIDI.h>

// MIDI enable pin, avoids conflict of Serial between USB for programming and DIN MIDI I/O
#define MIDI_ENABLE 12

// Variables:
int input_nb = 4; // select number of desired analog inputs (max 6)
int AnalogValue[6] = {0,0,0,0,0,0}; // define variables for the controller data
int lastAnalogValue[6] = {0,0,0,0,0,0}; // define the "lastValue" variables
int midiCCselect[6] = {22,23,24,25,26,27}; // select the midi Controller Number for each input (22 to 31 are free)
int thresh[6] = {1,1,1,1,1,1}; // select threshold for each analog input
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int ledPin = 13;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;

void setup() {
 MIDI.begin();   // MIDI lib, right baud  and so on
 pinMode(ledPin, OUTPUT);
 pinMode(buttonPin1, INPUT);
 pinMode(buttonPin2, INPUT);
 pinMode(buttonPin3, INPUT);
 pinMode(buttonPin4, INPUT);
}

void loop() {
  
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  
  if (buttonState1 == HIGH) {
    digitalWrite(ledPin, HIGH);
    MIDI.sendNoteOn(42,127,1);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
  
  
  for (int i =0; i < input_nb; i++) {
    //  My potentiometer gave a range from 0 to 1023:
    AnalogValue[i] = analogRead(i);
    //  convert to a range from 0 to 127:
    int cc = AnalogValue[i]/8;

    // check if analog input has changed
    if (cc != lastAnalogValue[i] ) {
      //send control change on cc#i
      midiCC(0xB0, midiCCselect[i], cc);
      //Serial.println(String(midiCCselect[i])+": "+String(cc));
      // update lastAnalogValue variable
      lastAnalogValue[i] = cc;
    }

  }  //  endfor
}

// sends a Midi CC. 
void midiCC(byte CC_data, byte c_num, byte c_val){
  Serial.write(CC_data);
  Serial.write(c_num);
  Serial.write(c_val);


}

void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

thanks!

Did you write that sketch on your own ?
The analog part is ok.
But the button part is not.
Do you know what you're doing with the analogRead and all? You're reading the input, storing it into a variable and then compare it with the actual read so if it changed, it sends CC. Otherwise it would send repeatedly the CC messages.
You have to do the same with the button. Check if it changed the state, if it changed and it's pressed, send note on, and if it's released send note off <<<< this is important, you should not send a note on without a note off following.

thanks! that did the trick!

this code is based on pieces floating around here on the forum regarding midi controllers, i adapted it a bit to my liking :blush: i couldn't find other examples so i started to puzzle a bit blindly. i understand some programming ideas from pure data and a bit of supercollider but i'm not familiar with C/C++. i thought Arduino might be a good start to learn (and i already learned A LOT last 5 hours...)

btw i didn't knew you could nest if.. else statements, this is really helpfull. again thanks!