Arduino Forum

Using Arduino => Audio => Topic started by: NervusTwitch on Jul 30, 2020, 06:48 am

Title: USB Midi Foot Controller Help
Post by: NervusTwitch on Jul 30, 2020, 06:48 am
Just some quick info on my knowledge.

In the past I have built several button boxes using button matrix,encoders,and pots for gaming and various other programs. Ive only used the Nano once, several Pro Micro's and a UNO once for force feedback for my racing sim setup.
I have not ever done the programming side of Arduino, I used MMJOY as it was adequate for my needs.

Now what Im trying to do is make a midi foot controller to use with my DAW and Amp/pedal sims for guitar thru USB.

My plans are to 10 momentary foot switches with LEDS only for 7 of the switches to show activated switch or off and 1 pot for an expression pedal.I plan to also use the Pro Micro since I already have it extra. Plus another on/off/on switch for switching layers.(used the way Ill show in example)

But for now Im having trouble trying to understand how I need to wire all this up with the amount of controls I want, meaning Im not sure if I have enough inputs. Trying to keep it budget since so far I already have the parts from the parts box so as of now cost is $0.

I was looking at this example from github,easy to understand diagrams and such but like I said cant figure out how to count for the extra buttons I was wanting to add.

Please some info,links any help would be appreciative.

Example Diagram from github: Also github link for the what I want but just more switches than the author is using.

https://github.com/Hecsall/arduino-midi-footswitch

(https://raw.githubusercontent.com/Hecsall/arduino-midi-footswitch/readme-assets/img/wiring_scheme.png)
Title: Re: USB Midi Foot Controller Help
Post by: Grumpy_Mike on Jul 30, 2020, 09:57 am
Well first off do you know you can use the analogue inputs as normal digital pins. Just use A0 and so on in the digitalRead and write and in the pinMode call.

I have a MIDI foot switch project at:- http://www.thebox.myzen.co.uk/Hardware/MIDI_Footsteps.html (http://www.thebox.myzen.co.uk/Hardware/MIDI_Footsteps.html)

That uses an analogue multiplexer to read many sensors.

The thing is you have two problems:-
1) How to get more inputs
2) How to use those inputs for MIDI.

So search first for how to get more inputs.
There are several solutions to this:-
1) Using one or more shift registers for more inputs. This is like the more normal using shift registers for more outputs but uses a different type of shift register.

2) Multiplex them. You can use either digital or analogue multiplexers to feed many inputs into one input, digital or analogue. These multiplexers have address lines with control what input gets switched to the output.

