I checked it again and noticed one error, change CreateSendCC to this:
void CreateSendCC(int button, int state){
//function takes button number and state to create a MIDI message
byte velocity = state*127;
if (button<4){
byte track = button;
byte byte2 = 20;
Serial.write(177+track);
Serial.write(20+byte2);
} else {
byte track = 0;
byte byte2 = 20+button;
Serial.write(177+track);
Serial.write(20+byte2);
}
Serial.write(velocity);
// you could also just use Serial.write(177); Serial.write(20+button); Serial.write(velocity),
// but this way, the feedback for the red led is the same as the messages from buttons 1-4;
// This makes it easier to use for other applications, if you ever need it.
}
Can you detail the other errors you received? Of course you have to define the variables which are used in this script before, as you did with your old script. Also you have to fill in or remove this part, it was a guidance for you where you could complete the script.
else if(byte3 == ...){
//insert everything else you need here, e.g. mode button, clear
...
}
So all in all this should run without errors, but you have to modify the variables at start of the script to fit your build:
const int NUM_BTNS = 8; //modify this
const int NUM_LEDS = 8; // modify this
unsigned long buttons_time[NUM_BTNS] = {}; //stores the time a button has been pressed
unsigned long wait_time = 20; // your time to wait after each press, for debouncing.
int btnState[] = {0,0,0,0,0,0,0,0}; //modify this such that it fits the number of buttons
int oldBtnState[] ={0,0,0,0,0,0,0,0}; //modify this such that it fits the number of buttons
// you propably could wait it even longer (if you experience problems, increase it)
int BTN_PINS[] = {1,2,3,4,5,6,7,8}; //modify this
int RED_LED_PINS[] = {1,2,3,4,5,6,7,8};//modify this
int GRN_LED_PINS[] = {1,2,3,4,5,6,7,8};//modify this
void checkButtons() {
//instead of delaying after each press, we check if enough time has passed between presses
unsigned long time = millis(); //stores the current time in milliseconds
for (int i = 0; i < NUM_BTNS; i++) {
if ((time-buttons_time[i]) > wait_time){ // debouncing
btnState[i] = digitalRead(BTN_PINS[i]); // check each button
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
CreateSendCC(i, btnState[i]); // and send midi message (control change)
buttons_time[i] = time; // store time when it was pressed
}
}
}
}
void CreateSendCC(int button, int state){
//function takes button number and state to create a MIDI message
byte velocity = state*127;
if (button<4){
byte track = button;
byte byte2 = 20;
Serial.write(177+track);
Serial.write(20+byte2);
} else {
byte track = 0;
byte byte2 = 20+button;
Serial.write(177+track);
Serial.write(20+byte2);
}
Serial.write(velocity);
// you could also just use Serial.write(177); Serial.write(20+button); Serial.write(velocity),
// but this way, the feedback for the red led is the same as the messages from buttons 1-4;
// This makes it easier to use for other applications, if you ever need it.
}
void setup() {
// In your setup, you need to start serial communication
Serial.begin(31250);
}
void loop() {
// first check midi messages, then buttons
if (Serial.available() >= 3){
// if at least 3 bytes are available, read them
// messages in Mobius: MidiOut("control" track byte2 byte3)
byte byte1 = Serial.read(); //byte1 = track + 176
byte track = byte1 - 177; //we use 0-based tracks instead, so track 1 in mobius track 0 here
if (track >= 0 && track <= 3){ //track 0,1,2,3
byte byte2 = Serial.read(); // use byte2 to encode record/mute/..., in range 20-31
byte byte3 = Serial.read(); // byte3 in range 0-127, if 0, turn off led, else turn on
//20 = record
//21 = mute/play
//22 = ...
//30 = reset, all off
if (byte2 ==20){
//record: turn red led nr. <track> on or off
if (byte3 == 127){
digitalWrite(RED_LED_PINS[track], HIGH);
} else if (byte3 == 0){
//led off
digitalWrite(RED_LED_PINS[track], LOW);
}
} else if (byte2 == 21){
//playback
if (byte3 == 127){
//play
digitalWrite(GRN_LED_PINS[track], HIGH);
} else if (byte3 == 0){
//mute (off)
digitalWrite(GRN_LED_PINS[track], LOW);
}
} else if (byte2 == 30){
// you can use 30 as a (global) reset message, it should turn everything off
// here, the track does not matter, just use between 1-4
// if you need individual reset messages, delete the loop and replace i with track,
// and make a seperate global reset message e.g. byte2 = 31
// Mobius: MidiOut("control" track 30 127)
if (byte3 == 127){
for (int i=0; i< NUM_LEDS; i++){
digitalWrite(RED_LED_PINS[i], LOW);
digitalWrite(GRN_LED_PINS[i], LOW);
//turn neopixel off
}
}
}
}
}
//check buttons
checkButtons();
}
If i revert back to the original code, could I just add the CreateSendCC function to it?
Sure, this would be possible, it actually is a good idea to test the controller and to gain some understanding of it all! Only downside is that your pedal and midi-controller is not synced up, so you will likely notice some differences between mobius and your controller when playing!