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!