3) Port expanders. Chips like the MSP23017 and MSP23S17 can give you 16 extra input / output pins. https://www.adafruit.com/product/732 (https://www.adafruit.com/product/732)
Or you can buy boards with these already on:-
Expansion Board (https://uk.rs-online.com/web/p/interface-development-kits/1360716)

Quote
easy to understand diagrams
What you posted is not a circuit diagram or schematic. It is physical layout diagram, otherwise known as a piece of Fritzing crap. They are not much use apart from blindly making a copy of something without thinking at all. Learning to read real schematics will serve you well in this game.

In the physical layout diagram you posted you will find that 10K will make the LEDs look dim and using 0.5W resistors is extreme overkill, normal quarter watt or eighth watt will be way more than you need. So it is best to assume the people who produced that, don't know what they are doing.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Jul 30, 2020, 11:24 am
Thanks for the info.
I will be looking into your info more.

As for the diagram and link,i posted it as to show that it is how i wanted my setup to work. Since im very new to the music abilities of arduino it was simple to get an idea for me to know how and what i might need to do. As i mentioned i have no experience programming arduinos. I have made some fairly extensive button and control boxes but i used mmjoy as its a graphical interface for that purpose to use for flight sims etc.

But after looking at my project a bit more, i realised that i dont need as many buttons as mentioned above.

The project is actually only 8 switches of which only 7 will have led and 1 pot. For some reason i kept counting 2 extra switches that im not using.
So if im looking at things correct just the single pro micro would be enough.

As far as what you posted about leds and resistors,yes i am aware as i have a stock pile of the 1/4 watt that i use in other projects.

As for schematics i have an ok understanding of them since i use them normally for my other projects. Im just trying to keep my current one simple,not overly complicated so i thought the linked project would be good start.

Im just hobbiest and i learn what i need to as i work on whatever project i may be attempting,so far i havent failed at any with very minimal help from others,just read alot on what i need.

Im sure ill have another question as i plan this out more but i do like to figure things out for myself, its the way my dad taught me auto mechanics. 

Also please forgive my reply grammar, im using a tablet to reply with at the moment.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 01, 2020, 07:35 am
Ok I basically built the project mentioned in the OP except instead of 5 switches/leds Im using 7,everything works.

But I have 1 issue for now that I cant seem to figure out,well maybe 2 but one thing at a time.

All the switches seem to function as intended but I cant figure out how to make the led's light when I press a switch, it will only flash once with each press. That goes for all 7 switches/led's.

All I'm wanting this foot controller to do is to be used as a foot controller for amp changes and effects on/off and leave the corresponding led to each switch lit or off depending on state. Some of the software I will use this with would be Reaper DAW,Amplitube,Bias Amp/FX and so forth.

Is what Im wanting the led's to do possible? Remember I am just now learning the coding part of Arduino.

Code: [Select]
/*
Arduino USB MIDI FootSwitch
by Hecsall (https://github.com/Hecsall)
*/

// https://www.arduino.cc/en/Reference/MIDIUSB
#include "MIDIUSB.h"
#include "variables.h"

// Utility functions

// Event type is hard-coded (0x09 = note on, 0x08 = note off).
// First parameter is the MIDI channel, combined with the note-on/note-off.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Second parameter (pitch) is the note number (48 = middle C).
// Third parameter is the velocity (0 -> 127, 0 = no_sound, 64 = normal, 127 = fastest).

void noteOn(byte channel, byte pitch, byte velocity)
{
    midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
    MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity)
{
    midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
    MidiUSB.sendMIDI(noteOff);
}

// Event type hard-coded (0x0B = control change, aka "MIDI CC").
// First parameter is the channel (0-15), combined with the event type.
// Second parameter is the "control change" number (0-119, see top link to midi.org).
// Third parameter is the control value (0-127).

void controlChange(byte channel, byte control, byte value)
{
    midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
    MidiUSB.sendMIDI(event);
}

// BPM Counter
void initBPM()
{
    midiEventPacket_t rx;
    do {
        rx = MidiUSB.read();
        //Count pulses and send note
        if (rx.byte1 == 0xF8)
        {
            ++ppqn;
            if (ppqn == 24)
            {
                digitalWrite(10, HIGH);
                delay(80);
                digitalWrite(10, LOW);
                ppqn = 0;
            };
        }
        //Clock start byte
    } while (rx.header != 0);
}

// Button in "Push" mode
void handlePushButton(byte i)
{
    if (digitalRead(button_pins[i]) == LOW && button_states[i] == false)
    {
        controlChange(0, button_layers[current_layer][i], 127);
        MidiUSB.flush();
        button_states[i] = true;
        digitalWrite(led_pins[i], HIGH); // Turn the LED on
        led_states[i] = true;
        delay(15);
    }
    else if (digitalRead(button_pins[i]) == HIGH && button_states[i] == true)
    {
        controlChange(0, button_layers[current_layer][i], 0);
        MidiUSB.flush();
        button_states[i] = false;
        digitalWrite(led_pins[i], LOW); // Turn the LED off
        led_states[i] = false;
        delay(15);
    }
}

// Button in "Toggle" mode
void handleToggleButton(byte i)
{
    buttonState = digitalRead(button_pins[i]);
    if (buttonState != lastButtonState){
        if (buttonState == LOW && button_states[i] == false)
        {
            controlChange(0, button_layers[current_layer][i], 127);
            MidiUSB.flush();
            button_states[i] = true;
            digitalWrite(led_pins[i], HIGH); // Turn the LED on
            led_states[i] = true;
        }
        else if (buttonState == LOW && button_states[i] == true)
        {
            controlChange(0, button_layers[current_layer][i], 0);
            MidiUSB.flush();
            button_states[i] = false;
            digitalWrite(led_pins[i], LOW); // Turn the LED off
            led_states[i] = false;
        }
        delay(100);
    }
    lastButtonState = buttonState;
}

// Button "Change" mode
void handleChangeMode(byte i)
{
    buttonState = digitalRead(button_pins[i]);
    if (buttonState != lastButtonState){
        // Note: only layer 1 (switch OFF) can be customized
        if (buttonState == LOW && button_modes[1][i] == 0 && millis() - time > debounce)
        {
            button_modes[1][i] = 1;
            digitalWrite(led_pins[i], HIGH); // Turn the LED on
            led_states[i] = true;
            time = millis();
        }
        else if (buttonState == LOW && button_modes[1][i] == 1 && millis() - time > debounce)
        {
            button_modes[1][i] = 0;
            digitalWrite(led_pins[i], LOW); // Turn the LED off
            led_states[i] = false;
            time = millis();
        }
        delay(50);
    }
    lastButtonState = buttonState;
}

// Turn off all LEDs
void poweroffLeds()
{
    for (uint8_t i = 0; i < 7; i++)
    {
        digitalWrite(led_pins[i], LOW);
        led_states[i] = false;
    }
}

// Turn on all the current mode LEDs
void showModeLeds()
{
    for (uint8_t i = 0; i < 7; i++)
    {
        if (button_modes[1][i] == 1)
        {
            digitalWrite(led_pins[i], HIGH);
            led_states[i] = true;
        }
    }
}

// Check and set which layer is active
void setLayer()
{
    if (digitalRead(switch_pins[0]) == LOW && digitalRead(switch_pins[1]) == HIGH && current_layer != 0)
    {
        current_layer = 0; // Switch UP
    }
    else if (digitalRead(switch_pins[0]) == HIGH && digitalRead(switch_pins[1]) == HIGH && current_layer != 1)
    {
        current_layer = 1; // Switch OFF
    }
    else if (digitalRead(switch_pins[0]) == HIGH && digitalRead(switch_pins[1]) == LOW && current_layer != 2)
    {
        current_layer = 2; // Switch DOWN
    }
}

// Actual logic
void setup()
{
    Serial.begin(115200);
    // Button Pins
    for (uint8_t i = 0; i < 7; i++)
    {
        pinMode(button_pins[i], INPUT_PULLUP);
    }
    // LED Pins
    for (uint8_t i = 0; i < 7; i++)
    {
        pinMode(led_pins[i], OUTPUT);
    }
    // Switch Pins
    for (uint8_t i = 0; i < 2; i++)
    {
        pinMode(switch_pins[i], INPUT_PULLUP);
    }
    // Set currently active Layer
    setLayer();
}

void loop()
{
    initBPM(); // Blinking BPM LED - needs to be enabled in your DAW
    setLayer(); // Set which Layer we are using

    // Button operations based on current_layer
    for (uint8_t i = 0; i < 7; i++)
    {
        if (current_layer < 2) // Only layers 0 and 1 are normal operational layers
        {
            poweroffLeds();
            if (button_modes[current_layer][i] == 0)
            {
                handlePushButton(i);
            }
            else if (button_modes[current_layer][i] == 1)
            {
                handleToggleButton(i);
            }
        }
        else if (current_layer == 2) // Layer 2 is the "settings" layer
        {
            showModeLeds();
            handleChangeMode(i);
        }
    }
}



Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 01, 2020, 07:41 am
Also my variables file

Variables

Code: [Select]
/*
Arduino USB MIDI FootSwitch
by Hecsall (https://github.com/Hecsall)
*/

// https://www.arduino.cc/en/Reference/MIDIUSB
#include "MIDIUSB.h"
#include "variables.h"

// Utility functions

// Event type is hard-coded (0x09 = note on, 0x08 = note off).
// First parameter is the MIDI channel, combined with the note-on/note-off.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Second parameter (pitch) is the note number (48 = middle C).
// Third parameter is the velocity (0 -> 127, 0 = no_sound, 64 = normal, 127 = fastest).

void noteOn(byte channel, byte pitch, byte velocity)
{
    midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
    MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity)
{
    midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
    MidiUSB.sendMIDI(noteOff);
}

// Event type hard-coded (0x0B = control change, aka "MIDI CC").
// First parameter is the channel (0-15), combined with the event type.
// Second parameter is the "control change" number (0-119, see top link to midi.org).
// Third parameter is the control value (0-127).

void controlChange(byte channel, byte control, byte value)
{
    midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
    MidiUSB.sendMIDI(event);
}

// BPM Counter
void initBPM()
{
    midiEventPacket_t rx;
    do {
        rx = MidiUSB.read();
        //Count pulses and send note
        if (rx.byte1 == 0xF8)
        {
            ++ppqn;
            if (ppqn == 24)
            {
                digitalWrite(10, HIGH);
                delay(80);
                digitalWrite(10, LOW);
                ppqn = 0;
            };
        }
        //Clock start byte
    } while (rx.header != 0);
}

// Button in "Push" mode
void handlePushButton(byte i)
{
    if (digitalRead(button_pins[i]) == LOW && button_states[i] == false)
    {
        controlChange(0, button_layers[current_layer][i], 127);
        MidiUSB.flush();
        button_states[i] = true;
        digitalWrite(led_pins[i], HIGH); // Turn the LED on
        led_states[i] = true;
        delay(15);
    }
    else if (digitalRead(button_pins[i]) == HIGH && button_states[i] == true)
    {
        controlChange(0, button_layers[current_layer][i], 0);
        MidiUSB.flush();
        button_states[i] = false;
        digitalWrite(led_pins[i], LOW); // Turn the LED off
        led_states[i] = false;
        delay(15);
    }
}

// Button in "Toggle" mode
void handleToggleButton(byte i)
{
    buttonState = digitalRead(button_pins[i]);
    if (buttonState != lastButtonState){
        if (buttonState == LOW && button_states[i] == false)
        {
            controlChange(0, button_layers[current_layer][i], 127);
            MidiUSB.flush();
            button_states[i] = true;
            digitalWrite(led_pins[i], HIGH); // Turn the LED on
            led_states[i] = true;
        }
        else if (buttonState == LOW && button_states[i] == true)
        {
            controlChange(0, button_layers[current_layer][i], 0);
            MidiUSB.flush();
            button_states[i] = false;
            digitalWrite(led_pins[i], LOW); // Turn the LED off
            led_states[i] = false;
        }
        delay(100);
    }
    lastButtonState = buttonState;
}

// Button "Change" mode
void handleChangeMode(byte i)
{
    buttonState = digitalRead(button_pins[i]);
    if (buttonState != lastButtonState){
        // Note: only layer 1 (switch OFF) can be customized
        if (buttonState == LOW && button_modes[1][i] == 0 && millis() - time > debounce)
        {
            button_modes[1][i] = 1;
            digitalWrite(led_pins[i], HIGH); // Turn the LED on
            led_states[i] = true;
            time = millis();
        }
        else if (buttonState == LOW && button_modes[1][i] == 1 && millis() - time > debounce)
        {
            button_modes[1][i] = 0;
            digitalWrite(led_pins[i], LOW); // Turn the LED off
            led_states[i] = false;
            time = millis();
        }
        delay(50);
    }
    lastButtonState = buttonState;
}

// Turn off all LEDs
void poweroffLeds()
{
    for (uint8_t i = 0; i < 7; i++)
    {
        digitalWrite(led_pins[i], LOW);
        led_states[i] = false;
    }
}

// Turn on all the current mode LEDs
void showModeLeds()
{
    for (uint8_t i = 0; i < 7; i++)
    {
        if (button_modes[1][i] == 1)
        {
            digitalWrite(led_pins[i], HIGH);
            led_states[i] = true;
        }
    }
}

// Check and set which layer is active
void setLayer()
{
    if (digitalRead(switch_pins[0]) == LOW && digitalRead(switch_pins[1]) == HIGH && current_layer != 0)
    {
        current_layer = 0; // Switch UP
    }
    else if (digitalRead(switch_pins[0]) == HIGH && digitalRead(switch_pins[1]) == HIGH && current_layer != 1)
    {
        current_layer = 1; // Switch OFF
    }
    else if (digitalRead(switch_pins[0]) == HIGH && digitalRead(switch_pins[1]) == LOW && current_layer != 2)
    {
        current_layer = 2; // Switch DOWN
    }
}

// Actual logic
void setup()
{
    Serial.begin(115200);
    // Button Pins
    for (uint8_t i = 0; i < 7; i++)
    {
        pinMode(button_pins[i], INPUT_PULLUP);
    }
    // LED Pins
    for (uint8_t i = 0; i < 7; i++)
    {
        pinMode(led_pins[i], OUTPUT);
    }
    // Switch Pins
    for (uint8_t i = 0; i < 2; i++)
    {
        pinMode(switch_pins[i], INPUT_PULLUP);
    }
    // Set currently active Layer
    setLayer();
}

void loop()
{
    initBPM(); // Blinking BPM LED - needs to be enabled in your DAW
    setLayer(); // Set which Layer we are using

    // Button operations based on current_layer
    for (uint8_t i = 0; i < 7; i++)
    {
        if (current_layer < 2) // Only layers 0 and 1 are normal operational layers
        {
            poweroffLeds();
            if (button_modes[current_layer][i] == 0)
            {
                handlePushButton(i);
            }
            else if (button_modes[current_layer][i] == 1)
            {
                handleToggleButton(i);
            }
        }
        else if (current_layer == 2) // Layer 2 is the "settings" layer
        {
            showModeLeds();
            handleChangeMode(i);
        }
    }
}


Title: Re: USB Midi Foot Controller Help
Post by: Grumpy_Mike on Aug 01, 2020, 11:10 am
Quote
Is what Im wanting the led's to do possible?
Yes.
Try replacing
Code: [Select]
digitalWrite(led_pins[i], HIGH); // Turn the LED on

With
Code: [Select]
digitalWrite(led_pins[i], ! button_states[i]); // Turn the LED on
And do exactly the same in the else if clause for turning the LED off.

I can't fine the definition of the button_states array, it needs to be boolean to work.
When he told you on the GitHub page he was not a programmer he wasn't joaking. It is best to try and learn from good code. The code seems to have the turning on and off of the LEDs in two places, the code and the so called variables file, this is utter madness, I have no idea why he did this. You need to make those changes in both places to be sure the right one is being used.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 01, 2020, 11:33 am
Yes.
Try replacing
Code: [Select]
digitalWrite(led_pins[i], HIGH); // Turn the LED on

With
Code: [Select]
digitalWrite(led_pins[i], ! button_states[i]); // Turn the LED on
And do exactly the same in the else if clause for turning the LED off.

I can't fine the definition of the button_states array, it needs to be boolean to work.
When he told you on the GitHub page he was not a programmer he wasn't joaking. It is best to try and learn from good code.
Thanks for the help.

Just so im clear, anywhere its says what i have,replace with what you have said in all sections of code?

Once i get the leds working correct , then i have to figure out how to add and expression pedal and ill be set.
I have the pots wired to pro micro,just cant really figure out coding to add and where im suppose to put it in my current sketch.


Honestly i wouldnt know good from bad code. I was just wanting a basic foot controller to work with my music software,im no fan of coding.

Now ive built several projects but the are basically joysticks. Ive built from scratch a couple joysticks,throttle, a few button boxes, an led display that works simhub, and rumble motors to my racing pedals.
But i did all this using mmjoy2 of which is all graphical interface and really simplifies things,no coding.

Id rather just found a sketch that fit my needs but the current one is all i found.

All i want is usb midi foot controller with an expression pedal,7 buttons w/led on each,a shift switch to make the switches read another line of midi commands.

After my issues tonight with uploaded sketch connection issues im almost ready to give up and i never give up, drives my wife crazy...lol
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 01, 2020, 12:06 pm
I tried your suggestion for the leds, they didnt work at all afterwards. I might look into them more tomorrow.
But for now I want to figure out how to the expression pedal coded in of which I havent any idea yet.

But Im off to bed for now

Thanks for the help and anymore help.
Title: Re: USB Midi Foot Controller Help
Post by: cameron206 on Aug 01, 2020, 08:23 pm
If I were doing this, I would use the Control_Surface library. It would be pretty straightforward.

Only one part might be tricky:

Quote
All I'm wanting this foot controller to do is to be used as a foot controller for amp changes and effects on/off and leave the corresponding led to each switch lit or off depending on state. Some of the software I will use this with would be Reaper DAW,Amplitube,Bias Amp/FX and so forth.
This. Having the LED toggle when you click a switch is easy.
But do you want it to toggle if you change the state via Mouse, though? Of course you do. If you want it to follow the ~actual state~, not just if you toggle the switch.. then you got some twiddling to do, I think.

Title: Re: USB Midi Foot Controller Help
Post by: Grumpy_Mike on Aug 01, 2020, 10:38 pm
Quote
I tried your suggestion for the leds, they didnt work at all afterwards.
So what you should do on this forum is to post your latest code and say exactly what it did do. Then we can see if you got it right.

Quote
But for now I want to figure out how to the expression pedal coded in of which I havent any idea yet.
Almost trivial, read the pot and if it has changed since last time you read it then send out the reading divided by 8 to the appropriate CC channel number.


Quote
If I were doing this, I would use the Control_Surface library.
Maybe but that code is a lot more "software intense" requiring newcomers to know more software techniques than they normally do when asking these sorts of questions. In the end you give them advice about what to change but they don't actually learn anything and they are stuck in a dependent state. The whole point in answering questions here in my opinion is to teach something that can be built upon.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 02, 2020, 12:38 am
Only one part might be tricky:

This. Having the LED toggle when you click a switch is easy.
But do you want it to toggle if you change the state via Mouse, though? Of course you do. If you want it to follow the ~actual state~, not just if you toggle the switch.. then you got some twiddling to do, I think.


OK  so that never really crossed my mind, something to think about but for me one thing at a time. I'm just starting to get some kind of grasp on this.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 02, 2020, 12:46 am
So what you should do on this forum is to post your latest code and say exactly what it did do. Then we can see if you got it right.
Almost trivial, read the pot and if it has changed since last time you read it then send out the reading divided by 8 to the appropriate CC channel number.

Maybe but that code is a lot more "software intense" requiring newcomers to know more software techniques than they normally do when asking these sorts of questions. In the end you give them advice about what to change but they don't actually learn anything and they are stuck in a dependent state. The whole point in answering questions here in my opinion is to teach something that can be built upon.
Yes I realise now I should have posted the code here after my changes,I was tired and not really thinking to clearly which didnt help either.

I should have researched the software side more since I know nothing at all.Im more a hardware type and dont have much problems building things.

I may work on this some today but health issues keeping me down today.

Thank you everyone for all the help. Still not done but its close.
Title: Re: USB Midi Foot Controller Help
Post by: cameron206 on Aug 02, 2020, 06:34 am
The whole point in answering questions here in my opinion is to teach something that can be built upon.
I totally agree, in principal.
But I also feel the pain (being a non-coder myself), of facing a huge learning curve for something I might only do once or twice.

It was not until I'd made maybe 6 controllers that I felt like learning actual language :)

But you are NOT wrong.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 02, 2020, 07:12 am
I totally agree, in principal.
But I also feel the pain (being a non-coder myself), of facing a huge learning curve for something I might only do once or twice.

It was not until I'd made maybe 6 controllers that I felt like learning actual language :)

But you are NOT wrong.
Ok so you feel about the same as I do. Ive built alot of arduino game based controllers but didnt need to code anything,I am quiet comfortable with it.
But for this midi stuff it looks like coding is required and is the main reason I have put this off for so long since most likely this will just be a once or twice thing for me.

Now I looked into the Control Surface and a couple examples, to me it actually looks easier to use and get a handle on. I do much better when things are more straight forward.

As for this I have been looking for example code that will show me what I want so that I could implement it into my project. But most of the examples I have looked at are more a mixer type board.
Its my learning process, I look at examples that shows what I need then try to add/modify/learn for my needs.

I'm not asking anyone to sit and do the code for me, but I do have a question concerning Control Surface library:


what I want my pedal project to do is:
7 switches each with own led. leds to light on/off depending if the switch is on/off.
Have 6 of those switches bankable.
Have 1 expression pedal.
Be USB

Now Ive seen the array setups and such in the code and thats why this looks much easier to code and edit.

I read that I would need the midiusb control surface library, correct?

could I either get a basic generic code example or a link to one that will show those basic things I mention so can try learning this? Remember I am a total noob so I dont know all the places to look or really what I'm looking at,I barely understand this coding stuff.

Sorry for long post.

Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 02, 2020, 09:03 pm
Yes.
Try replacing
Code: [Select]
digitalWrite(led_pins[i], HIGH); // Turn the LED on

With
Code: [Select]
digitalWrite(led_pins[i], ! button_states[i]); // Turn the LED on
And do exactly the same in the else if clause for turning the LED off.

I can't fine the definition of the button_states array, it needs to be boolean to work.
When he told you on the GitHub page he was not a programmer he wasn't joaking. It is best to try and learn from good code. The code seems to have the turning on and off of the LEDs in two places, the code and the so called variables file, this is utter madness, I have no idea why he did this. You need to make those changes in both places to be sure the right one is being used.
Ok so I tried what you said again. I seem to be missing something here.
All lights still work,but they just give a single flash with each button push.

Also only 1 switch only lights every other switch push but in my amp sim software when testing it it reads every switch push.


Code: [Select]
/*
Arduino USB MIDI FootSwitch
by Hecsall (https://github.com/Hecsall)
*/

// https://www.arduino.cc/en/Reference/MIDIUSB
#include "MIDIUSB.h"
#include "variables.h"

// Utility functions

// Event type is hard-coded (0x09 = note on, 0x08 = note off).
// First parameter is the MIDI channel, combined with the note-on/note-off.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Second parameter (pitch) is the note number (48 = middle C).
// Third parameter is the velocity (0 -> 127, 0 = no_sound, 64 = normal, 127 = fastest).

void noteOn(byte channel, byte pitch, byte velocity)
{
    midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
    MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity)
{
    midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
    MidiUSB.sendMIDI(noteOff);
}

// Event type hard-coded (0x0B = control change, aka "MIDI CC").
// First parameter is the channel (0-15), combined with the event type.
// Second parameter is the "control change" number (0-119, see top link to midi.org).
// Third parameter is the control value (0-127).

void controlChange(byte channel, byte control, byte value)
{
    midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
    MidiUSB.sendMIDI(event);
}



// Button in "Push" mode
void handlePushButton(byte i)
{
    if (digitalRead(button_pins[i]) == LOW && button_states[i] == false)
    {
        controlChange(0, button_layers[current_layer][i], 127);
        MidiUSB.flush();
        button_states[i] = true;
        digitalWrite(led_pins[i], ! button_states[i]); // Turn the LED on
        led_states[i] = true;
        delay(15);
    }
    else if (digitalRead(button_pins[i]) == HIGH && button_states[i] == true)
    {
        controlChange(0, button_layers[current_layer][i], 0);
        MidiUSB.flush();
        button_states[i] = false;
        digitalWrite(led_pins[i], ! button_states[i]); // Turn the LED off
        led_states[i] = false;
        delay(15);
    }
}

// Button in "Toggle" mode
void handleToggleButton(byte i)
{
    buttonState = digitalRead(button_pins[i]);
    if (buttonState != lastButtonState){
        if (buttonState == LOW && button_states[i] == false)
        {
            controlChange(0, button_layers[current_layer][i], 127);
            MidiUSB.flush();
            button_states[i] = true;
            digitalWrite(led_pins[i], ! button_states[i]); // Turn the LED on
            led_states[i] = true;
        }
        else if (buttonState == LOW && button_states[i] == true)
        {
            controlChange(0, button_layers[current_layer][i], 0);
            MidiUSB.flush();
            button_states[i] = false;
            digitalWrite(led_pins[i], ! button_states[i]); // Turn the LED off
            led_states[i] = false;
        }
        delay(100);
    }
    lastButtonState = buttonState;
}

// Button "Change" mode
void handleChangeMode(byte i)
{
    buttonState = digitalRead(button_pins[i]);
    if (buttonState != lastButtonState){
        // Note: only layer 1 (switch OFF) can be customized
        if (buttonState == LOW && button_modes[1][i] == 0 && millis() - time > debounce)
        {
            button_modes[1][i] = 1;
            digitalWrite(led_pins[i], ! button_states[i]); // Turn the LED on
            led_states[i] = true;
            time = millis();
        }
        else if (buttonState == LOW && button_modes[1][i] == 1 && millis() - time > debounce)
        {
            button_modes[1][i] = 0;
            digitalWrite(led_pins[i], ! button_states[i]); // Turn the LED off
            led_states[i] = false;
            time = millis();
        }
        delay(50);
    }
    lastButtonState = buttonState;
}

// Turn off all LEDs
void poweroffLeds()
{
    for (uint8_t i = 0; i < 7; i++)
    {
        digitalWrite(led_pins[i], LOW);
        led_states[i] = false;
    }
}

// Turn on all the current mode LEDs
void showModeLeds()
{
    for (uint8_t i = 0; i < 7; i++)
    {
        if (button_modes[1][i] == 1)
        {
            digitalWrite(led_pins[i], HIGH);
            led_states[i] = true;
        }
    }
}

// Check and set which layer is active
void setLayer()
{
    if (digitalRead(switch_pins[0]) == LOW && digitalRead(switch_pins[1]) == HIGH && current_layer != 0)
    {
        current_layer = 0; // Switch UP
    }
    else if (digitalRead(switch_pins[0]) == HIGH && digitalRead(switch_pins[1]) == HIGH && current_layer != 1)
    {
        current_layer = 1; // Switch OFF
    }
    else if (digitalRead(switch_pins[0]) == HIGH && digitalRead(switch_pins[1]) == LOW && current_layer != 2)
    {
        current_layer = 2; // Switch DOWN
    }
}

// Actual logic
void setup()
{
    Serial.begin(115200);
    // Button Pins
    for (uint8_t i = 0; i < 7; i++)
    {
        pinMode(button_pins[i], INPUT_PULLUP);
    }
    // LED Pins
    for (uint8_t i = 0; i < 7; i++)
    {
        pinMode(led_pins[i], OUTPUT);
    }
    // Switch Pins
    for (uint8_t i = 0; i < 2; i++)
    {
        pinMode(switch_pins[i], INPUT_PULLUP);
    }
    // Set currently active Layer
    setLayer();
}

void loop()
{
   
    // Button operations based on current_layer
    for (uint8_t i = 0; i < 7; i++)
    {
        if (current_layer < 2) // Only layers 0 and 1 are normal operational layers
        {
            poweroffLeds();
            if (button_modes[current_layer][i] == 0)
            {
                handlePushButton(i);
            }
            else if (button_modes[current_layer][i] == 1)
            {
                handleToggleButton(i);
            }
        }
        else if (current_layer == 2) // Layer 2 is the "settings" layer
        {
            showModeLeds();
            handleChangeMode(i);
        }
    }
}
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 02, 2020, 09:25 pm
Also I cant find out how to add the expression pedal in the code, no idea how to start.
I really just want it to function as a wah control but Im lost here.
I have looked at examples for other projects but not even sure if it will work or were I would put the code.

Title: Re: USB Midi Foot Controller Help
Post by: Grumpy_Mike on Aug 03, 2020, 02:44 pm
Quote
I seem to be missing something here.
Did you make the same changes in the variables.h file as well as this code?
You need to post both files due to the absoloutly moronic way this code is written.

EDIT - OK now I am a bit cross. In reply #4 you said:-
Quote
Also my variables file
But this was not your variables.h file was it? It was your main file again. I though you were telling me the truth so I assumed that the author had done something absoloutly mad. It now appears that he was not being crazy here.

What you seem to have done is to change the number of LEDs and push buttons you have without updating the size of the arrays in the variables.h file. This means that you are addressing arrays outside the declared bounds and reading and writing at memory locations that have been allocated to something else. This will cause all sorts of strange behavior that is not symptomatic of what is going wrong.

So change the variables.h file to make these arrays bigger.
Note for single variables you don't need to do things like this:-
Code: [Select]
int button_states[5] = {false, false, false, false, false};
you can use:-
Code: [Select]
int button_states[] = {false, false, false, false, false, false, false};
and the array will be made big enough to hold all the variables.
In the case of two dimensional arrays you only have to specify the first dimension.

So make these changes and post back both files and say what happens.

Quote
Also I cant find out how to add the expression pedal in the code
So I told you how to do this in reply#9. If you do not understand something about that explanation then please ask specifically about what you don't understand.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 03, 2020, 07:47 pm
Ok I made those changes long ago in the variables.h file.
Later when Im home I'll post the files.

So in your reply about the pedal, I will look at it again.

Thanks
Title: Re: USB Midi Foot Controller Help
Post by: cameron206 on Aug 03, 2020, 08:37 pm
OK  so that never really crossed my mind, something to think about but for me one thing at a time. I'm just starting to get some kind of grasp on this.

If I were you, I would think about this before you go much further... because it fundamentally changes how you handle the LEDs.

And truly.. outside of the LEDs, your code is trivial. Look at the 'Trasposer' example in the control surface library if you are sending notes.

IN re: the LEDS.. if you want it to reflect the STATE of a condition in your DAW.. you gotta get into callbacks. Maybe emulate a known controller. And this will be very DAW specific.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 03, 2020, 08:42 pm
variables.h file.
I can post the main file but since my post count is low the forum makes me wait a few minutes to post again.
As for what I dont understand about adding the expression pedal, I dont understand any of that. I dont know how to even start to add it in,where or anything. I have looked at a few others projects and tried to figure out what to do but none of it worked,thru out a ton of errors.

Just so I dont seem to dense here...lol I need to clarify. In another post I talked about health issues,well that severe nerve damage that affects everything. That includes brain function and memory. Makes it hard for me at times to absorb and learn new things.

I just dont want to come off as being difficult or not willing.

Code: [Select]
/ Arduino Pins
const byte button_pins[7] = {1, 2, 3, 4, 5, 6, 7};   // Pins where buttons are connected
const byte switch_pins[2] = {8, 9};            // Pins where the switch is connected
const byte led_pins[7] = {10, 16, 14, 15, 18, 19, 20}; // Pins where LEDs are connected

// Layer: this is the currently select layer (changed by the switch)g
byte current_layer; // 0 = switch up, 1 = switch off, 2 = switch down

// CC values are the numbers in the decimal column of this table
// https://www.midi.org/specifications-old/item/table-3-control-change-messages-data-bytes-2

const int button_layers[2][7] = {
    /*
    Layer 0 (Switch Up)
    ,----------------------,
    |  49 |  50 | 51  |    |
    |----------------------|
    |  80 |  81 |  82 | 85 |
    `----------------------`
    */
    {
        49, 50, 51,
        80, 81, 82, 85},

    /*
    Layer 1 (Switch Off)
    ,---------------------,
    |  16 |  17 | 18 |    |
    |---------------------|
    |  19 |  20 | 21 | 48 |
    `---------------------`
    */
    {
        16, 17, 18,
        19, 20, 21, 48}
};

// Button modes: 0 = push button, 1 = toggle button
byte button_modes[2][7] = {
    // Layer 0 (Switch Up)
    {0, 0, 0, 0, 0, 0, 0},

    // Layer 1 (Switch Off)
    {0, 0, 0, 0, 0, 0, 0}
};

int buttonState = 0;
int lastButtonState = 0;

// Button states: true = button ON, false = button OFF
int button_states[7] = {false, false, false, false, false, false, false};

long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

// LED states
bool led_states[7] = {false, false, false, false, false, false, false};

byte ppqn = 0; // "Pulse Per Quarter Note"
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 03, 2020, 08:50 pm
If I were you, I would think about this before you go much further... because it fundamentally changes how you handle the LEDs.

And truly.. outside of the LEDs, your code is trivial. Look at the 'Trasposer' example in the control surface library if you are sending notes.

IN re: the LEDS.. if you want it to reflect the STATE of a condition in your DAW.. you gotta get into callbacks. Maybe emulate a known controller. And this will be very DAW specific.
I have been going thru all the control surface examples and trying to get some grasp on them. It just looks much easier than the one Im working with.
I think one of my issues is that other than connecting and audio interface to connect my mics to and being able to record into the DAW,I have never messed with anything midi and really just learning it as I go here. Ive always been a bit old school when it came to my gear. My only midi experience has been creating bass and drum tracks to add virtual instruments to to have backing tracks to play my guitar with.

Honestly this pedal is just to control pc amp sims and effects since the wife has started working at home I cant really use my main gear while she works.

At this point with the pedal controls Id just like the 7 switches and expression pedal to work and the leds. I dont need any banks or anything really.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 03, 2020, 09:17 pm
Did you make the same changes in the variables.h file as well as this code?
You need to post both files due to the absoloutly moronic way this code is written.

EDIT - OK now I am a bit cross. In reply #4 you said:-But this was not your variables.h file was it? It was your main file again. I though you were telling me the truth so I assumed that the author had done something absoloutly mad. It now appears that he was not being crazy here.

What you seem to have done is to change the number of LEDs and push buttons you have without updating the size of the arrays in the variables.h file. This means that you are addressing arrays outside the declared bounds and reading and writing at memory locations that have been allocated to something else. This will cause all sorts of strange behavior that is not symptomatic of what is going wrong.

So change the variables.h file to make these arrays bigger.
Note for single variables you don't need to do things like this:-
Code: [Select]
int button_states[5] = {false, false, false, false, false};
you can use:-
Code: [Select]
int button_states[] = {false, false, false, false, false, false, false};
and the array will be made big enough to hold all the variables.
In the case of two dimensional arrays you only have to specify the first dimension.

So make these changes and post back both files and say what happens.
So I told you how to do this in reply#9. If you do not understand something about that explanation then please ask specifically about what you don't understand.

main code
Code: [Select]
/*
Arduino USB MIDI FootSwitch
by Hecsall (https://github.com/Hecsall)
*/

// https://www.arduino.cc/en/Reference/MIDIUSB
#include "MIDIUSB.h"
#include "variables.h"

// Utility functions

// Event type is hard-coded (0x09 = note on, 0x08 = note off).
// First parameter is the MIDI channel, combined with the note-on/note-off.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Second parameter (pitch) is the note number (48 = middle C).
// Third parameter is the velocity (0 -> 127, 0 = no_sound, 64 = normal, 127 = fastest).

void noteOn(byte channel, byte pitch, byte velocity)
{
    midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
    MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity)
{
    midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
    MidiUSB.sendMIDI(noteOff);
}

// Event type hard-coded (0x0B = control change, aka "MIDI CC").
// First parameter is the channel (0-15), combined with the event type.
// Second parameter is the "control change" number (0-119, see top link to midi.org).
// Third parameter is the control value (0-127).

void controlChange(byte channel, byte control, byte value)
{
    midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
    MidiUSB.sendMIDI(event);
}



// Button in "Push" mode
void handlePushButton(byte i)
{
    if (digitalRead(button_pins[i]) == LOW && button_states[i] == false)
    {
        controlChange(0, button_layers[current_layer][i], 127);
        MidiUSB.flush();
        button_states[i] = true;
        digitalWrite(led_pins[i], HIGH); // Turn the LED on
        led_states[i] = true;
        delay(15);
    }
    else if (digitalRead(button_pins[i]) == HIGH && button_states[i] == true)
    {
        controlChange(0, button_layers[current_layer][i], 0);
        MidiUSB.flush();
        button_states[i] = false;
        digitalWrite(led_pins[i], LOW); // Turn the LED off
        led_states[i] = false;
        delay(15);
    }
}

// Button in "Toggle" mode
void handleToggleButton(byte i)
{
    buttonState = digitalRead(button_pins[i]);
    if (buttonState != lastButtonState){
        if (buttonState == LOW && button_states[i] == false)
        {
            controlChange(0, button_layers[current_layer][i], 127);
            MidiUSB.flush();
            button_states[i] = true;
            digitalWrite(led_pins[i], HIGH); // Turn the LED on
            led_states[i] = true;
        }
        else if (buttonState == LOW && button_states[i] == true)
        {
            controlChange(0, button_layers[current_layer][i], 0);
            MidiUSB.flush();
            button_states[i] = false;
            digitalWrite(led_pins[i], LOW); // Turn the LED off
            led_states[i] = false;
        }
        delay(100);
    }
    lastButtonState = buttonState;
}

// Button "Change" mode
void handleChangeMode(byte i)
{
    buttonState = digitalRead(button_pins[i]);
    if (buttonState != lastButtonState){
        // Note: only layer 1 (switch OFF) can be customized
        if (buttonState == LOW && button_modes[1][i] == 0 && millis() - time > debounce)
        {
            button_modes[1][i] = 1;
            digitalWrite(led_pins[i], HIGH); // Turn the LED on
            led_states[i] = true;
            time = millis();
        }
        else if (buttonState == LOW && button_modes[1][i] == 1 && millis() - time > debounce)
        {
            button_modes[1][i] = 0;
            digitalWrite(led_pins[i], LOW); // Turn the LED off
            led_states[i] = false;
            time = millis();
        }
        delay(50);
    }
    lastButtonState = buttonState;
}

// Turn off all LEDs
void poweroffLeds()
{
    for (uint8_t i = 0; i < 7; i++)
    {
        digitalWrite(led_pins[i], LOW);
        led_states[i] = false;
    }
}

// Turn on all the current mode LEDs
void showModeLeds()
{
    for (uint8_t i = 0; i < 7; i++)
    {
        if (button_modes[1][i] == 1)
        {
            digitalWrite(led_pins[i], HIGH);
            led_states[i] = true;
        }
    }
}
Title: Re: USB Midi Foot Controller Help
Post by: cameron206 on Aug 03, 2020, 10:40 pm
At this point with the pedal controls Id just like the 7 switches and expression pedal to work and the leds. I dont need any banks or anything really.
Well, here is some code for the switches and an EXP pedal.
NO LEDs-- you can add that when you decide precisely what you want them to do :)

It compiles for Leonardo, and I think it is self explanatory.



Code: [Select]
// midi.controller  for 7 switch DAW control
// by Cameron @the.nw.enterprise, http://thenorthwestenterprise.com/
// Library Source https://github.com/tttapa/Control-Surface
// Dependency library maybe should also be installed: https://github.com/PaulStoffregen/Encoder
// Written for PRO MICRO

#include <Control_Surface.h> // Include the library

// Instantiate a MIDI interface
//USBDebugMIDI_Interface usbmidi(115200); // uncomment this for serial monitor in ide
//HardwareSerialMIDI_Interface serialmidi = {Serial, MIDI_BAUD}; //uncomment this for 5-pin operation- this sends on TX
USBMIDI_Interface midi; // uncomment for native MIDI over USB
//HairlessMIDI_Interface hair (); // uncomment this for Hairless

/*Instantiate CCbuttons
Will fire 127 on press and 0 on release. Use a momentary switch, like the others.
Assign CC number from here:
https://tttapa.github.io/Control-Surface-doc/Doxygen/d4/dbe/namespaceMIDI__CC.html
https://www.midi.org/specifications-old/item/table-3-control-change-messages-data-bytes-2
*/

CCButton button2 = {2, {MIDI_CC::Sound_Controller_1, CHANNEL_1},};  //switch  CC#70
CCButton button3 = {3, {MIDI_CC::Sound_Controller_2, CHANNEL_1},};  //switch  CC#71
CCButton button4 = {4, {MIDI_CC::Sound_Controller_3, CHANNEL_1},};  //switch  CC#72
CCButton button5 = {5, {MIDI_CC::Sound_Controller_4, CHANNEL_1},};  //switch  CC#73
CCButton button6 = {6, {MIDI_CC::Sound_Controller_5, CHANNEL_1},};  //switch  CC#74
CCButton button7 = {7, {MIDI_CC::Sound_Controller_6, CHANNEL_1},};  //switch  CC#75
CCButton button8 = {8, {MIDI_CC::Sound_Controller_7, CHANNEL_1},};  //switch  CC#76


// Setup for an Analog pot for an EXP pedal on cc #11
CCPotentiometer potentiometer = {
  A0, {MIDI_CC::Expression_Controller, CHANNEL_1}
};


void setup() {
Control_Surface.begin(); // Initialize Control Surface
}

void loop() {
Control_Surface.loop(); // Update the Control Surface
}



*sorry Mike.  :)
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 03, 2020, 10:51 pm
Thanks.
That will give me something to look thru.
The leds arent a big deal for if I click with mouse or keyboard like you mention in previous reply.
This pedal setup is mostly just to be used with my amp sims in pc and while using the same sims inside my DAW(Reaper).

So really all Im gonna need them for is visual ref when playing and using my foot.
I have been spending some time looking over Control Surface and watching this guy on youtube that has lots of tutorials on code,those has given me some insight to how code functions a bit better.

I was not joking when I said I knew nothing about code, I just kinda jumped into. Then again I have a habit doing that with everything...lol.
Title: Re: USB Midi Foot Controller Help
Post by: Grumpy_Mike on Aug 03, 2020, 11:28 pm
Quote
As for what I dont understand about adding the expression pedal, I dont understand any of that.
Ok.
The expression peddle is controlled by MIDI Control Changes message number 0x0b, well at least this controls the most significant 7 bits of it. The least significant 7 bits of it is controlled by MIDI Control Changes message number 0x2b ( these are hexadecimal numbers which makes it easy to spot patterns you could use decimal numbers here but then they disintegrate in your mind to just random numbers )
A lot of by MIDI Control Changes message have their most significant values at 0x0(something) and their least significant value at 0x2(something).

In practice in the Arduino you only get a 10 bit reading from the A/D converter so often you only use the most significant MIDI message.

You peddle is basically a potentiometer connected to an analogue input pin so you get a reading of between 0 and 1023. If you divide that by 8 then you get a number between 0 and 127.

So to send the peddle to the appropriate MIDI controller number ( which is 0x0b ) you need to read the value and send it to the controller.
Code: [Select]
rawValue = analogRead(pin);
controlChange(0x01, 0x0b, rawValue >> 3); // assuming you are sending to channel 1

With the >> meaning shift to the right 3 places, which is a quick way of dividing by eight.
 
Now the problem with this is that it will send lots and lots of control values all being the same and flood the channel. So it is best to arrange things so that you only send data when the peddle changes. Therefore you have to keep a track of the last reading you took when you sent control data. This means a better code is:-

Code: [Select]
rawValue = analogRead(pin);
if(abs(rawValue - lastRaw) >= 8) {
         controlChange(0x01, 0x0b, rawValue >> 3); // assuming you are sending to channel 1
         lastRaw = rawValue;
}

The abs takes the absolute value of the subtraction so it works if the new value is higher or lower than the last value. Note all these new variables will need to be declaired at the start of the code before ant function definition.

So where to put this code? You want to do this as often as possible so you put it at the very beginning of the void function.

@cameron206   "*sorry Mike"
No I think you are right to do this, lets get away from that awful code.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 04, 2020, 12:30 am
Thanks Grumpy.
So of that info I knew about the pot values and such, that midi stuff had me really confused.

The void function was really messing me up last night until I saw that guy on youtube that teaches code for the arduino. He cleared up the void functions for me, I think. I'll know I try it.

As for the code Ive been using,its really not exactly what I wanted my setup to do but it was the closest I could find to base mine off of. I figured it wouldnt be too hard to edit it.

The buttons and adding more to the original code and same for the leds wasnt too difficult to figure out on my own. My biggest issue has been trying to learn the code for pot.

Ive been tinkering with the original code learning how some of the basics work since cameron showed me the Control Surface. That just looks less complicated and not near as messy.
This just takes me awhile to do stuff like this with that nerve damage. If I sit at a desk too long my skin feels like my entire body is bad sun burnt so I have to step away often.

I'll put this info to use later, I have to step away for a day until the health stuff calms down.

Thank you both,I'm sure I'll have some more questions soon.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 04, 2020, 03:42 am
Well, here is some code for the switches and an EXP pedal.
NO LEDs-- you can add that when you decide precisely what you want them to do :)

It compiles for Leonardo, and I think it is self explanatory.



Code: [Select]
// midi.controller  for 7 switch DAW control
// by Cameron @the.nw.enterprise, http://thenorthwestenterprise.com/
// Library Source https://github.com/tttapa/Control-Surface
// Dependency library maybe should also be installed: https://github.com/PaulStoffregen/Encoder
// Written for PRO MICRO

#include <Control_Surface.h> // Include the library

// Instantiate a MIDI interface
//USBDebugMIDI_Interface usbmidi(115200); // uncomment this for serial monitor in ide
//HardwareSerialMIDI_Interface serialmidi = {Serial, MIDI_BAUD}; //uncomment this for 5-pin operation- this sends on TX
USBMIDI_Interface midi; // uncomment for native MIDI over USB
//HairlessMIDI_Interface hair (); // uncomment this for Hairless

/*Instantiate CCbuttons
Will fire 127 on press and 0 on release. Use a momentary switch, like the others.
Assign CC number from here:
https://tttapa.github.io/Control-Surface-doc/Doxygen/d4/dbe/namespaceMIDI__CC.html
https://www.midi.org/specifications-old/item/table-3-control-change-messages-data-bytes-2
*/

CCButton button2 = {2, {MIDI_CC::Sound_Controller_1, CHANNEL_1},};  //switch  CC#70
CCButton button3 = {3, {MIDI_CC::Sound_Controller_2, CHANNEL_1},};  //switch  CC#71
CCButton button4 = {4, {MIDI_CC::Sound_Controller_3, CHANNEL_1},};  //switch  CC#72
CCButton button5 = {5, {MIDI_CC::Sound_Controller_4, CHANNEL_1},};  //switch  CC#73
CCButton button6 = {6, {MIDI_CC::Sound_Controller_5, CHANNEL_1},};  //switch  CC#74
CCButton button7 = {7, {MIDI_CC::Sound_Controller_6, CHANNEL_1},};  //switch  CC#75
CCButton button8 = {8, {MIDI_CC::Sound_Controller_7, CHANNEL_1},};  //switch  CC#76


// Setup for an Analog pot for an EXP pedal on cc #11
CCPotentiometer potentiometer = {
  A0, {MIDI_CC::Expression_Controller, CHANNEL_1}
};


void setup() {
Control_Surface.begin(); // Initialize Control Surface
}

void loop() {
Control_Surface.loop(); // Update the Control Surface
}



*sorry Mike.  :)
Ok couple questions here after looking at that code. So far the buttons are making sense.
But in this example:
 
Code: [Select]
CCButton button7 = {6, {MIDI_CC::Sound_Controller_6, CHANNEL_1},};  //switch  CC#75

Is the number "6" the pin slot used on the board? or is that suppose to be added after "channel_1} HERE,}"

Also in the section for expression pedal, "A0", does it always need to be that pin or any in the section labeled with A?

In the other code I was using the pins are labeled as 0,1,2 etc... but the ones labeled with "A" in the number, it was using 18,29,20 and so on.
So in control surface does it need to be told the A,A1 or can it be either?

I didnt explain that the best but this pic will show what Im asking:

(https://learn.sparkfun.com/tutorials/pro-micro--fio-v3-hookup-guide/hardware-overview-pro-micro)

https://learn.sparkfun.com/tutorials/pro-micro--fio-v3-hookup-guide/hardware-overview-pro-micro (https://learn.sparkfun.com/tutorials/pro-micro--fio-v3-hookup-guide/hardware-overview-pro-micro)
Title: Re: USB Midi Foot Controller Help
Post by: cameron206 on Aug 04, 2020, 04:54 am
Ok couple questions here after looking at that code. So far the buttons are making sense.
But in this example:
 
Code: [Select]
CCButton button7 = {6, {MIDI_CC::Sound_Controller_6, CHANNEL_1},};  //switch  CC#75

Is the number "6" the pin slot used on the board? or is that suppose to be added after "channel_1} HERE,}"

Also in the section for expression pedal, "A0", does it always need to be that pin or any in the section labeled with A?


Yes, the 6 is pin 6.. same with the others.
And the A0 can be any analog pin. On your diagram pins 18-21 are Analog pins 0-3.. as silkscreened on the board.

IN re: the LEDs.. look at the examples in the "MIDI-Callback" section if you wanna do this ~correctly~.
If you really want to get that going, you gotta set up your DAW to pulse out some MIDI messages to let your controller know the position or state of the function you are controlling. Generic remotes often don't do this, so depending on your DAW.. you might have to emulate some supported controller. This just entails programming your control with the same messages, and setting up your DAW to commune with it.

It sounds more complicated than it is... but is really the only way to have your LEDs consistently reflect the position of your parameter, unless you ALWAYS start with the parameters off, and all your project files have them off, etc.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 04, 2020, 05:04 am
Ok that makes sense. SO when making the code I just enter whats printed on the board for the A pins?

When I made things using MMjoy in place of making my own code for other projects, it used a totally different numbering than what was on the board so Im just making sure.

Havent really tried anything with code, just reading it over to make sense, so far much easier for me than the old mess.

Thank you.
Title: Re: USB Midi Foot Controller Help
Post by: Grumpy_Mike on Aug 04, 2020, 09:03 am
Quote
The void function was really messing me up last night
Void is simply telling the compiler that this function returns no values. When beginners talk about the void function they normally mean the loop function. But any function can be a void function. If your YouTube person  didn't make that clear that marks him out as a beginner himself, so he might have made other mistakes.
Title: Re: USB Midi Foot Controller Help
Post by: PieterP on Aug 04, 2020, 10:28 am
what I want my pedal project to do is:
7 switches each with own led. leds to light on/off depending if the switch is on/off.
Have 6 of those switches bankable.
Have 1 expression pedal.
Be USB
The following code is for 4 momentary push buttons that send MIDI Control Change events when pressed. They are latched in software, so if you press them once, they send an "on" message, and when you press them a second time, they send an "off" message.
The controller numbers of the buttons can be changed using banks. The active bank is selected using two push buttons (up and down), you can use other kinds of selectors (https://tttapa.github.io/Control-Surface-doc/Doxygen/d0/d0c/group__Selectors.html) as well.
The LEDs reflect the state of the buttons.
Code: [Select]
#include <Control_Surface.h> // Include the Control Surface library
 
// Instantiate a MIDI over USB interface
USBMIDI_Interface midi;
 
// Instantiate four Banks, with eight tracks per bank.
Bank<4> bank(8);
//   │       └───── number of tracks per bank
//   └───────────── number of banks
 
// Instantiate a Bank selector to control which one of the four Banks is active.
IncrementDecrementSelector<4> selector = {
    bank,       // Bank to manage
    {2, 3},     // Push button pins (increment, decrement)
    Wrap::Wrap, // Wrap around
};

// Instantiate an array of latched push buttons that send MIDI CC messages
// when pressed.
Bankable::CCButtonLatched<4> buttons[] {
  { bank, 4, 0x10 },
  //  │   │    └──── Base MIDI CC controller number
  //  │   └───────── Button pin number
  //  └───────────── Bank that changes the controller number
  { bank, 5, 0x11 },
  { bank, 6, 0x12 },
  { bank, 7, 0x13 },
};

// The bank controls the offset of the controller number relative to the
// button's base address. The amount of offset depends on the "number of
// tracks per bank" argument used when creating the bank. In this case,
// it's 8 tracks per bank, so for every bank you go up, 8 is added to the
// controller number of each button:
//
//         │  Button 1  │  Button 2  │  Button 3  │  Button 4  │  Offset
// ────────┼────────────┼────────────┼────────────┼────────────┤
// Bank 1  │    0x10    │    0x11    │    0x12    │    0x13    │  0×8=0
// Bank 2  │    0x18    │    0x19    │    0x1A    │    0x1B    │  1×8=8
// Bank 3  │    0x20    │    0x21    │    0x22    │    0x23    │  2×8=16
// Bank 4  │    0x28    │    0x29    │    0x2A    │    0x2B    │  3×8=24

// The array of pin numbers for LEDs that display the states of the buttons.
const pin_t ledPins[] = { 10, 11, 12, 13 };

// Get the length of an array
template <class T, size_t N> constexpr size_t length(T (&)[N]) { return N; }

static_assert(length(buttons) == length(ledPins),
              "Error: requires same number of buttons as LEDs");

void setup() {
  Control_Surface.begin();  // Initialize the Control Surface
  for (auto pin : ledPins)  // Set the pinMode to output for all LEDs
      pinMode(pin, OUTPUT);
}

void loop() {
  Control_Surface.loop();  // Update the Control Surface

  // Loop over all buttons and LEDs
  for (size_t i = 0; i < length(buttons); ++i)
      // Update the LED states to reflect the toggled switch states
      digitalWrite(ledPins[i], buttons[i].getState() ? HIGH : LOW);
}


If you want the LEDs to reflect the state by the software or device you're talking to, you can use the CCValueLED (https://tttapa.github.io/Control-Surface-doc/Doxygen/d3/dca/classBankable_1_1CCValueLED.html). It listens for incoming MIDI CC messages. This only works if the software/device sends feedback, of course.

The code is even simpler than the previous one:
Code: [Select]
#include <Control_Surface.h> // Include the Control Surface library
 
// Instantiate a MIDI over USB interface
USBMIDI_Interface midi;
 
// Instantiate four Banks, with eight tracks per bank.
Bank<4> bank(8);
//   │       └───── number of tracks per bank
//   └───────────── number of banks
 
// Instantiate a Bank selector to control which one of the four Banks is active.
IncrementDecrementSelector<4> selector = {
    bank,       // Bank to manage
    {2, 3},     // Push button pins (increment, decrement)
    Wrap::Wrap, // Wrap around
};

// Instantiate an array of latched push buttons that send MIDI CC messages
// when pressed.
Bankable::CCButtonLatched<4> buttons[] {
  { bank, 4, 0x10 },
  //  │   │    └──── Base MIDI CC controller number
  //  │   └───────── Button pin number
  //  └───────────── Bank that changes the controller number
  { bank, 5, 0x11 },
  { bank, 6, 0x12 },
  { bank, 7, 0x13 },
};

// The bank controls the offset of the controller number relative to the
// button's base address. The amount of offset depends on the "number of
// tracks per bank" argument used when creating the bank. In this case,
// it's 8 tracks per bank, so for every bank you go up, 8 is added to the
// controller number of each button:
//
//         │  Button 1  │  Button 2  │  Button 3  │  Button 4  │  Offset
// ────────┼────────────┼────────────┼────────────┼────────────┤
// Bank 1  │    0x10    │    0x11    │    0x12    │    0x13    │  0×8=0
// Bank 2  │    0x18    │    0x19    │    0x1A    │    0x1B    │  1×8=8
// Bank 3  │    0x20    │    0x21    │    0x22    │    0x23    │  2×8=16
// Bank 4  │    0x28    │    0x29    │    0x2A    │    0x2B    │  3×8=24

// Instantiate an array of LEDs that listen for incoming MIDI CC messages
// and displays their values.
Bankable::CCValueLED<4>leds[] {
  { bank, 10, 0x10 },
  //  │    │    └──── Base MIDI CC controller number
  //  │    └───────── LED pin number
  //  └────────────── Bank that changes the controller number
  { bank, 11, 0x11 },
  { bank, 12, 0x12 },
  { bank, 13, 0x13 },
};

void setup() {
  Control_Surface.begin();  // Initialize the Control Surface
}

void loop() {
  Control_Surface.loop();  // Update the Control Surface
}


For the expression pedal, you can have a look at the Conrol-Change-Potentiometer.ino (https://tttapa.github.io/Control-Surface-doc/Doxygen/de/de4/Control-Change-Potentiometer_8ino-example.html) example.

I read that I would need the midiusb control surface library, correct?
Correct, Control Surface uses MIDIUSB as a backend for most Arduino boards.

IN re: the LEDs.. look at the examples in the "MIDI-Callback" section if you wanna do this ~correctly~.
IN re: the LEDS.. if you want it to reflect the STATE of a condition in your DAW.. you gotta get into callbacks.
Not necessarily, Control Surface comes with many classes that automatically listen for MIDI input, see the full list of MIDI Input Elements (https://tttapa.github.io/Control-Surface-doc/Doxygen/df/d8b/group__MIDIInputElements.html).

If these classes don't suit your needs, you can use MIDI callbacks instead. They're the most flexible approach, because they allow you to interact with the incoming MIDI messages directly, but that requires a bit more programming, of course.

When beginners talk about the void function they normally mean the loop function. But any function can be a void function. If your YouTube person  didn't make that clear that marks him out as a beginner himself, so he might have made other mistakes.
You want to do this as often as possible so you put it at the very beginning of the void function.
?

Pieter
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 04, 2020, 04:06 pm
The following code is for 4 momentary push buttons that send MIDI Control Change events when pressed. They are latched in software, so if you press them once, they send an "on" message, and when you press them a second time, they send an "off" message.
The controller numbers of the buttons can be changed using banks. The active bank is selected using two push buttons (up and down), you can use other kinds of selectors (https://tttapa.github.io/Control-Surface-doc/Doxygen/d0/d0c/group__Selectors.html) as well.
The LEDs reflect the state of the buttons.
Code: [Select]
#include <Control_Surface.h> // Include the Control Surface library
 
// Instantiate a MIDI over USB interface
USBMIDI_Interface midi;
 
// Instantiate four Banks, with eight tracks per bank.
Bank<4> bank(8);
//   │       └───── number of tracks per bank
//   └───────────── number of banks
 
// Instantiate a Bank selector to control which one of the four Banks is active.
IncrementDecrementSelector<4> selector = {
    bank,       // Bank to manage
    {2, 3},     // Push button pins (increment, decrement)
    Wrap::Wrap, // Wrap around
};

// Instantiate an array of latched push buttons that send MIDI CC messages
// when pressed.
Bankable::CCButtonLatched<4> buttons[] {
  { bank, 4, 0x10 },
  //  │   │    └──── Base MIDI CC controller number
  //  │   └───────── Button pin number
  //  └───────────── Bank that changes the controller number
  { bank, 5, 0x11 },
  { bank, 6, 0x12 },
  { bank, 7, 0x13 },
};

// The bank controls the offset of the controller number relative to the
// button's base address. The amount of offset depends on the "number of
// tracks per bank" argument used when creating the bank. In this case,
// it's 8 tracks per bank, so for every bank you go up, 8 is added to the
// controller number of each button:
//
//         │  Button 1  │  Button 2  │  Button 3  │  Button 4  │  Offset
// ────────┼────────────┼────────────┼────────────┼────────────┤
// Bank 1  │    0x10    │    0x11    │    0x12    │    0x13    │  0×8=0
// Bank 2  │    0x18    │    0x19    │    0x1A    │    0x1B    │  1×8=8
// Bank 3  │    0x20    │    0x21    │    0x22    │    0x23    │  2×8=16
// Bank 4  │    0x28    │    0x29    │    0x2A    │    0x2B    │  3×8=24

// The array of pin numbers for LEDs that display the states of the buttons.
const pin_t ledPins[] = { 10, 11, 12, 13 };

// Get the length of an array
template <class T, size_t N> constexpr size_t length(T (&)[N]) { return N; }

static_assert(length(buttons) == length(ledPins),
              "Error: requires same number of buttons as LEDs");

void setup() {
  Control_Surface.begin();  // Initialize the Control Surface
  for (auto pin : ledPins)  // Set the pinMode to output for all LEDs
      pinMode(pin, OUTPUT);
}

void loop() {
  Control_Surface.loop();  // Update the Control Surface

  // Loop over all buttons and LEDs
  for (size_t i = 0; i < length(buttons); ++i)
      // Update the LED states to reflect the toggled switch states
      digitalWrite(ledPins[i], buttons[i].getState() ? HIGH : LOW);
}


If you want the LEDs to reflect the state by the software or device you're talking to, you can use the CCValueLED (https://tttapa.github.io/Control-Surface-doc/Doxygen/d3/dca/classBankable_1_1CCValueLED.html). It listens for incoming MIDI CC messages. This only works if the software/device sends feedback, of course.

The code is even simpler than the previous one:
Code: [Select]
#include <Control_Surface.h> // Include the Control Surface library
 
// Instantiate a MIDI over USB interface
USBMIDI_Interface midi;
 
// Instantiate four Banks, with eight tracks per bank.
Bank<4> bank(8);
//   │       └───── number of tracks per bank
//   └───────────── number of banks
 
// Instantiate a Bank selector to control which one of the four Banks is active.
IncrementDecrementSelector<4> selector = {
    bank,       // Bank to manage
    {2, 3},     // Push button pins (increment, decrement)
    Wrap::Wrap, // Wrap around
};

// Instantiate an array of latched push buttons that send MIDI CC messages
// when pressed.
Bankable::CCButtonLatched<4> buttons[] {
  { bank, 4, 0x10 },
  //  │   │    └──── Base MIDI CC controller number
  //  │   └───────── Button pin number
  //  └───────────── Bank that changes the controller number
  { bank, 5, 0x11 },
  { bank, 6, 0x12 },
  { bank, 7, 0x13 },
};

// The bank controls the offset of the controller number relative to the
// button's base address. The amount of offset depends on the "number of
// tracks per bank" argument used when creating the bank. In this case,
// it's 8 tracks per bank, so for every bank you go up, 8 is added to the
// controller number of each button:
//
//         │  Button 1  │  Button 2  │  Button 3  │  Button 4  │  Offset
// ────────┼────────────┼────────────┼────────────┼────────────┤
// Bank 1  │    0x10    │    0x11    │    0x12    │    0x13    │  0×8=0
// Bank 2  │    0x18    │    0x19    │    0x1A    │    0x1B    │  1×8=8
// Bank 3  │    0x20    │    0x21    │    0x22    │    0x23    │  2×8=16
// Bank 4  │    0x28    │    0x29    │    0x2A    │    0x2B    │  3×8=24

// Instantiate an array of LEDs that listen for incoming MIDI CC messages
// and displays their values.
Bankable::CCValueLED<4>leds[] {
  { bank, 10, 0x10 },
  //  │    │    └──── Base MIDI CC controller number
  //  │    └───────── LED pin number
  //  └────────────── Bank that changes the controller number
  { bank, 11, 0x11 },
  { bank, 12, 0x12 },
  { bank, 13, 0x13 },
};

void setup() {
  Control_Surface.begin();  // Initialize the Control Surface
}

void loop() {
  Control_Surface.loop();  // Update the Control Surface
}


For the expression pedal, you can have a look at the Conrol-Change-Potentiometer.ino (https://tttapa.github.io/Control-Surface-doc/Doxygen/de/de4/Control-Change-Potentiometer_8ino-example.html) example.
Correct, Control Surface uses MIDIUSB as a backend for most Arduino boards.
Not necessarily, Control Surface comes with many classes that automatically listen for MIDI input, see the full list of MIDI Input Elements (https://tttapa.github.io/Control-Surface-doc/Doxygen/df/d8b/group__MIDIInputElements.html).

If these classes don't suit your needs, you can use MIDI callbacks instead. They're the most flexible approach, because they allow you to interact with the incoming MIDI messages directly, but that requires a bit more programming, of course.
?

Pieter
Thank you for all that information. That has cleared up some things for me, I think. I'll find out when I try using the info.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 04, 2020, 04:12 pm
Void is simply telling the compiler that this function returns no values. When beginners talk about the void function they normally mean the loop function. But any function can be a void function. If your YouTube person  didn't make that clear that marks him out as a beginner himself, so he might have made other mistakes.
He may have made that clear I kinda glanced thru a couple videos. He seemed to make things fairly clear.

If your interested in knowing if he really knows this stuff here is a link. He has many vids. His name is Paul McWhorter

https://www.youtube.com/watch?v=fJWR7dBuc18
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 05, 2020, 10:45 am
OK I have the switches and exp pedal working perfect.
I tried the leds and got nothing but a ton of errors when compiling the code.
I also opted to not do banks since it wasnt really needed for my needs but I did add another switch and led in its place.
Also when using this code with my amp sim I have to go into the program and manually set to latching, problem is  3-4 of the settings do not allow me to set them for latching.
I didnt really understand your examples of the leds or how to make latching especially since I decided to not use banks.

Heres the code, I know I messed it up just not understanding. As mentioned in previous replies, this is my 1st time to try coding.

Code: [Select]
// midi.controller  for 7 switch DAW control
// by Cameron @the.nw.enterprise, http://thenorthwestenterprise.com/
// Library Source https://github.com/tttapa/Control-Surface
// Dependency library maybe should also be installed: https://github.com/PaulStoffregen/Encoder
// Written for PRO MICRO

#include <Encoder.h>
#include <Control_Surface.h> // Include the library


// Instantiate a MIDI interface
USBMIDI_Interface midi; // uncomment for native MIDI over USB

/*Instantiate CCbuttons
Will fire 127 on press and 0 on release. Use a momentary switch, like the others.
Assign CC number from here:
https://tttapa.github.io/Control-Surface-doc/Doxygen/d4/dbe/namespaceMIDI__CC.html
https://www.midi.org/specifications-old/item/table-3-control-change-messages-data-bytes-2
*/

CCButton button9 = {0, {MIDI_CC::Sound_Controller_8, CHANNEL_1},};  //switch  CC#77
CCButton button10 = {1, {MIDI_CC::Sound_Controller_9, CHANNEL_1},};  //switch  CC#78
CCButton button2 = {2, {MIDI_CC::Sound_Controller_1, CHANNEL_1},};  //switch  CC#70
CCButton button3 = {3, {MIDI_CC::Sound_Controller_2, CHANNEL_1},};  //switch  CC#71
CCButton button4 = {4, {MIDI_CC::Sound_Controller_3, CHANNEL_1},};  //switch  CC#72
CCButton button5 = {5, {MIDI_CC::Sound_Controller_4, CHANNEL_1},};  //switch  CC#73
CCButton button6 = {6, {MIDI_CC::Sound_Controller_5, CHANNEL_1},};  //switch  CC#74
CCButton button7 = {7, {MIDI_CC::Sound_Controller_6, CHANNEL_1},};  //switch  CC#75
CCButton button8 = {8, {MIDI_CC::Sound_Controller_7, CHANNEL_1},};  //switch  CC#76



// Setup for an Analog pot for an EXP pedal on cc #11
CCPotentiometer potentiometer = {
  A3, {MIDI_CC::Expression_Controller, CHANNEL_1}
};

const pin_t ledPins[] = { 9, 10, 16, 14, 15, 18, 19, 20 };

 
// Get the length of an array
template <class T, size_t N> constexpr size_t length(T (&)[N]) { return N; }

static_assert(length(8) == length(8),
              "Error: requires same number of buttons as LEDs");

void setup() {
  Control_Surface.begin();  // Initialize the Control Surface
  for (auto pin : ledPins)  // Set the pinMode to output for all LEDs
      pinMode(pin, OUTPUT);
}

void loop() {
  Control_Surface.loop();  // Update the Control Surface

  // Loop over all buttons and LEDs
  for (size_t i = 0; i < length(8); ++i)
      // Update the LED states to reflect the toggled switch states
      digitalWrite(ledPins[i], buttons[i].getState() ? HIGH : LOW);
}
Title: Re: USB Midi Foot Controller Help
Post by: PieterP on Aug 05, 2020, 11:50 am
Why did you get rid of the "buttons" array? The rest of the code relies on the array existing.

The "length" function returns the length of an array, so
Code: [Select]
static_assert(length(8) == length(8)) doesn't make any sense, because "8" is an integer, not an array. The goal of the assertion is to check that the number of buttons is the same as the number of LEDs, otherwise, the code below could access out of bounds of one of the arrays.

The CCButton class doesn't latch the switch state, so it doesn't have a "getState" method, it only has a "getButtonState" method, which returns either Falling, Pressed, Rising, Released.

Also when using this code with my amp sim I have to go into the program and manually set to latching, problem is  3-4 of the settings do not allow me to set them for latching.
Are you using CCButton or CCButtonLatched? CCButton sends the state of input pin directly. CCButtonLatched latches the input: press once to send "on" message (release does nothing), press a second time to send "off" message (release does nothing).
It sounds to me like you need CCButtonLatched, not CCButton.

I didnt really understand your examples of the leds or how to make latching especially since I decided to not use banks.
If you don't want to use banks, just remove the "Bankable::", the number of banks, and the "bank" argument from the buttons:
Code: [Select]
#include <Control_Surface.h> // Include the Control Surface library
 
// Instantiate a MIDI over USB interface
USBMIDI_Interface midi;

// Instantiate an array of latched push buttons that send MIDI CC messages
// when pressed.
CCButtonLatched buttons[] {
  {  4, 0x10 },
  // │    └──── MIDI CC controller number
  // └───────── Button pin number
  { 5, 0x11 },
  { 6, 0x12 },
  { 7, 0x13 },
};

// The array of pin numbers for LEDs that display the states of the buttons.
const pin_t ledPins[] = { 10, 11, 12, 13 };

// Get the length of an array
template <class T, size_t N> constexpr size_t length(T (&)[N]) { return N; }

static_assert(length(buttons) == length(ledPins),
              "Error: requires same number of buttons as LEDs");

void setup() {
  Control_Surface.begin();  // Initialize the Control Surface
  for (auto pin : ledPins)  // Set the pinMode to output for all LEDs
      pinMode(pin, OUTPUT);
}

void loop() {
  Control_Surface.loop();  // Update the Control Surface

  // Loop over all buttons and LEDs
  for (size_t i = 0; i < length(buttons); ++i)
      // Update the LED states to reflect the toggled switch states
      digitalWrite(ledPins[i], buttons[i].getState() ? HIGH : LOW);
}
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 05, 2020, 04:52 pm
Why did you get rid of the "buttons" array? The rest of the code relies on the array existing.


I see, I didnt understand the buttons array. I was looking at your whole example differently than meant.
Like I said total new guy at this,never touched code in my life.

So I would add the buttons array(your entire code) to my already existing code I have working? But remove the word bankable.
 My Current:

Code: [Select]
// midi.controller  for 7 switch DAW control
// by Cameron @the.nw.enterprise, http://thenorthwestenterprise.com/
// Library Source https://github.com/tttapa/Control-Surface
// Dependency library maybe should also be installed: https://github.com/PaulStoffregen/Encoder
// Written for PRO MICRO

#include <Control_Surface.h> // Include the library

// Instantiate a MIDI interface
//USBDebugMIDI_Interface usbmidi(115200); // uncomment this for serial monitor in ide
//HardwareSerialMIDI_Interface serialmidi = {Serial, MIDI_BAUD}; //uncomment this for 5-pin operation- this sends on TX
USBMIDI_Interface midi; // uncomment for native MIDI over USB
//HairlessMIDI_Interface hair (); // uncomment this for Hairless

/*Instantiate CCbuttons
Will fire 127 on press and 0 on release. Use a momentary switch, like the others.
Assign CC number from here:
https://tttapa.github.io/Control-Surface-doc/Doxygen/d4/dbe/namespaceMIDI__CC.html
https://www.midi.org/specifications-old/item/table-3-control-change-messages-data-bytes-2
*/

CCButton button2 = {2, {MIDI_CC::Sound_Controller_1, CHANNEL_1},};  //switch  CC#70
CCButton button3 = {3, {MIDI_CC::Sound_Controller_2, CHANNEL_1},};  //switch  CC#71
CCButton button4 = {4, {MIDI_CC::Sound_Controller_3, CHANNEL_1},};  //switch  CC#72
CCButton button5 = {5, {MIDI_CC::Sound_Controller_4, CHANNEL_1},};  //switch  CC#73
CCButton button6 = {6, {MIDI_CC::Sound_Controller_5, CHANNEL_1},};  //switch  CC#74
CCButton button7 = {7, {MIDI_CC::Sound_Controller_6, CHANNEL_1},};  //switch  CC#75
CCButton button8 = {8, {MIDI_CC::Sound_Controller_7, CHANNEL_1},};  //switch  CC#76


// Setup for an Analog pot for an EXP pedal on cc #11
CCPotentiometer potentiometer = {
  A0, {MIDI_CC::Expression_Controller, CHANNEL_1}
};


void setup() {
Control_Surface.begin(); // Initialize Control Surface
}

void loop() {
Control_Surface.loop(); // Update the Control Surface
}
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 05, 2020, 07:07 pm
Alright so I played with the code some more.
I have the buttons working correct and expression pedal.

But adding the leds like I thought you said in your last reply breaks the code when compiling.

It works until I add everything after "CCPtotntiometer". This section Im having a hard time understanding, so Im a bit lost there. I starting to understand more just those led sections.

Code: [Select]
#include <Encoder.h>
#include <Control_Surface.h> // Include the Control Surface library
 
// Instantiate a MIDI over USB interface
USBMIDI_Interface midi;
 
// Instantiate four Banks, with eight tracks per bank.

//   │       └───── number of tracks per bank
//   └───────────── number of banks
 
// Instantiate a Bank selector to control which one of the four Banks is active.


// Instantiate an array of latched push buttons that send MIDI CC messages
// when pressed.
CCButtonLatched buttons[] {
  { 4, 0x10 },
  //  │   │    └──── Base MIDI CC controller number
  //  │   └───────── Button pin number
  //  └───────────── Bank that changes the controller number
  { 1, 0x11 },
  { 0, 0x12 },
  { 2, 0x13 },
  { 3, 0x14 },
  { 4, 0x15 },
  { 5, 0x16 },
  { 6, 0x17 },
  { 7, 0x18 },
 
};

// The bank controls the offset of the controller number relative to the
// button's base address. The amount of offset depends on the "number of
// tracks per bank" argument used when creating the bank. In this case,
// it's 8 tracks per bank, so for every bank you go up, 8 is added to the
// controller number of each button:
//
//         │  Button 1  │  Button 2  │  Button 3  │  Button 4  │  Offset
// ────────┼────────────┼────────────┼────────────┼────────────┤
// Bank 1  │    0x10    │    0x11    │    0x12    │    0x13    │  0×8=0
// Bank 2  │    0x18    │    0x19    │    0x1A    │    0x1B    │  1×8=8
// Bank 3  │    0x20    │    0x21    │    0x22    │    0x23    │  2×8=16
// Bank 4  │    0x28    │    0x29    │    0x2A    │    0x2B    │  3×8=24

// Instantiate an array of LEDs that listen for incoming MIDI CC messages
// and displays their values.
CCValueLED leds[] {
  { 10, 0x10 },
  //  │    │    └──── Base MIDI CC controller number
  //  │    └───────── LED pin number
  //  └────────────── Bank that changes the controller number
  { 9, 0x10 },
  { 10, 0x11 },
  { 16, 0x12 },
  { 14, 0x13 },
  { 15, 0x14 },
  { 18, 0x15 },
  { 19, 0x16 },
  { 20, 0x17 },
 
 
};


// Setup for an Analog pot for an EXP pedal on cc #11
CCPotentiometer potentiometer = {
  A3, {MIDI_CC::Expression_Controller, CHANNEL_1}
};


// The array of pin numbers for LEDs that display the states of the buttons.
const pin_t ledPins[] = { 9, 10, 16, 14, 15, 18, 19, 20 };

// Get the length of an array
template <class T, size_t N> constexpr size_t length(T (&)[N]) { return N; }

static_assert(length(buttons) == length(ledPins),
              "Error: requires same number of buttons as LEDs");

void setup() {
  Control_Surface.begin();  // Initialize the Control Surface
  for (auto pin : ledPins)  // Set the pinMode to output for all LEDs
      pinMode(pin, OUTPUT);
}

void loop() {
  Control_Surface.loop();  // Update the Control Surface

  // Loop over all buttons and LEDs
  for (size_t i = 0; i < length(buttons); ++i)
      // Update the LED states to reflect the toggled switch states
      digitalWrite(ledPins[i], buttons[i].getState() ? HIGH : LOW);
}
Title: Re: USB Midi Foot Controller Help
Post by: PieterP on Aug 05, 2020, 07:48 pm
You seem very confused, I'd recommend taking a step back and starting with a simple sketch.
Don't start throwing different components together until you understand how each component works separately.
Feel free to ask specific questions about the parts you don't understand.

The two code blocks I posted in reply #30 are two separate programs/sketches.
The first one uses the LEDs to display the state of the latched buttons.
The second one listens for incoming MIDI messages and displays the value of these messages to the LEDs.
You cannot do both at once, so you'll have to choose one.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 05, 2020, 08:59 pm
You seem very confused, I'd recommend taking a step back and starting with a simple sketch.
Don't start throwing different components together until you understand how each component works separately.
Feel free to ask specific questions about the parts you don't understand.

The two code blocks I posted in reply #30 are two separate programs/sketches.
The first one uses the LEDs to display the state of the latched buttons.
The second one listens for incoming MIDI messages and displays the value of these messages to the LEDs.
You cannot do both at once, so you'll have to choose one.
Did not realise I used both.
I have 8 momentary switches each with an LED. I just want to show the state of the latched button.Plus I have the expression pedal,no led.
The button pins

Using your code and deleting the banks in the code everything but led's function.

So where I have the list of CCbuttonLatched and CCValueLeds, did I do that part correct?
If so then the corrections need to be made here?

Code: [Select]
// The array of pin numbers for LEDs that display the states of the buttons.
const pin_t ledPins[] = { 9, 10, 16, 14, 15, 18, 19, 20 };

// Get the length of an array
template <class T, size_t N> constexpr size_t length(T (&)[N]) { return N; }

static_assert(length(buttons) == length(ledPins),
              "Error: requires same number of buttons as LEDs");

void setup() {
  Control_Surface.begin();  // Initialize the Control Surface
  for (auto pin : ledPins)  // Set the pinMode to output for all LEDs
      pinMode(pin, OUTPUT);
}

void loop() {
  Control_Surface.loop();  // Update the Control Surface

  // Loop over all buttons and LEDs
  for (size_t i = 0; i < length(buttons); ++i)
      // Update the LED states to reflect the toggled switch states
      digitalWrite(ledPins[i], buttons[i].getState() ? HIGH : LOW);
}


I'm assuming this is the bad section...? Just need to learn where I made the mistakes,its how I learn.

Code: [Select]
// The array of pin numbers for LEDs that display the states of the buttons.
const pin_t ledPins[] = { 9, 10, 16, 14, 15, 18, 19, 20 };


I do appreciate all the help, Thank you.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 05, 2020, 09:49 pm
Played with the code some more. No errors, but also no LED's. All the switches work though.

Code: [Select]
#include <Encoder.h>
#include <Control_Surface.h> // Include the Control Surface library
 
// Instantiate a MIDI over USB interface
USBMIDI_Interface midi;
 
// Instantiate four Banks, with eight tracks per bank.

//   │       └───── number of tracks per bank
//   └───────────── number of banks
 
// Instantiate a Bank selector to control which one of the four Banks is active.


// Instantiate an array of latched push buttons that send MIDI CC messages
// when pressed.
CCButtonLatched buttons[] {
  { 4, 0x10 },
  //  │   │    └──── Base MIDI CC controller number
  //  │   └───────── Button pin number
  //  └───────────── Bank that changes the controller number
  { 1, 0x11 },
  { 0, 0x12 },
  { 2, 0x13 },
  { 3, 0x14 },
  { 4, 0x15 },
  { 5, 0x16 },
  { 6, 0x17 },
  { 7, 0x18 },
 
};

// The bank controls the offset of the controller number relative to the
// button's base address. The amount of offset depends on the "number of
// tracks per bank" argument used when creating the bank. In this case,
// it's 8 tracks per bank, so for every bank you go up, 8 is added to the
// controller number of each button:
//
//         │  Button 1  │  Button 2  │  Button 3  │  Button 4  │  Offset
// ────────┼────────────┼────────────┼────────────┼────────────┤
// Bank 1  │    0x10    │    0x11    │    0x12    │    0x13    │  0×8=0
// Bank 2  │    0x18    │    0x19    │    0x1A    │    0x1B    │  1×8=8
// Bank 3  │    0x20    │    0x21    │    0x22    │    0x23    │  2×8=16
// Bank 4  │    0x28    │    0x29    │    0x2A    │    0x2B    │  3×8=24




// Setup for an Analog pot for an EXP pedal on cc #11
CCPotentiometer potentiometer = {
  A3, {MIDI_CC::Expression_Controller, CHANNEL_1}
};


// Instantiate an array of LEDs that listen for incoming MIDI CC messages
// and displays their values.
CCValueLED leds[] {
  { 10, 0x10 },
  //  │    │    └──── Base MIDI CC controller number
  //  │    └───────── LED pin number
  //  └────────────── Bank that changes the controller number
  { 9, 0x11 },
  { 10, 0x12 },
  { 16, 0x13 },
  { 14, 0x14 },
  { 15, 0x15 },
  { 18, 0x16 },
  { 19, 0x17 },
  { 20, 0x18 },
};

void setup() {
  Control_Surface.begin();  // Initialize the Control Surface
}

void loop() {
  Control_Surface.loop();  // Update the Control Surface
}
Title: Re: USB Midi Foot Controller Help
Post by: PieterP on Aug 05, 2020, 10:40 pm
I have 8 momentary switches each with an LED. I just want to show the state of the latched button.Plus I have the expression pedal,no led.
Then you have to use the sketch without CCValueLED.
Like I said before, CCValueLED listens for incoming MIDI messages. It's meant to be used for DAWs that send MIDI feedback. If the LEDs aren't working with your code from reply #39, that probably means your DAW doesn't send any feedback.

Using your code and deleting the banks in the code everything but led's function.

So where I have the list of CCbuttonLatched and CCValueLeds, did I do that part correct?
If so then the corrections need to be made here?

Code: [Select]
// The array of pin numbers for LEDs that display the states of the buttons.
const pin_t ledPins[] = { 9, 10, 16, 14, 15, 18, 19, 20 };

// Get the length of an array
template <class T, size_t N> constexpr size_t length(T (&)[N]) { return N; }

static_assert(length(buttons) == length(ledPins),
              "Error: requires same number of buttons as LEDs");

void setup() {
  Control_Surface.begin();  // Initialize the Control Surface
  for (auto pin : ledPins)  // Set the pinMode to output for all LEDs
      pinMode(pin, OUTPUT);
}

void loop() {
  Control_Surface.loop();  // Update the Control Surface

  // Loop over all buttons and LEDs
  for (size_t i = 0; i < length(buttons); ++i)
      // Update the LED states to reflect the toggled switch states
      digitalWrite(ledPins[i], buttons[i].getState() ? HIGH : LOW);
}

I'm not sure what you mean, there's no array of CCButtonLatched or CCValueLED in that code.

I'm assuming this is the bad section...? Just need to learn where I made the mistakes,its how I learn.

Code: [Select]
// The array of pin numbers for LEDs that display the states of the buttons.
const pin_t ledPins[] = { 9, 10, 16, 14, 15, 18, 19, 20 };

Why? That's just an array declaration/initialization of some pin numbers, it doesn't do anything if you don't use it.

There's an important distinction:
Instantiating MIDI Elements (e.g. CCButton, CCValueLED, USBMIDI_Interface ...) has side effects. The Control Surface library will detect that you created them, and will initialize and update them for you when you call Control_Surface.begin() and Control_Surface.loop().
If you instantiate most other types (integers, pin_t, float ...), there are no side effects. If you instantiate them but don't use them, your code doesn't behave any differently than before you instantiated them.

I'll try to explain it from the beginning:

The most basic sketch uses just an array of CCButtonLatched. Control Surface will automatically initialize them for you, and then update them continuously in the loop. When it detects a button press, it sends a MIDI message.

If you want to add LEDs to display the state of these buttons, you have to add some code yourself:

1. You have to initialize the LED pins in the setup, set their pin states to OUTPUT mode.
Code: [Select]
 for (auto pin : ledPins)  // Set the pinMode to output for all LEDs
      pinMode(pin, OUTPUT);

2. You have to check the state of each button, and then update the LED state appropriately.
Code: [Select]
 // Loop over all buttons and LEDs
  for (size_t i = 0; i < length(buttons); ++i)
      // Update the LED states to reflect the toggled switch states
      digitalWrite(ledPins[i], buttons[i].getState() ? HIGH : LOW);


You should be able to understand at least these 4 lines of code before combining it with any other code.
Search terms: "C++ range based for loop", "Arduino pinMode", "C++ for loop", "Arduino digitalWrite", "C++ conditional operator", "C++ array"
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 05, 2020, 11:01 pm
Then you have to use the sketch without CCValueLED.
Like I said before, CCValueLED listens for incoming MIDI messages. It's meant to be used for DAWs that send MIDI feedback. If the LEDs aren't working with your code from reply #39, that probably means your DAW doesn't send any feedback.
I'm not sure what you mean, there's no array of CCButtonLatched or CCValueLED in that code.
Why? That's just an array declaration/initialization of some pin numbers, it doesn't do anything if you don't use it.

Sorry I post the wrong section of code for the question.
I had the leds setup with the CCValue also in this code but removed that part.

Code: [Select]
/*Instantiate CCbuttons
Will fire 127 on press and 0 on release. Use a momentary switch, like the others.
Assign CC number from here:
https://tttapa.github.io/Control-Surface-doc/Doxygen/d4/dbe/namespaceMIDI__CC.html
https://www.midi.org/specifications-old/item/table-3-control-change-messages-data-bytes-2
*/

CCButton button9 = {0, {MIDI_CC::Sound_Controller_8, CHANNEL_1},};  //switch  CC#77
CCButton button10 = {1, {MIDI_CC::Sound_Controller_9, CHANNEL_1},};  //switch  CC#78
CCButton button2 = {2, {MIDI_CC::Sound_Controller_1, CHANNEL_1},};  //switch  CC#70
CCButton button3 = {3, {MIDI_CC::Sound_Controller_2, CHANNEL_1},};  //switch  CC#71
CCButton button4 = {4, {MIDI_CC::Sound_Controller_3, CHANNEL_1},};  //switch  CC#72
CCButton button5 = {5, {MIDI_CC::Sound_Controller_4, CHANNEL_1},};  //switch  CC#73
CCButton button6 = {6, {MIDI_CC::Sound_Controller_5, CHANNEL_1},};  //switch  CC#74
CCButton button7 = {7, {MIDI_CC::Sound_Controller_6, CHANNEL_1},};  //switch  CC#75
CCButton button8 = {8, {MIDI_CC::Sound_Controller_7, CHANNEL_1},};  //switch  CC#76



I'll work thru your last reply and try to get this learned. Believe I do try and search on the errors I get and such before replying back here. I'll get it.Im more the hardware side of things than software.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 06, 2020, 12:47 am
if it matters, I need to correct an earlier reply.

"I have 8 momentary switches each with an LED. I just want to show the state of the latched button.Plus I have the expression pedal,no led."

These are switches that do not have LED builtin, they are separate mounted LED's from the switches.
Title: Re: USB Midi Foot Controller Help
Post by: cameron206 on Aug 06, 2020, 01:28 am
FYI, I am really getting a lot out of this.
I never experimented with latchedCCbuttons... I had just been using actual latching switches :)

Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 06, 2020, 01:59 am
FYI, I am really getting a lot out of this.
I never experimented with latchedCCbuttons... I had just been using actual latching switches :)


Well glad Im some help.

Im still cant get the leds to work at all. Im thinking the code he made im not understanding at all about what to put where.
Buttons are pretty straight forward to me but not those LED's code.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 06, 2020, 07:21 am

You seem very confused, I'd recommend taking a step back and starting with a simple sketch.
Don't start throwing different components together until you understand how each component works separately.
Feel free to ask specific questions about the parts you don't understand.

I am so sorry. This reply you made got me thinking,so I went thru and read everything again from your 1st reply and realised I was mixing your code and Cameron's codes together.

I was looking thru and reading so many different things I didnt even notice what I did.


So this is where Im at Just to have all this mess I made cleared up. Buttons work as intended.
I will play with leds code some more to see if I can get it.
Code: [Select]
#include <Encoder.h>
#include <Control_Surface.h> // Include the Control Surface library
 
// Instantiate a MIDI over USB interface
USBMIDI_Interface midi;
 


// Instantiate an array of latched push buttons that send MIDI CC messages
// when pressed.
CCButtonLatched buttons[] {
  { 0, 0x10 },
  //  │   │    └──── Base MIDI CC controller number
  //  │   └───────── Button pin number
  //  └───────────── Bank that changes the controller number
  { 1, 0x11 },
  { 2, 0x12 },
  { 3, 0x13 },
  { 4, 0x14 },
  { 5, 0x15 },
  { 6, 0x16 },
  { 7, 0x17 },
};


// Setup for an Analog pot for an EXP pedal on cc #11
CCPotentiometer potentiometer = {
  A3, {MIDI_CC::Expression_Controller, CHANNEL_1}
};

void setup() {
  Control_Surface.begin();  // Initialize the Control Surface
}

void loop() {
  Control_Surface.loop();  // Update the Control Surface
}
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 06, 2020, 11:45 am
THANK YOU SO MUCH!

I finally was able to get all of the setup working,no errors,all buttons latching, and led's working with each switch press on/off.

I actually had the entire code correct after my 2nd attempt the other day but I kept getting this weird error.
It would complete the compile but everytime I would attempt upload I would get a long list of errors so I thought the code was wrong all along.

Then thinking that code was wrong it when I started trying all the different things and got extremely confused.

So I started from scratch,read your code and directions step by step very close. Hit compile= all good. Upload=ERRORS.

So I tried making sense of the errors and realised these were not code errors but some really weird looking stuff. Without touching the code I thought it thru, made sure it was set to correct port and it was.

Unplugged the USB cable from 1 slot,plugged into another and BAM, worked. Tested out good so far in my amp sim.
I cant believe that was causing me all my misery and confusion and I had it right all along...Geez.

So thank you and everyone else that helped me, Im sure yall were probably thinking this guy has no glue, maybe even getting tired of me...lol.

Final code
Code: [Select]
#include <Encoder.h>
#include <Control_Surface.h> // Include the Control Surface library
 
// Instantiate a MIDI over USB interface
USBMIDI_Interface midi;
 


// Instantiate an array of latched push buttons that send MIDI CC messages
// when pressed.
CCButtonLatched buttons[] {
  { 0, 0x10 },
  //  │   │    └──── Base MIDI CC controller number
  //  │   └───────── Button pin number
  //  └───────────── Bank that changes the controller number
  { 1, 0x11 },
  { 2, 0x12 },
  { 3, 0x13 },
  { 4, 0x14 },
  { 5, 0x15 },
  { 6, 0x16 },
  { 7, 0x17 },
};



// Setup for an Analog pot for an EXP pedal on cc #11
CCPotentiometer potentiometer = {
  A3, {MIDI_CC::Expression_Controller, CHANNEL_1}
};

// The array of pin numbers for LEDs that display the states of the buttons.
const pin_t ledPins[] = { 9, 10, 16, 14, 15, 18, 19, 20 };

// Get the length of an array
template <class T, size_t N> constexpr size_t length(T (&)[N]) { return N; }

static_assert(length(buttons) == length(ledPins),
              "Error: requires same number of buttons as LEDs");

void setup() {
  Control_Surface.begin();  // Initialize the Control Surface
  for (auto pin : ledPins)  // Set the pinMode to output for all LEDs
      pinMode(pin, OUTPUT);
}

void loop() {
  Control_Surface.loop();  // Update the Control Surface

  // Loop over all buttons and LEDs
  for (size_t i = 0; i < length(buttons); ++i)
      // Update the LED states to reflect the toggled switch states
      digitalWrite(ledPins[i], buttons[i].getState() ? HIGH : LOW);
}
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 06, 2020, 12:50 pm
Ok slight mishap.

Switch on pin 3, led 14 only work every other press.
Before I had the led's working the switch worked correct.

Any ideas what I missed?

 I have not changed any of the wiring,pins, or switches in any way. SO I assume its software code.
Title: Re: USB Midi Foot Controller Help
Post by: cameron206 on Aug 06, 2020, 08:13 pm
~just~ pin 3? The other buttons work correctly?
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 06, 2020, 08:45 pm
~just~ pin 3? The other buttons work correctly?
Yes.
If I swap pins around it will change which switch it will do it on....weird
Title: Re: USB Midi Foot Controller Help
Post by: cameron206 on Aug 06, 2020, 09:59 pm
I cant see any flaw that would make pin 3 different.
Are you hooked up to the DAW? How are you perceiving this error?

If in the DAW, it could be that the parameter wants On/Off to toggle.. so it needs both the 127 and the 0 to fire... thus two clicks?
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 06, 2020, 10:50 pm
I cant see any flaw that would make pin 3 different.
Are you hooked up to the DAW? How are you perceiving this error?

If in the DAW, it could be that the parameter wants On/Off to toggle.. so it needs both the 127 and the 0 to fire... thus two clicks?
So this is what I have found with testing along with my previous reply.

No software started,no daw,amp sims...nothing. That same switch on pin 3 and led on pin 14 only lights up every other push. Connect my stand alone amp sim(Amplitube 4) and same results. The software has a red indication light showing that a midi message is received or not,it alos only lights every other push.

SO I tried setting up my own commands in Amplitube4 using the learn function and selecting between latched On/Off setting= same results.
So I am assuming this happens all the time whether its connected to any software or not.

But, this was only the case after adding all the LED code in the last code I posted. Before the code was added the button functioned as all the other do, every push registers a light on the unit and in any software I tried it in.
All switches are momentary with a click when fully pressed. I keep this type of foot switches  around since I use them often for my main amp(Marshall) and FX pedalboard(Boss GT100) as repairs or as external switch boxes for the FX board I run.

Most all my testing has been done using the stand alone version of Amplitube 4 amp sim since it starts very fast and I really dont need to adjust any settings to check if button presses register.

This is the only reason I suspect code since it wasnt an issue until adding the LED code in. IF I delete that code back to normal functions, very strange.

Maybe its just me looking at this wrong, I troubleshoot things thinking whats the most logical cause and start running down the options from there. Thats not always the best way to troubleshoot either.

So yeah I have no clue at this point
Title: Re: USB Midi Foot Controller Help
Post by: PieterP on Aug 06, 2020, 11:31 pm
Ok slight mishap.

Switch on pin 3, led 14 only work every other press.
Before I had the led's working the switch worked correct.

Any ideas what I missed?

 I have not changed any of the wiring,pins, or switches in any way. SO I assume its software code.
I'm unable to reproduce this (using an Arduino Leonardo, which uses the same chip as the Pro Micro, because I don't own a Pro Micro).

Are you using the exact code you posted earlier? If not, could you post the code you're using?
Could you also try changing the pin numbers in software? Try the following, for example:
Code: [Select]
CCButtonLatched buttons[] {
  { 0, 0x10 },
  { 1, 0x11 },
  { 2, 0x12 },
  { 4, 0x13 },
  { 3, 0x14 },
  { 5, 0x15 },
  { 6, 0x16 },
  { 7, 0x17 },
};

(I swapped pins 3 and 4.)
Does the problem still affect pin 3 in this case, or does it affect pin 4?

What version of Control Surface are you using? Did you download the master version or v1.1.1?
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 06, 2020, 11:39 pm
I'm unable to reproduce this (using an Arduino Leonardo, which uses the same chip as the Pro Micro, because I don't own a Pro Micro).

Are you using the exact code you posted earlier? If not, could you post the code you're using?
Could you also try changing the pin numbers in software? Try the following, for example:
Code: [Select]
CCButtonLatched buttons[] {
  { 0, 0x10 },
  { 1, 0x11 },
  { 2, 0x12 },
  { 4, 0x13 },
  { 3, 0x14 },
  { 5, 0x15 },
  { 6, 0x16 },
  { 7, 0x17 },
};

(I swapped pins 3 and 4.)
Does the problem still affect pin 3 in this case, or does it affect pin 4?

What version of Control Surface are you using? Did you download the master version or v1.1.1?
I will give that try later when home.
I think i dl the madter version.

I thought maybe the switch was faulty or not momentary so i swapped it with known switch type and new,same result but ill try the software pin swap.

And yes exact same code i posted,last code posted in my thank you reply #46.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 06, 2020, 11:45 pm
Also other day when using camerons button code i had the same issue,i swapped pins in software and it correct the troubled button but moved it to another. I didnt mess with the code much and assumed i messed up code.
But now seems same with current code. Ill verify the swap later as you suggest.
Title: Re: USB Midi Foot Controller Help
Post by: cameron206 on Aug 07, 2020, 12:47 am
I wonder if there is some clock output on 3?
In the serial monitor or MIDI ox.... is the output clean?
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 07, 2020, 01:10 am
I wonder if there is some clock output on 3?
In the serial monitor or MIDI ox.... is the output clean?
I cant really answer.
Remember before this I never messed with anything midi so those terms are foreign to me.

Could you elaborate some more on the question and I'll do my best to answer?

Are you meaning in the software Im using? If so not sure how to check.
Sorry I'm a total noob here.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 07, 2020, 01:23 am
OK.
Switched the pins in code.
Same switch still worked the same. All it did was shift the led that lights with that switch over one LED.

I'll go thru that switch wiring and double check it all, again.
Title: Re: USB Midi Foot Controller Help
Post by: PieterP on Aug 07, 2020, 01:25 am
OK.
Switched the pins in code.
Same switch still worked the same. All it did was shift the led that lights with that switch over one LED.

I'll go thru that switch wiring and double check it all, again.
So it's still the switch connected to pin 3 that misbehaves? That's very strange, and is most likely a hardware issue.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 07, 2020, 01:49 am
So it's still the switch connected to pin 3 that misbehaves? That's very strange, and is most likely a hardware issue.
Correct.
Im thinking its hardware now. But what puzzles me is this,

What Im using for the controller is an old 4 switch amp foot controller for controlling onboard amp FX and such.
I gutted the electronics from it but kept the 4 original foot switches, then added 3 more to that housing plus a foot expression pedal with switch that connects with a cable and jack.

The switch causing the issue is one of the four originals, but I changed switch with a new one to rule it out but same results. All 8 switches wiring are identical to each other, plus the LED's.

I will be checking wiring again. Thinking possibly a small wire strand is touching somewhere causing this.
Title: Re: USB Midi Foot Controller Help
Post by: cameron206 on Aug 07, 2020, 03:45 am
ha ha I would bet a million dollars that switch is a latching switch. Many guitar amp channel switchers use latched.

Put a meter on it.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 07, 2020, 06:42 am
ha ha I would bet a million dollars that switch is a latching switch. Many guitar amp channel switchers use latched.

Put a meter on it.

I thought the same but this wasnt an analog pedal like the normal type. This type controlled onboard digital effects the amp had itself.
Now to be sure I tried a switch tonight from a batch of momentary switches I ordered for projects that required non latching. Tried 2 of them. No matter which one I tried same results.

I have gone thru the wiring more than I can count now with a 600 lumens headlamp and magnifying glass to see every connection best I can. It all looks good.

This is really frustrating, never had an issue like this with everything I have made thru the years using those foot switch types.

Tell me if I did something wrong with connections that Im overlooking maybe.

Basically 1 lug on each switch are all connected by a single GND wire,that same lug also has the led leed.
The opposite LED leed has the resistor that goes to Pro MIcro pins for all led's.

The 2nd lug of the switch then goes to each pin on the Pro Micro.

I have wired in a 5 pin DIN jack to be able to connect the external expression pedal and on/off switch to main unit with Arduino and switches.
POT= 1 pin GND,1 to 5v, and 1 to Pro Micro pin.
switch is wired same as the others.

1 LED to show power when USB connected. Of course 1 to 5v and other to GND with needed resistor wired correct.

Best of my knowledge that should be correct.
Title: Re: USB Midi Foot Controller Help
Post by: PieterP on Aug 07, 2020, 06:12 pm
Does the problem occur when you're using just one button?
Code: [Select]
#include <Control_Surface.h>
 
USBMIDI_Interface midi;

CCButtonLatched button = { 3, 0x13 };
const pin_t ledPin = 14;

void setup() {
  Control_Surface.begin();
  pinMode(ledPin, OUTPUT);
}

void loop() {
  Control_Surface.loop();
  digitalWrite(ledPin, button.getState() ? HIGH : LOW);
}
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 07, 2020, 06:44 pm
Does the problem occur when you're using just one button?
Code: [Select]
#include <Control_Surface.h>
 
USBMIDI_Interface midi;

CCButtonLatched button = { 3, 0x13 };
const pin_t ledPin = 14;

void setup() {
  Control_Surface.begin();
  pinMode(ledPin, OUTPUT);
}

void loop() {
  Control_Surface.loop();
  digitalWrite(ledPin, button.getState() ? HIGH : LOW);
}


I'll give that a try.
Gonna go get a new test meter today before I do much more, mine decided to quit on me last night
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 07, 2020, 07:51 pm
Found the issue.

Ok so the original 4 switches that I mentioned from the old pedal I used, 3 are momentary and 1 was latching. Why this was done I have no clue but all 4 are the original installed by Marshall.

No for the other part that stumped me. Couple months ago I ordered 20pc momentary foot switches, each sealed individually and all 20 labeled "MOMENTARY". I used most of them rebuilding and replacing a previous pedal board project all of them had proper function.

Now all the ones I had left still sealed and labeled from seller said momentary but they are not. SPST Latching is what they really are and if my test meter wasnt acting up without my knowing before it quit altogether I would have known that from the start.

This lil project has been one of the most pains Ive had to deal with in a long time. 1st its me getting confused with the code because of a bug/glitch happening with the IDE,then all my confusion and really messing it up to wrong mislabeled switches and finally a test meter going faulty until it just quit....Just WOW...lol.

Anyways had to order more switches and I think its all worked out and proper function.

But yall may hear from me again soon since I know how this is working, I'm think I wanna add one of the port expansion cards to give me a few more pins. I wanna add a side plug in pedal that will use 3 pedals w/led to control my looper in the same amp sim software.

Just 2 questions.
What Multiplexer card would I need? I looked up the one Grumpy mentioned on the 1st page searching by the chip model.
Should I need a specific layout on the card or just any as long as its running that chip?
I usually use Amazon U.S. for that stuff.

Any example code I can get that shows how to add that to mine? I dont need a code for looper just for adding the PIN expansion card.

I dont think it will be as hard for me to figure out since I have better understanding how the code works since I really had it correct the 2nd time I tried but the software was bugged making me think I messed up.

Thanks again everyone, I learned quiet a bit thru all this.
Title: Re: USB Midi Foot Controller Help
Post by: PieterP on Aug 08, 2020, 01:43 pm
Glad to hear you found it!

Personally I use 74HC4067 16-channel multiplexers for most of my projects. I got them from eBay, on a small breakout board (less than $1 each, IIRC). The 74HC4051 is similar, it's an 8-channel multiplexer if you don't need 16 channels. Multiplexers are very simple devices, so it doesn't really matter which one you buy.

Multiplexers are supported out of the box using Control Surface, just declare the multiplexer, and use their pins (mux.pin(n)) instead of the usual pin numbers (A0 etc.) when declaring the potentiometers, buttons, or other inputs.
See this getting started guide (https://tttapa.github.io/Control-Surface-doc/Doxygen/d5/d7d/md_pages_Getting-Started.html#first-output-extio).
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 08, 2020, 06:07 pm
Glad to hear you found it!

Personally I use 74HC4067 16-channel multiplexers for most of my projects. I got them from eBay, on a small breakout board (less than $1 each, IIRC). The 74HC4051 is similar, it's an 8-channel multiplexer if you don't need 16 channels. Multiplexers are very simple devices, so it doesn't really matter which one you buy.

Multiplexers are supported out of the box using Control Surface, just declare the multiplexer, and use their pins (mux.pin(n)) instead of the usual pin numbers (A0 etc.) when declaring the potentiometers, buttons, or other inputs.
See this getting started guide (https://tttapa.github.io/Control-Surface-doc/Doxygen/d5/d7d/md_pages_Getting-Started.html#first-output-extio).
Thanks.
That sounds easy then.
Title: Re: USB Midi Foot Controller Help
Post by: cameron206 on Aug 08, 2020, 06:10 pm
Quote
Ok so the original 4 switches that I mentioned from the old pedal I used, 3 are momentary and 1 was latching. Why this was done I have no clue but all 4 are the original installed by Marshall.
ha ha where is my $1M
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 08, 2020, 06:13 pm
ha ha where is my $1M
Your gonna be waiting a loooooong time for that....lol
Title: Re: USB Midi Foot Controller Help
Post by: chrisplaysmusic on Aug 14, 2020, 05:40 am
Found the issue.

Ok so the original 4 switches that I mentioned from the old pedal I used, 3 are momentary and 1 was latching. Why this was done I have no clue but all 4 are the original installed by Marshall.
This is a cool project. I was looking for something similar. Would love to see some photos/video once you have everything done.

-Chris
https://www.guitarlobby.com/best-midi-keyboards/ (https://www.guitarlobby.com/best-midi-keyboards/)
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 14, 2020, 06:10 am
This is a cool project. I was looking for something similar. Would love to see some photos/video once you have everything done.

-Chris
https://www.guitarlobby.com/best-midi-keyboards/ (https://www.guitarlobby.com/best-midi-keyboards/)
I'll get ya some pics.
Its basically done, just a few tweaks Im making here and there as I go.
So far its working pretty good.

Only 1 downside with it so far.
Amplitube 4 doesnt send midi out to devices so if you control anything with mouse and doesnt change the state of the leds on the controller which really isnt a problem for me. I just have to engage all the switches to light up before I start Amplitube then its fine.

I just used old pedals that were no use to me anymore to work. I already had everything on hand so it was a cheap project.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 14, 2020, 06:25 am

This is a cool project. I was looking for something similar. Would love to see some photos/video once you have everything done.

-Chris
https://www.guitarlobby.com/best-midi-keyboards/ (https://www.guitarlobby.com/best-midi-keyboards/)
(https://imgur.com/U7BZ4PV)


Housings used:
Old Marshall AVT foot controller
Behringer FS112
Zoom vol/exp pedal

The board was just plywood with black vinyl on top. The aluminum was U channel and cut to 45 degree angles at corners.
Gutted all original electronics except switches.

The exp and FS112 were connected by using 5 pin DIN jacks and I made the patch cables.
The Behringer pedal with the red and green leds are for controller my DAW(Reaper). Green=play and red= record.

So far all works great. I think I need to change the exp pedal pot to a 10k,its 100k right now.
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 14, 2020, 06:33 am
Hmm didnt post the pic for some reason

I also repainted the Marshall box since the original finish was really bad off.


Try again

(http://i.imgur.com/U7BZ4PV.jpg) (https://imgur.com/U7BZ4PV)
Title: Re: USB Midi Foot Controller Help
Post by: NervusTwitch on Aug 16, 2020, 01:13 am
Glad to hear you found it!

Personally I use 74HC4067 16-channel multiplexers for most of my projects. I got them from eBay, on a small breakout board (less than $1 each, IIRC). The 74HC4051 is similar, it's an 8-channel multiplexer if you don't need 16 channels. Multiplexers are very simple devices, so it doesn't really matter which one you buy.

Multiplexers are supported out of the box using Control Surface, just declare the multiplexer, and use their pins (mux.pin(n)) instead of the usual pin numbers (A0 etc.) when declaring the potentiometers, buttons, or other inputs.
See this getting started guide (https://tttapa.github.io/Control-Surface-doc/Doxygen/d5/d7d/md_pages_Getting-Started.html#first-output-extio).
I have a quick question about the code im using, the one you gave in examples.

Its using latched buttons with the LED's indicating the state. I wanted to try using non-latching so in the code CCbuttonslatched, I just removed the latched command but that gave errors.

So how do I keep the function of the led's but with non-latching?
Trying a new DAW and there seems no setting to make it latched on or off so I have to double click everything.
Id like to try without latching to know if I want to use the controller or not in DAW since my amp sim allows me to set latched or not in the settings.
Title: Re: USB Midi Foot Controller Help
Post by: PieterP on Aug 18, 2020, 10:49 am
Its using latched buttons with the LED's indicating the state. I wanted to try using non-latching so in the code CCbuttonslatched, I just removed the latched command but that gave errors.
The CCButton class doesn't have a getState method (because it doesn't have a latched state). It does have a getButtonState method. If it returns Button::Pressed, turn on the LED.