DIYstompboxes.com

DIY Stompboxes => Digital & DSP => Topic started by: potul on November 04, 2015, 01:21:57 PM

Title: Arduino MIDI Switcher
Post by: potul on November 04, 2015, 01:21:57 PM
Hi Everyone,

Long time no post in the forum, but recently I finished a little project that I think can be interesting for someone, so I've decided to post it here.
It's basically a MIDI switcher for an amp. So, a device that can receive MIDI messages (ie, from your MIDI foot controller) and control your amp channel, reverb or other characteristics that can be changed via the normal footswitch.

It's really something I don't need now, because I only use my amp (Peavey Bandit) as a power amp and I feed it with my POD, but one friend was in need of one and I decided to give it a try.
In addition, I just had in my drawer a recently purchased Aruino nano asking for a project...

The device

Device features:

-DC Jack compatible with BOSS PS
-MIDI IN. This is where we will receive the messages. I have a placeholder for MIDI OUT as well, but I didn't implemente it yet (my thought is to do a MIDI through, just relaying what comes in)
-Stereo jack.  In my case, the Bandit uses a stereo jack to control 2 things, amp channel and booter. You will need to adapt this to your amp needs.
-Push button. Used to configure presets and save them.

How does it work?:

The Arduino stores in Non Volatile Memory all the presets, one for each MIDI Program.
In each preset, you can configure what relay is on/off of the two. These will control your amp.
When a Program Change MIDI message is received, it activates/deactivates the appropriate relays based on the presets stored.
In order to change a preset, first you need to send the PC MIDI message, then push the button multiple times until you reach your desired setup. Every time you push, the device cycles through all combinations. When you are happy with it, you need to push for a long period (more than 1 second), and it will be stored and ready for next time you send the same PC MIDI message.

Tecnical details:

The MIDI switcher has 2 main parts:

-Arduino nano: All the Arduion power in a small package.
(http://i471.photobucket.com/albums/rr74/mateu_ros/placa_r3_nano_atmega328p_compatible_arduino_290_290.jpg)
I bought it here, really cheap: http://www.banggood.com/ATmega328P-Nano-V3-Controller-Board-Compatible-Arduino-p-940937.html (http://www.banggood.com/ATmega328P-Nano-V3-Controller-Board-Compatible-Arduino-p-940937.html)

-Relay module: It has 2 relays. We can connect them as Normally Closed or Normally Open, depending on your needs.
(http://i471.photobucket.com/albums/rr74/mateu_ros/_12.jpg)
I bought this one from ebay, but you can get similar ones from multiple places: http://www.ebay.es/itm/MODULO-RELAY-RELE-5V-DE-2-CANALES-PARA-ARDUINO-ARM-PIC-AVR-DSP-RASPBERRY-PI-/261475911035? (http://www.ebay.es/itm/MODULO-RELAY-RELE-5V-DE-2-CANALES-PARA-ARDUINO-ARM-PIC-AVR-DSP-RASPBERRY-PI-/261475911035?)

In order to put the small amount of external components needed for the prouject, I used a perfboard PCB I had laying aroung.
Here you have the design:
(http://i471.photobucket.com/albums/rr74/mateu_ros/midiswitch.jpg)

As you can see... small part count. Only 3 resistors and one diode, and one optocoupler. BTW, the diode is the one in vertical, with the cathode in pin 2 of the opto.
 
Connections:

SW1: Momentary Push button, normally open. If you have a differnt type of button, you will need to adapt the software. The pins is configured with and internal pullup, so no extra resistor is required.
SW GND: This one goes to the other pin of the pushbutton.
RELAY1 & RELAY2: these will go to the relay module In1 and In2.
FOOT1 & FOOT&: For future use. Explained later.
MIDI IN 4 & 5: These go to the MIDI IN jack.
MIDI OUT 2,4,5: You can skip these, only for future use.
9v(BAT): Power in from DC jack. It can be up to 12v
GND(BAT): Ground from DC jack.
RELAY PWR: Relay power (5v). It gets connected to Vin
RELAY GND: Relay module ground.

You will need to adapt the way you connect the relay to the amp, as this depends on the amp. Look for the footswitch schematic, and try to replicate it with the relays.
In my case (Peavey Bandit) it goes like this:

Sleeve: Common, it goes to the central pin of each relay.
Ring: To one of the outer pins of Relay 1.
Tip: To one of the outer pins of Relay 2.

Here you have it in a sweet box.... whenever I have time I will move it to a better enclosure. But this one works great :)

(http://i471.photobucket.com/albums/rr74/mateu_ros/IMG_20151027_173459718_HDR.jpg)

Guts shot:

(http://i471.photobucket.com/albums/rr74/mateu_ros/IMG_20151027_173520149_HDR.jpg)


Software

And, here you have the code. You will need:

-Mini USB cable
-Arduino IDE. you can get it from  www.arduino.cc
-Depending on the arduino you buy, you might need an extyra driver. Check with the vendor.
-Extra libraries you will need to install:
Onebutton : To control events based on button pushes.   http://www.mathertel.de/Arduino/OneButtonLibrary.aspx (http://www.mathertel.de/Arduino/OneButtonLibrary.aspx)
MIDI Library: To send/receive MIDI messages. http://playground.arduino.cc/Main/MIDILibrary (http://playground.arduino.cc/Main/MIDILibrary)

Code: [Select]
#include <OneButton.h>
#include <EEPROM.h>
#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
#define LED1 11 //set pin for Relay1
#define LED2 10 //set pin for Relay2
#define SW1 12  //set pin for button
MIDI_CREATE_DEFAULT_INSTANCE();

byte curr_program;  //current PC value

// Setup a new OneButton on pin 11. 
OneButton button1(SW1, true);

// -----------------------------------------------------------------------------
// This function will be automatically called when a NoteOn is received.
// It must be a void-returning function with the correct parameters,
// see documentation here:
// http://arduinomidilib.fortyseveneffects.com/a00022.html
void handleNoteOn(byte channel, byte pitch, byte velocity)
{
    // Do whatever you want when a note is pressed.
    // Try to keep your callbacks short (no delays ect)
    // otherwise it would slow down the loop() and have a bad impact
    // on real-time performance.
}

void handleNoteOff(byte channel, byte pitch, byte velocity)
{
    // Do something when the note is released.
    // Note that NoteOn messages with 0 velocity are interpreted as NoteOffs.
}

void handleProgramChange(byte channel, byte number)
{
  //do something when receiving PC messages
  byte value;
  curr_program=number; //stores the PC number for later
  //read preset value from EEPROM
  value=EEPROM.read(number);
  //activate relay 1 if needed
  if (getBit(value,1))
   {
     digitalWrite(LED1,HIGH);
   }
   else
   {
      digitalWrite(LED1,LOW);   
   }
  //activate relay 2 if needed
  if (getBit(value,2))
   {
     digitalWrite(LED2,HIGH);
   }
   else
   {
      digitalWrite(LED2,LOW);   
   }

}

// This function will be called when the button1 was pressed 1 time.
void click1() {
  //toggle Relay1
  byte value;
  value=digitalRead(LED1)+digitalRead(LED2)*2;
  value++;
    if (getBit(value,1))
   {
     digitalWrite(LED1,HIGH);
   }
   else
   {
      digitalWrite(LED1,LOW);   
   }
  //activate relay 2 if needed
  if (getBit(value,2))
   {
     digitalWrite(LED2,HIGH);
   }
   else
   {
      digitalWrite(LED2,LOW);   
   }
} // click1


// This function will be called when the button1 was pressed 2 times in a short timeframe.
void doubleclick1() {
} // doubleclick1


// This function will be called once, when the button1 is pressed for a long time.
void longPressStart1() {
  //save preset
  byte value=0;
  if (digitalRead(LED1))
  {
    setBit(value,1);
  }
  if (digitalRead(LED2))
  {
    setBit(value,2);
  }
  EEPROM.write(curr_program,value);
 
} // longPressStart1


// This function will be called often, while the button1 is pressed for a long time.
void longPress1() {
  //Serial.println("Button 1 longPress...");
} // longPress1


// This function will be called once, when the button1 is released after beeing pressed for a long time.
void longPressStop1() {
  //digitalWrite(LED2,LOW);
} // longPressStop1


// -----------------------------------------------------------------------------
void setup()
{
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
    pinMode(SW1, INPUT_PULLUP);
    //write inital presets, only for debugging.
    for (int i=0;i<128;i++)
    {
      //EEPROM.write(i,i);
    }
   
    //Define button handling functions
    button1.attachClick(click1);
    button1.attachDoubleClick(doubleclick1);
    button1.attachLongPressStart(longPressStart1);
    button1.attachLongPressStop(longPressStop1);
    button1.attachDuringLongPress(longPress1);
    // Connect the handleNoteOn function to the library,
    // so it is called upon reception of a NoteOn.
    MIDI.setHandleNoteOn(handleNoteOn);  // Put only the name of the function
    // Do the same for NoteOffs
    MIDI.setHandleNoteOff(handleNoteOff);
    // And for PC message
    MIDI.setHandleProgramChange(handleProgramChange);
    // Initiate MIDI communications, listen to all channels
    MIDI.begin(MIDI_CHANNEL_OMNI);
    //Serial.begin(9600); //Set serial to 9600, only for debugging
   
}
void loop()
{
    // Call MIDI.read the fastest you can for real-time performance.
    MIDI.read();
    // There is no need to check if there are messages incoming
    // if they are bound to a Callback function.
    // The attached method will be called automatically
    // when the corresponding message has been received.

    // Call button tick to check button status
    button1.tick();
}

byte setBit(byte &store, byte bitn) { //bit 1 is right-most
      store |= (1 << (bitn - 1)); //set bit 5 to '1'.
}

byte clearBit(byte &store, byte bitn) {
      store &= !(1 << (bitn - 1));
}
 
bool getBit(byte store, byte bitn) {
      byte b = (1 << (bitn - 1));
      return (store & b);
}

Pending topics:

There are some improvements pending. When I have the time I will implement them:

1- Take the LEDs outside of the box. When I have the definitive box, I will simply remove the red LED from the module, and plug in 2 new LEDs that will be visible from the outside.
2- MIDI through: The ideas is to conncet the MIDI OUT and modify the software to simply output everything that comes from MIDI IN. This way you can daisy chain it with another MIDI device you need to control
3- Footswitch IN: I have left 2 FOOT1 and FOOT2 connections reserved. The ideas is to connect the current amp footswitch. With this and a routine to detect rising/falling edges, I plan to be able to  still use the footwitch at the same time as the MIDI Switch.
4- Find a catchy name for it. :)

I hope this will be of interest for someone. If you plan to build one and need help, don't hesitate to contact me.

Have a nice DIY day!

Mat
Title: Re: Arduino MIDI Switcher
Post by: LC_legend on November 26, 2015, 10:56:44 AM
Wow! Congratulations on the project and detailed write-up!  Im looking for something like this but with a little twist.

I have the same setup as you, pod hd500 to poweramp to cab. But im allways wanting more from the tone (dont we all?) I get from the pods amp models. A while ago I built a dr. boogie wich i really like and im having great fun plugging it in the loop of the hd500. I also have a Harley Benton (or Joyo) american sound wich i really like with the same setup (these are based on tech21 charater series). Both of them sound and feel analog and IMHO sound more pleasant to the hear at any given volume. Im also planning to build two more pedals to work as preamps (azabache and humble from ROG)

This got me thinking: what if i could send a ProgramChange from the hd500 to an arduino module that would store presets and change these pedals On and Off? They wouldnt even have to be in the form of pedals. They could be in a single enclosure and work as channels on a head.

Your project looks perfect for this, but im wondering if it could have at least two more channels... at least :icon_twisted:
Also, could it store presets? When we change presets on the hd500 it sends PC message over midi (up to 64, i think). If the arduino could match the same preset would be GRWEAT.

what do you think? is it possible?
 
Title: Re: Arduino MIDI Switcher
Post by: potul on November 26, 2015, 12:35:33 PM
Hi

In fact, the project  as it is with some modifications can be adapted to your needs esasily.

-Presets: This functionality is already there. The on/of combinations (for channel/reverb, etc...) are stored in the arduino. The POD only sends PC messages.

-Control loops intead of amp channel: In order to do this, you only would need to change the type of relays. My project is using SPDT relays, while you would need DPDT. In the arduino world these are usually called "signal relays"

-More channels: If you need more than 2 channels, some things need to be done:

1-Adapt the software to deal with more outputs. A change in the logic to store presets is required as well.
2-You will probably need another type of "programming" interface... now with only one button works well for 2 channels (only 4 combinations), but if you double this, you go to 16 combinations. I would probably have one button per channel.

BTW, the project you described is also one I've had in my head for a long time.... If you want to go on with it let me know and I can try to help you as much as possible. I can do the programming for you, as I'm familiar with the software.

Mat

If you
Title: Re: Arduino MIDI Switcher
Post by: andersm on May 25, 2016, 01:17:12 AM
Brilliant, I have been looking for this for ages!
Since I'm a bloody noob in dyi midi things, please forgive me if I ask silly questions.
Basically, I play a Laney Ironheart head, which has a 4 switch stompbox via midi cable (although the amp has no actual midi), but alternatively can be toggled by 2 foot switches with 2 buttons each (one is lead vs clean and clean vs crunch, the other toggles reverb and boost, through a stereo 1/4" jack). I only really need to toggle clean vs lead, given the music I play.
So I do get this right? with this and my trusty old rfx midi buddyI could simultaneously switch patches on my multi fx and toggle amp channels, right?
I may need some guidance but this is precisely what I was looking for.
Title: Re: Arduino MIDI Switcher
Post by: potul on May 25, 2016, 02:11:19 AM
Yes, that's the idea. Being able to change your amp channel through midi.

A couple of things to consider:

-If you want to control your FX and the midi switcher at the same time you need to send the same program change to both devices. Does your FX unit have a "MIDI through"?

-You need to check that your amp is not using some sort of digital control. Is the jack footswitch simply a couple of  switches and LED? or is there some electronics in?

Mat
Title: Re: Arduino MIDI Switcher
Post by: potul on May 25, 2016, 02:21:14 AM
I just checked your amp.. if I'm not mistaken it can use the FS2 footswitch that is completely passive, so you should be ablt to control it.
Title: Re: Arduino MIDI Switcher
Post by: andersm on May 25, 2016, 04:42:27 AM
Yes, the manual specifically suggests those passive switches and there's a wiring diagram on the rear panel of the amp.
I need to dig out my old midi controller but if I remember right it has a midi through. also the fx unit I was gonna buy has a midi out, would that work too?
Title: Re: Arduino MIDI Switcher
Post by: potul on May 25, 2016, 11:12:38 AM
Great,

regarding your FX unit, it depends on what the midi out does. If it just sends PC message when you press buttons in it... it might not work. If it relays all incoming messages, or send PC messages every time the unit changes patch, it will work.

what FX unit is it? Maybe I can dig into the MID specs
Title: Re: Arduino MIDI Switcher
Post by: andersm on May 25, 2016, 01:21:19 PM
I haven't bought it yet, but I would love to get the Line6 M5. Mostly because it's small, affordable and yet has midi.
I had another idea but this may make things more complicated. I need the M5 mostly for modulating and time based effects and therefore put it in the fx loop of the amp head. now, since the distortions of the M5 are not en par with the rest of the effects and I like the overdrive of my Laney best anyway - after all I chose that amp for a reason - I might still need a couple of stomp boxes before the preamp (noise gate and my electro harmonix metal muff, in cases when I have to play someone else's amp).

So what I basically need for that is a way to bypass a pre-fx loop via midi. Do I make any sense?
I could use a simple A/B switch box for that, but that means tapdancing again. Would be cool if I could just build an additional A/B switch in the same enclosure with the rest of the hardware.

by the way, I double checked, the floor board has a midi through, they just call it "share" for some reason but the manual says it's a through
Title: Re: Arduino MIDI Switcher
Post by: fwbulldog on May 26, 2016, 01:14:38 PM
Thanks for posting this!  It's a huge head start.  I currently use a GCX Loop Switch to control my Blackstar Ht60.  It requires three momentary switches to select clean, OD1, OD2.  In my current setup this takes up three loops from my GCX.  I'd like to get these loops back for fx, and I'm looking for a cheaper alternative to the RJM Mini Amp Gizmo ($229 + cable). 

Your layout is fantastic and very easy to follow.  At first I did not understand the need for the 6N138 Optocoupler.  I did some research, and I'd like to pass on what I learned.

There is a great page that explains MIDI signaling, even down to the HW interface.  https://learn.sparkfun.com/tutorials/midi-tutorial/all.  Some people that come here might have a high level understanding of what MIDI does, but need some more background on how the signaling works if they're going to build this circuit.

You don't want to directly (i.e. electrically) connect an external voltage driver to your Arduino GPIO.  Voltage spikes (or current sinks) could damage the chip or cause ground loops, noise, etc.  Since MIDI uses a 5V circuit to transmit data, you need to isolate the signal from the UART on the Arduino.  This is accomplished with the 61N38 Optocoupler that potpul has selected.  This chip isolates the incoming 5V MIDI signal by using a LED/phototransitor circuit.  There is no electrical connection between the MIDI input and output.  Its optically isolated. 

The following schematic (from the MIDI spec), shows the diode and optocoupler.  The diode (D1) is connected between MIDI pins 4 and 5 to form the current loop.  Note that the direction of the diode is important.  The diode is there to prevent current from flowing the wrong direction.

(http://i451.photobucket.com/albums/qq235/kurt_shetler/midihw_zpsiorlu9sg.gif)


Midi use connector pins 4 and 5 to create a 5V loop.  At the transmitter,Pin 4 is pulled up to 5V through a weak pull-up resistor.  Pin 5 is connected to the MIDI device transmit UART.   When the MIDI device wants to send traffic it outputs a logical 1 or 0 on the UART TX pin.  The website explains it very well.


(http://i451.photobucket.com/albums/qq235/kurt_shetler/signaling-one_zpsrnpi7pqd.png)


At the output, pin 4 is pulled high through a small resistor, and pin 5 is the buffered UART transmit line. On the input, these lines are tied to the anode and cathode of the LED in the opto-isolator. Since the UART output is high when not transmitting, both pins 4 and 5 will be at the same voltage, no current flows through the LED, thus it is not illuminated. When the LED is dark, the phototransistor is off, and the UART receives the voltage through the pullup, resulting in a logic one.
When the UART starts transmitting a byte, the start bit will pull pin 5 low, and current flows through the LED, illuminating it. When the LED is on, the phototransistor turns on, swamping the pullup, pulling the UART input to ground, and signaling a zero.

You can read about the MIDI out and through circuits there as well.   MIDI was designed to be a low-cost signaling interface, and its pretty simple to implement if you understand what the reference circuit does.  Im fine with this project being the end of my MIDI chain, so I dont really care about out and through. 

Since I need to switch three channels Ill have to use a four channel relay (http://www.banggood.com/3Pcs-5V-4-Channel-Relay-Module-For-Arduino-PIC-ARM-DSP-AVR-MSP430-Blue-p-983480.html).  Im a little concerned about the 5V current draw of all four relays at the same time, so I may need to also build a 5V regulator circuit for separately powering the relays (as opposed to taking the 5V output from the Adruino board).  Ill be powering the box using my Voodoo Labs Pedal Power Iso 5.  No batteries involved. 

Ill probably have to experiment with the code to see what the minimum momentary pulse time is for the Blackstar and tweak the code for three outputs instead of two. 

Very cool project!  Thanks again. 
Title: Re: Arduino MIDI Switcher
Post by: potul on May 31, 2016, 01:57:40 AM
So what I basically need for that is a way to bypass a pre-fx loop via midi. Do I make any sense?
I could use a simple A/B switch box for that, but that means tapdancing again. Would be cool if I could just build an additional A/B switch in the same enclosure with the rest of the hardware.
you can do it, but if you want to do it properly you will need a different type of relay board. The one I posted is a simple spdt, for bypassing you should use dpdt. Still.. you could do it with an spdt if you are not concerned about true bypass.
by the way, I double checked, the floor board has a midi through, they just call it "share" for some reason but the manual says it's a through
I don't think this would work. you need to pass the same midi messages to 2 different devices. This is usually done by using the midi through to daisy chain them. I don't think the midi through of the floor will output the messages generated by itself. You might need to look into the manual to clarify.
The ideal would be if the FX unit has a midi through. If not, we can implement a midi through from the arduino. It should not be complicated.
Title: Re: Arduino MIDI Switcher
Post by: potul on May 31, 2016, 02:08:44 AM
Since I need to switch three channels Ill have to use a four channel relay (http://www.banggood.com/3Pcs-5V-4-Channel-Relay-Module-For-Arduino-PIC-ARM-DSP-AVR-MSP430-Blue-p-983480.html).  Im a little concerned about the 5V current draw of all four relays at the same time, so I may need to also build a 5V regulator circuit for separately powering the relays (as opposed to taking the 5V output from the Adruino board).  Ill be powering the box using my Voodoo Labs Pedal Power Iso 5.  No batteries involved. 
you might want to check with both the relays and arduino spec sheet. 4 relays will draw something like 200mA. I don't recall how much the arduino regulator can handle. But I've had some bad experience in the past with relays and regulators, so having an extra one will not hurt.
Ill probably have to experiment with the code to see what the minimum momentary pulse time is for the Blackstar and tweak the code for three outputs instead of two. 

Very cool project!  Thanks again.
If you need more relays you might want to change the user interface. I decided to go cheap and use a single button, which works for 2 relays (ony 4 possible combinations). For 3 relays, that would be 8 combinations... you might want to consider a different way to manage it. Maybe adding 2 more buttons, one for each relay.
Title: Re: Arduino MIDI Switcher
Post by: andersm on May 31, 2016, 11:18:25 AM
you can do it, but if you want to do it properly you will need a different type of relay board. The one I posted is a simple spdt, for bypassing you should use dpdt. Still.. you could do it with an spdt if you are not concerned about true bypass.

Hm okay, I have no experience on whether true bypass would really make a sound difference here.
You're of course right about the midi through of the board. Isn't the midi out of the fx unit meant to daisychain units though?

Title: Re: Arduino MIDI Switcher
Post by: potul on May 31, 2016, 05:41:54 PM
yes if your FX unit has a midi out it might work.
Title: Re: Arduino MIDI Switcher
Post by: fwbulldog on June 01, 2016, 05:22:18 PM

If you need more relays you might want to change the user interface. I decided to go cheap and use a single button, which works for 2 relays (ony 4 possible combinations). For 3 relays, that would be 8 combinations... you might want to consider a different way to manage it. Maybe adding 2 more buttons, one for each relay.

I've been thinking about it, and I don't think I need any buttons.  For my application, I need three program changes that momentarily grounds a relay.  Only one will ever be active at a time.  I don't need eight combinations, just three.   And since only one relay will ever be active at once, I think I can get away with just using the 5V output from the Arduino, same as you did. 

Some slight code changes from your application, shouldn't be too hard.

Title: Re: Arduino MIDI Switcher
Post by: potul on June 02, 2016, 01:50:01 AM
up to you.

But if you don't have any button you will need to "hardcode" your presets. I would go for one button, and you cycle through your 3 channels when pushing it.

Remember to modify the code to make it momentary.

Title: Re: Arduino MIDI Switcher
Post by: andersm on June 13, 2016, 10:28:52 PM
Okay, so while I'm waiting for the rest of my hardware to arrive, I came up with a way to organise the fx loops.
Since I found a relay board with 8 channels and can therefore use 2 switchable fx loops I brainstormed a bit.
The easiest way would have been to just hook up the 2 loops in line and use them for pre fx like noise gate and distortion (handy, as I always wanted an "automatic" noise gate that disappears whenever I play solo or clean sounds...
But then I thought about using one of the loops for an additional post fx unit that doesn't otherwise respond to midi, which means I would have to keep the signal paths discrete. Luckily, one way of achieving this includes a true bypass option. Which is something I wanted to have anyhow...

Basically what I'm doing is this: I put a manual (how is a FOOT switch ever manual?!) foot switch before each of the relays, which acts as a true bypass. the one after the first loop is a 4PDT that does the signal routing for me so pre and post signals don't mix up.

Now there are different scenarios in which I can use it.
Loop A will only work for PRE fx. I will probably put my noise gate here. I will then not true-bypass it and only use the relay-bypass for clean and solo sounds.

Loop B can now be used either for another PRE fx, like a Wah or be true-bypassed, as I don't like using the Wah in my metal band anyway ;)
If I true-bypass it though, and put a POST fx unit in Loop B, the unit will be active for the post fx loop that so far cointains only my Line6. So basically, if you put any fx unit in Loop B, the true-bypass button will actually let you route either the pre signal or the post signal through that Loop. I admit, it makes everything a tad complicated but it could lead to an intresting use of effects.

The downsides is: if you're not going to use Loop B at all, the only way to maintain true bypass for both pre and post signal would be to put a patch cable in the loop (or leave the post fx unit out of this box alltogether). I can live with that though, because for my use, it's not a question of needing to change that setup during a set, but rather whether I want to use them in principleor not; so I can prepare the routing before a set.

So, here's my diagram, a bit on the wonky side, but hey, I'm a dabbler in that. If you find any flaws, please do point them out.
I haven't wired in any LEDs here, yet, but they will be vital. Oh and I haven't bothered drawing in the obvious grounds connections.
I added a version with colour coded signal paths, too. If it doesn't work, by amazing coincidnce you can also use it as a tube map in London. Obviously for guitarists, the tube sounds better than the metro  :icon_mrgreen:

https://dl.dropboxusercontent.com/u/23918855/FX%20loop.jpg
https://dl.dropboxusercontent.com/u/23918855/FX%20loop%20plus%20colour%20coded%20signal%20paths.jpg

Title: Re: Arduino MIDI Switcher
Post by: potul on June 14, 2016, 06:31:58 AM
I must admit you lost me.   ;D

I will review your diagrams tonight and will give you my feedback.

Mat
Title: Re: Arduino MIDI Switcher
Post by: potul on June 14, 2016, 08:30:54 AM
sorry but I don't undestand  the concept.

where would you connect POST FX IN and POSTFX OUT?
OUTPUT goes to the amp input, or to the power amp input (usually the return of the amp)?
loop B... is this going to be used before or after the amp pre-amp?
What is the purpose of the 4PDT?

Sorry to ask, but I want to understand what you try to achieve.

In addition,... you mentioned you have 8 relays.... but you are only using 2 in the schem. (you need 4 for the amp, right?, so you have 2 spare ones)

Title: Re: Arduino MIDI Switcher
Post by: potul on June 14, 2016, 08:52:41 AM
Ok,... I think I got it... your coloured one helped... Please correct me if I'm wrong.

-You want 2 controllable independend loops via MIDI (A and B). You will put some FX there.
-You want to choose if LOOP B goes before the AMP, or in the FX loop of the amp.
-You added an extra swithc for truebypass of LOOPA.
-OUTPUT will go to the AMP INPUT (where you usually place your guitar)
-POST FX OUT goes to AMP RETURN
-POST FX IN goes to AMP SEND

If this is it, I think you overcomplicated... :). Or maybe not... and the issue is that I don't picture it.

If I'm not wrong, you can do the same with a 3PDT (or a 4PDT if you want LED)

Question: Do you need to switch on the fly the LOOP B from PRE to POST? Or is this something you could simply wire differently upfront before the show?



Title: Re: Arduino MIDI Switcher
Post by: andersm on June 14, 2016, 11:46:08 AM
Sorry for making it so overcomplicated  :D
But yes, you got everything right. The 4PDT was necessary to keep the signal paths discrete and not to create any short circuits.
I had indeed forgotten that there were relays left, maybe I could use thm to contol the bubble machine and the flame thrower on stage XD

As for the question about the use of Loop B, I guess it depends on what effect I put there. I don't think I would need to switch this on the fly, really. But then again, this way I could if need be. And some effects work both ways but sound differently.
But I guess if I can fit an enclosure into my floorboard that's big enough for all the switches I might just give the post fx their own loop, while I'm at it.
Title: Re: Arduino MIDI Switcher
Post by: potul on June 15, 2016, 02:23:18 AM
So here it goes, my 2 cents....

If you don't need to move loop B from pre to post in the middle of a show, I would skip completely the 4PDT (I will explain later). If it's needed, it can be done with a 3PDT using the FX juggler schematic from Geofex.

Option 1: No relocation of loop B on the fly

-Wire both Loop A and Loop B as true bypass using 2 relays each.
-You then don't need the additional true bypass for loop A. If you still want to control independently Loop A, you can add a button and control it via the Arduino.
-Instead of sending the output of loop A to the input of Loop B, just send the output of Loop A to a jack and the input of Loop B to another jack. This way, you have two separated loops, not connected to each other.
-You have then 2 ways to connect it

Guitar -> LoopA -> preamp -> LoopB -> power amp
Guitar -> LoopA -> Loop B -> Amp

You can simply use a cable patch from LoopA to LoopB, or even better, using a switching jack, you can make this connection automatic when there is nothing inserted in the post-fx jack.
Each loop is trubypass and controlled via MIDI. If you want to change position of LoopB you need to rewire.

Option 2: Loop B switching

if switching is needed without re-wiring, I would do:
-Basically same as option 1. But in this case, think about post-fx being another loop (at the end it is a loop that goes to the preamp of your amp and returns)
-You keep the 2 relays for LoopA and LoopB for true bypass. post-fx loop is always on.
-You add the FX juggler so that you can change the order of LoopB and post-fx.
http://www.geofex.com/article_folders/juggler/juggler.htm (http://www.geofex.com/article_folders/juggler/juggler.htm)
You can use a rotary 4PDT and add a status LED
This will enable you to change the order of the post-fx and loopB loops, that is in essence placing the loopB before or after your preamp.

Title: Re: Arduino MIDI Switcher
Post by: potul on June 15, 2016, 03:34:59 AM
If you need it I can draw something....
Title: Re: Arduino MIDI Switcher
Post by: andersm on June 15, 2016, 05:44:28 AM
Wow. That is pretty cool, although I have to admit I'm not sure I understood all the technicalities.
I would go with option A, since most effects are either pre or post, really.

Basically there are 2 aspects I haven't quite understood yet.
) How do you wire 2 relays to create a true bypass? Doesn't the actual signal still have to run through them?
) The switching jack, I assume, would be the SEND jack for the B Loop, right? How would that relate to whether it's a post or pre effect?
Sorry for the hassle, maybe a schematic would help me indeed. You do have an online tip jar for coffee I hope ;)
Title: Re: Arduino MIDI Switcher
Post by: potul on June 15, 2016, 05:58:10 AM
Ok,... gimme some time. This evening when I'm  at home I will draw something.

Mat
Title: Re: Arduino MIDI Switcher
Post by: andersm on June 15, 2016, 06:44:17 AM
Take your time dude, I appreciate this!
Title: Re: Arduino MIDI Switcher
Post by: potul on June 16, 2016, 11:43:15 AM
Here it goes, this is what I would do, without fancy switching:

Relay 1 and 2 get activated at the same time.
Relay 3 and 4 get activated at the same time.

(http://i471.photobucket.com/albums/rr74/mateu_ros/switching.png) (http://s471.photobucket.com/user/mateu_ros/media/switching.png.html)

How to connect it..

Option 1: LoopB is "post" (so, in the FX loop of the amp)

Guitar -> IN
LOOPA Send -> Stompbox IN
Stompbox OUT -> LOOP A return
Preamp Send -> Amp IN
Amp FX send -> Preamp Return
LOOP B send -> Stompbox IN
Stompbox OUT -> LOOP B return
OUT -> Amp Fx return

With this connections, LOOP A is pre, LOOP B is post.

Option 2: LoopB is "Pre" (so, before Amp input)

Guitar -> IN
LOOPA Send -> Stompbox IN
Stompbox OUT -> LOOP A return
Preamp Send -> Nothing
Nothing -> Preamp Return
LOOP B send -> Stompbox IN
Stompbox OUT -> LOOP B return
OUT -> AMP IN

With these connections, both LOOPs are PRE. Nothing inserted in the Preamp Send/Return, or you could put there something you want between loops, without switching capability.
Note that the Preamp Send jack has a switch, so when no jack is inserted, the whole Preamp loop is bypassed.


Take a look a it and see if this fulfills your needs.

Mat




Title: Re: Arduino MIDI Switcher
Post by: potul on June 16, 2016, 11:54:39 AM
It's still possible to add "on-the-fly switching" to change LoopB from Pre to Post, using a 3pdt or 4pdt if LED needed.
Title: Re: Arduino MIDI Switcher
Post by: andersm on June 16, 2016, 01:22:37 PM
Alright, I get it now  8)
Yes I will definitely go for this. I didn't even know those switched jacks existed. Brilliant!
Thank you for the schematic.
Title: Re: Arduino MIDI Switcher
Post by: potul on June 17, 2016, 03:47:14 AM
Yep, you have the open type, like this one:

(https://upload.wikimedia.org/wikipedia/commons/a/af/Jack-plug--socket-switch.jpg)
(http://www.prestonelectronics.com/audio2/ttip/images/jack.gif)

And the neutrik style:

(http://www.midikits.net/foot_pedal/mono_skt_wiring.JPG)

Title: Re: Arduino MIDI Switcher
Post by: jimmyq on March 09, 2017, 07:08:17 AM
Wow, this looks awesome!
I've been looking for this kind of thing for a while and this looks the closest to what I've been looking for.
There might be some adjustments I'd need to make though as I'm trying to modify a standard A/B box, so that I can switch it via midi commands from a floor controller.
I'm a complete newbie in this area, but I'm thinking that each relay could route the signal to A or B. Each relay would have to stay on until I change channel so would I need a specific relay type?
I'd have to install a switch on the unit to latch from manual mode to midi mode unless I could wire it so that stomping on it overrides the midi signal. I'd appreciate your thoughts?
Title: Re: Arduino MIDI Switcher
Post by: potul on March 09, 2017, 08:18:38 AM
Hi

For a pure A/B switch you only need one Relay. If you want A/B/Y then it gets more tricky, it could probably be done with 2 relays (without buffering).

If you want to "override" with a stomp, what I would do is add a momentary switch, and do it via software. So you can switch from the stomp, and switch via MIDI. Last one to be actuated wins.

Mat
Title: Re: Arduino MIDI Switcher
Post by: caldersm on March 20, 2017, 03:24:58 PM
Agree with you on using the momentary switch and do the code in the chip.

I just finished some software software using a Teensy.....for a project with 8 momentary switches in and 8 relays out, a Mini OLED display, a rotary encoder with switch, USB Midi and Hardwired Midi with a remote stomp via a cable.  Via the menu you can change PC and CC Channels and the CC Command registers.....also Latching vs Momentary outputs.  About 700 lines code....

Title: Re: Arduino MIDI Switcher
Post by: achben on August 26, 2017, 01:40:54 PM
I have everything working save for being able to change the state of the pins with note on/off or PC.
What code do I put where to change the states of the digital pins with PC 01 and PC 02?
Title: Re: Arduino MIDI Switcher
Post by: potul on August 27, 2017, 02:42:09 AM
It should be easy to add it. I am away from my PC this week. Tonight I will be back home and I will take a look at the code.
Title: Re: Arduino MIDI Switcher
Post by: potul on August 27, 2017, 11:21:38 AM
I have everything working save for being able to change the state of the pins with note on/off or PC.
What code do I put where to change the states of the digital pins with PC 01 and PC 02?

Can you clarify? Do you want to change the state with note on/off, or with PC messages?

If note on/off,  what notes do you want to use for each relay?
Title: Re: Arduino MIDI Switcher
Post by: jmrwoods on February 16, 2018, 02:12:41 PM
Resurrecting this thread.  Here is a version for amps with momentary (non-latching) footswitches.

(https://farm5.staticflickr.com/4652/40258881122_2445354b60_b.jpg)

HUGE thanks to potul for his help with coding, advice, and troubleshooting.

As you can see, this version incorporates LEDs. It senses and shows the amps current state. It senses which is on/off so it knows which relay(s) to engage when memory is recalled. Instead of a normal push button, I elected for a momentary footswitch. In this case, I can set it next to my POD when Im programming (and tap with my foot) or it works just as well sitting on top of the amp when setup more for performance. The button works the exact same was as potul described in his first post (cycles through the four possible states press of >1sec saves to memory).

Sensing circuit was simple (one for each channel) and basically based on the footswitch that came with the amp. A simple voltage divider (with or without LED) would work, too. The end result should be getting around 3V (for an ON state) to the proper Arduino pin, or ~0V for an OFF state. Here is the original sketch, but I ended up with different values for the zeners and added resistors (circuit is at the very bottom of the circuit board in the gut shot).

(https://farm5.staticflickr.com/4760/40258880002_08e7905de1_b.jpg)

(https://farm5.staticflickr.com/4618/40258879702_5a5e571876_b.jpg)

(https://farm5.staticflickr.com/4668/40258884362_99b89aac7e_b.jpg)

(https://farm5.staticflickr.com/4701/40258882662_99340c408f_b.jpg)


Updated code:

Code: [Select]
#include <OneButton.h>
#include <EEPROM.h>
#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
#define SW1 12  //set pin for button (Cycle states & Save)
#define LED1 11 //set pin for Relay1 (Reverb)
#define LED2 10 //set pin for Relay2 (Channel)
#define REV 9   //Set pin for reading Reverb
#define CHAN 8  //set pin for reading Channel

//#define BUT 7 //pin for debugging
//#define BUTUP 6 //pin for debugging
MIDI_CREATE_DEFAULT_INSTANCE();

byte curr_program;  //current PC value

// Setup a new OneButton on pin 11. 
OneButton button1(SW1, true);
//OneButton button2(BUT, true); //debugging
//OneButton button3(BUTUP, true); //debugging

// -----------------------------------------------------------------------------
// This function will be automatically called when a NoteOn is received.
// It must be a void-returning function with the correct parameters,
// see documentation here:
// http://arduinomidilib.fortyseveneffects.com/a00022.html
void handleNoteOn(byte channel, byte pitch, byte velocity)
{
    // Do whatever you want when a note is pressed.
    // Try to keep your callbacks short (no delays ect)
    // otherwise it would slow down the loop() and have a bad impact
    // on real-time performance.
}

void handleNoteOff(byte channel, byte pitch, byte velocity)
{
    // Do something when the note is released.
    // Note that NoteOn messages with 0 velocity are interpreted as NoteOffs.
}

void handleProgramChange(byte channel, byte number)
{
  //do something when receiving PC messages
  byte value;
  curr_program=number; //stores the PC number for later
  //read preset value from EEPROM
  value=EEPROM.read(number);
  //activate relay 1 if needed
     if (getBit(value,1))
   {
    if (!digitalRead(REV))
      {
        digitalWrite(LED1,LOW);
        delay(50);
        digitalWrite(LED1,HIGH);
      }
   }
   else
   {
      if (digitalRead(REV))
      {
        digitalWrite(LED1,LOW);
        delay(50);
        digitalWrite(LED1,HIGH);   
      }
   }
  //activate relay 2 if needed
  if (getBit(value,2))
   {
    if (!digitalRead(CHAN))
      {
        digitalWrite(LED2,LOW);
        delay(50);
        digitalWrite(LED2,HIGH);
      }
   }
   else
   {
      if (digitalRead(CHAN))
      {
        digitalWrite(LED2,LOW);
        delay(50);
        digitalWrite(LED2,HIGH);   
      }
   }

}
//debug start
//void click2(){
//  handleProgramChange(1, curr_program-1);
//  }
//  void click3(){
//  handleProgramChange(1, curr_program+1);
//  }
//debug end

// This function will be called when the button1 was pressed 1 time.
void click1() {
  //toggle Relay1
  byte value;
  value=digitalRead(REV)+digitalRead(CHAN)*2;
  value++;
    if (getBit(value,1))
   {
    if (!digitalRead(REV))
      {
        digitalWrite(LED1,LOW);
        delay(50);
        digitalWrite(LED1,HIGH);
      }
   }
   else
   {
      if (digitalRead(REV))
      {
        digitalWrite(LED1,LOW);
        delay(50);
        digitalWrite(LED1,HIGH);   
      }
   }
  //activate relay 2 if needed
  if (getBit(value,2))
   {
    if (!digitalRead(CHAN))
      {
        digitalWrite(LED2,LOW);
        delay(50);
        digitalWrite(LED2,HIGH);
      }
   }
   else
   {
      if (digitalRead(CHAN))
      {
        digitalWrite(LED2,LOW);
        delay(50);
        digitalWrite(LED2,HIGH);   
      }
   }
} // click1


// This function will be called when the button1 was pressed 2 times in a short timeframe.
void doubleclick1() {
} // doubleclick1


// This function will be called once, when the button1 is pressed for a long time.
void longPressStart1() {
  //save preset
  byte value=0;
  if (digitalRead(REV))
  {
    setBit(value,1);
  }
  if (digitalRead(CHAN))
  {
    setBit(value,2);
  }
  EEPROM.write(curr_program,value);
 
} // longPressStart1


// This function will be called often, while the button1 is pressed for a long time.
void longPress1() {
  //Serial.println("Button 1 longPress...");
} // longPress1


// This function will be called once, when the button1 is released after beeing pressed for a long time.
void longPressStop1() {
  //digitalWrite(LED2,LOW);
} // longPressStop1


// -----------------------------------------------------------------------------
void setup()
{
    pinMode(LED1, OUTPUT);
    digitalWrite(LED1,HIGH);
    pinMode(LED2, OUTPUT);
    digitalWrite(LED2,HIGH);
    pinMode(CHAN, INPUT);
    pinMode(REV, INPUT);
    pinMode(SW1, INPUT_PULLUP);
//    pinMode(BUT, INPUT_PULLUP); //debug
//    pinMode(BUTUP, INPUT_PULLUP);  //debug
    //write inital presets, only for debugging.
//    for (int i=0;i<128;i++)
//    {
//      EEPROM.write(i,i);
//    }
   
    //Define button handling functions
//    button2.attachClick(click2); //debug
//    button3.attachClick(click3); //debug
    button1.attachClick(click1);
    button1.attachDoubleClick(doubleclick1);
    button1.attachLongPressStart(longPressStart1);
    button1.attachLongPressStop(longPressStop1);
    button1.attachDuringLongPress(longPress1);
    // Connect the handleNoteOn function to the library,
    // so it is called upon reception of a NoteOn.
    MIDI.setHandleNoteOn(handleNoteOn);  // Put only the name of the function
    // Do the same for NoteOffs
    MIDI.setHandleNoteOff(handleNoteOff);
    // And for PC message
    MIDI.setHandleProgramChange(handleProgramChange);
    // Initiate MIDI communications, listen to all channels
    MIDI.begin(MIDI_CHANNEL_OMNI);
    //Serial.begin(9600); //Set serial to 9600, only for debugging
   
}
void loop()
{
    // Call MIDI.read the fastest you can for real-time performance.
    MIDI.read();
    // There is no need to check if there are messages incoming
    // if they are bound to a Callback function.
    // The attached method will be called automatically
    // when the corresponding message has been received.

    // Call button tick to check button status
    button1.tick();
//    button2.tick();  //debug
//    button3.tick();  //debug
}

byte setBit(byte &store, byte bitn) { //bit 1 is right-most
      store |= (1 << (bitn - 1)); //set bit 5 to '1'.
}

byte clearBit(byte &store, byte bitn) {
      store &= !(1 << (bitn - 1));
}
 
bool getBit(byte store, byte bitn) {
      byte b = (1 << (bitn - 1));
      return (store & b);
}
Title: Re: Arduino MIDI Switcher
Post by: Hatredman on March 21, 2018, 01:57:48 PM
What a lovely Strat!

.sig goes here.

Title: Re: Arduino MIDI Switcher
Post by: potul on March 22, 2018, 03:31:18 AM
Resurrecting this thread.  Here is a version for amps with momentary (non-latching) footswitches.

I just wanted to step in to highlight the remote collaborative job we did, Mike and I. It was fun to troubleshoot the software and hardware from the other side of the world.

This version will work with a Bugera amp (I don't remember the exact model), but it could maybe work with other brands. The key here is that the circuit is monitoring the status of the channels by looking at the voltage that comes from the amp through the footswitch cable. This only works in those amps that use momentary switches, and have LEDs on the footswitch. For other brands/models, some minor adjustments on the hardware might be needed (adjusting the zener or similar)

Good Job!
Title: Re: Arduino MIDI Switcher
Post by: jmrwoods on April 09, 2018, 12:15:50 PM
What a lovely Strat!

.sig goes here.

Thanks! That's actually a JTV69s Tyler Variax that I refinished. It started out Shoreline Gold, but I wanted to highlight more of the offset design (i.e. Jaguar, Jazzmaster) and less strat. Walnut stain and hand-rubbed oil finish. Turned out great!


Resurrecting this thread.  Here is a version for amps with momentary (non-latching) footswitches.

I just wanted to step in to highlight the remote collaborative job we did, Mike and I. It was fun to troubleshoot the software and hardware from the other side of the world.

This version will work with a Bugera amp (I don't remember the exact model), but it could maybe work with other brands. The key here is that the circuit is monitoring the status of the channels by looking at the voltage that comes from the amp through the footswitch cable. This only works in those amps that use momentary switches, and have LEDs on the footswitch. For other brands/models, some minor adjustments on the hardware might be needed (adjusting the zener or similar)

Good Job!

It WAS a lot of fun - and best of all - works like a charm.
Title: Re: Arduino MIDI Switcher
Post by: EeroN on July 27, 2018, 05:24:45 AM
potul,

Thank you for sharing your instructions here. I just recently completed a three channel tube preamp with a graphic EQ assignable to each channel (Mesa style). I built a temporary footswitch for it, but eventually wanted to have MIDI control. I took your basic circuit and code, and after some tweaking I got it working.

Eventually, I will control five features of my preamp with the controller: CH1 CH2 or CH3, EQ on/off, reverb on/off (CH1 only). I have alredy implemented these in the code, I just have some wiring to do.

I'm running the controller with 12 V from my preamp, as that's the voltage my relays work at. Relay switching in the preamp is controlled by a CMOS Quad fliflop. The Arduino is controlling transistors, which again pull the corresponding logic pins of the fliflop high for channel changes.

Here's a quick video:

https://www.youtube.com/watch?v=-dw1Pc2Kgns

I'll put it in a Hammond enclosure on my pedalboard, where I'll send program changes from a Boss MS-3.

If anyone wants to see the code or has any other questions, I'll happily answer them.

Thanks again potul and all who have contribted to this thread.
Title: Re: Arduino MIDI Switcher
Post by: potul on July 27, 2018, 06:33:56 AM
Great!

Happy to see my work is an inspiration for others.

Mat
Title: Re: Arduino MIDI Switcher
Post by: bogdancat on December 11, 2018, 01:07:53 AM
potul,

Thank you for sharing your instructions here. I just recently completed a three channel tube preamp with a graphic EQ assignable to each channel (Mesa style). I built a temporary footswitch for it, but eventually wanted to have MIDI control. I took your basic circuit and code, and after some tweaking I got it working.

Eventually, I will control five features of my preamp with the controller: CH1 CH2 or CH3, EQ on/off, reverb on/off (CH1 only). I have alredy implemented these in the code, I just have some wiring to do.

I'm running the controller with 12 V from my preamp, as that's the voltage my relays work at. Relay switching in the preamp is controlled by a CMOS Quad fliflop. The Arduino is controlling transistors, which again pull the corresponding logic pins of the fliflop high for channel changes.

Here's a quick video:

https://www.youtube.com/watch?v=-dw1Pc2Kgns

I'll put it in a Hammond enclosure on my pedalboard, where I'll send program changes from a Boss MS-3.

If anyone wants to see the code or has any other questions, I'll happily answer them.

Thanks again potul and all who have contribted to this thread.

Please post the code for your switch. I'm looking to make a similar one for an Ibanez amp.
Title: Re: Arduino MIDI Switcher
Post by: EeroN on December 21, 2018, 10:57:54 AM
Sorry, I just now checked back on this thread. Here's my code:

Code: [Select]
#include <OneButton.h>
#include <EEPROM.h>
#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
#define RLY1 11 //set pin for Relay1
#define RLY2 10 //set pin for Relay2
#define RLY3 9  //set pin for Relay3
#define RLY4 8  //set pin for Relay4
#define REVC 7  //set pin for Reverb control (jfet)
#define SW1 12  //set pin for button
MIDI_CREATE_DEFAULT_INSTANCE();

byte curr_program;  //current PC value
byte state;     //current state

byte n = 1;     //these are for cycling states
byte neq = 0;

unsigned long startMillis; //for getting relays to activate simultaneously
unsigned long currentMillis;
const unsigned long period = 50; //set delay for RLY4

bool eq;
bool ch1;
bool ch2;
bool ch3;
bool rev;

// Setup a new OneButton on pin 11.
OneButton button1(SW1, true);

// -----------------------------------------------------------------------------
// This function will be automatically called when a NoteOn is received.
// It must be a void-returning function with the correct parameters,
// see documentation here:
// http://arduinomidilib.fortyseveneffects.com/a00022.html
void handleNoteOn(byte channel, byte pitch, byte velocity)
{
  // Do whatever you want when a note is pressed.
  // Try to keep your callbacks short (no delays ect)
  // otherwise it would slow down the loop() and have a bad impact
  // on real-time performance.
}

void handleNoteOff(byte channel, byte pitch, byte velocity)
{
  // Do something when the note is released.
  // Note that NoteOn messages with 0 velocity are interpreted as NoteOffs.
}

void handleProgramChange(byte channel, byte number)
{
  //do something when receiving PC messages
  byte value;
  curr_program = number; //stores the PC number for later
  //read preset value from EEPROM
  value = EEPROM.read(number);

  if (getBit(value, 1))
  {
    digitalWrite(RLY4, HIGH);
  }
  if (getBit(value, 2))
  {
    digitalWrite(RLY1, HIGH);
    delay(period);
    digitalWrite(RLY1, LOW);
  }
  if (getBit(value, 3))
  {
    digitalWrite(RLY2, HIGH);
    delay(period);
    digitalWrite(RLY2, LOW);
  }
  if (getBit(value, 4))
  {
    digitalWrite(RLY3, HIGH);
    delay(period);
    digitalWrite(RLY3, LOW);
  }
  if (getBit(value, 5) && getBit(value, 2))
  {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else
  {
    digitalWrite(LED_BUILTIN, LOW);
  }
}

// This function will be called when the button1 was pressed 1 time.
void click1() {

  eq = false;
  ch1 = false;
  ch2 = false;
  ch3 = false;

  byte temp = 1 << n;
  state = temp + neq;

  if (getBit(state, 1))
  {
    digitalWrite(RLY4, HIGH);
    eq = true;
    neq = 0;
    n++;
  }
  else
  {
    neq = 1;
  }

  if (getBit(state, 4))
  {
    digitalWrite(RLY3, HIGH);
    delay(period);
    digitalWrite(RLY3, LOW);
    ch3 = true;
    digitalWrite(LED_BUILTIN, LOW);
  }

  if (getBit(state, 3))
  {
    digitalWrite(RLY2, HIGH);
    delay(period);
    digitalWrite(RLY2, LOW);
    ch2 = true;
    digitalWrite(LED_BUILTIN, LOW);
  }

  if (getBit(state, 2))
  {
    digitalWrite(RLY1, HIGH);
    delay(period);
    digitalWrite(RLY1, LOW);
    ch1 = true;
  }
  if (state == 9) //restart cycling all states
  {
    n = 1;
    neq = 0;
  }
} // click1


// This function will be called when the button1 was pressed 2 times in a short timeframe.
void doubleclick1() { //toggle reverb
  if (rev == false && ch1)
  {
    digitalWrite(LED_BUILTIN, HIGH);
    rev = true;
  }
  else
  {
    digitalWrite(LED_BUILTIN, LOW);
    rev = false;
  }

} // doubleclick1


// This function will be called once, when the button1 is pressed for a long time.
void longPressStart1() {
  //save preset
  byte value;
  if (eq)
  {
    setBit(value, 1);
    //Serial.print("eq ");
  }
  if (ch1)
  {
    setBit(value, 2);
    //Serial.print("ch1 ");
  }
  if (ch2)
  {
    setBit(value, 3);
    //Serial.print("ch2 ");
  }
  if (ch3)
  {
    setBit(value, 4);
    //Serial.print("ch3 ");
  }
  if (digitalRead(LED_BUILTIN) && ch1)
  {
    setBit(value, 5);
    //Serial.print(" rev");
  }

  EEPROM.write(curr_program, value);
  //Serial.println(value, BIN);

} // longPressStart1


// This function will be called often, while the button1 is pressed for a long time.
void longPress1() {
  //Serial.println("Button 1 longPress...");
} // longPress1


// This function will be called once, when the button1 is released after beeing pressed for a long time.
void longPressStop1() {
  //digitalWrite(RLY2,LOW);
} // longPressStop1


// -----------------------------------------------------------------------------
void setup()
{
  pinMode(RLY1, OUTPUT);
  pinMode(RLY2, OUTPUT);
  pinMode(RLY3, OUTPUT);
  pinMode(RLY4, OUTPUT);
  pinMode(REVC, OUTPUT);
  pinMode(SW1, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);

  //write inital presets, only for debugging.
  for (int i = 0; i < 128; i++)
  {
    //EEPROM.write(i,i);
  }

  //Define button handling functions
  button1.attachClick(click1);
  button1.attachDoubleClick(doubleclick1);
  button1.attachLongPressStart(longPressStart1);
  button1.attachLongPressStop(longPressStop1);
  button1.attachDuringLongPress(longPress1);
  // Connect the handleNoteOn function to the library,
  // so it is called upon reception of a NoteOn.
  MIDI.setHandleNoteOn(handleNoteOn);  // Put only the name of the function
  // Do the same for NoteOffs
  MIDI.setHandleNoteOff(handleNoteOff);
  // And for PC message
  MIDI.setHandleProgramChange(handleProgramChange);
  // Initiate MIDI communications, listen to all channels
  MIDI.begin(MIDI_CHANNEL_OMNI);
  //Serial.begin(9600); //Set serial to 9600, only for debugging
  //Serial.println("Start");

}
void loop()
{
  // Call MIDI.read the fastest you can for real-time performance.
  MIDI.read();
  // There is no need to check if there are messages incoming
  // if they are bound to a Callback function.
  // The attached method will be called automatically
  // when the corresponding message has been received.

  // Call button tick to check button status
  button1.tick();
  // See if RLY4 is on, turn it off after preset period
  currentMillis = millis();
  if (digitalRead(RLY4));
  {
    startMillis = millis(); //start counting milliseconds if RLY4 on
    if (currentMillis - startMillis >= period)
    {
      digitalWrite(RLY4, LOW);
    }
  }
}

byte setBit(byte &store, byte bitn) { //bit 1 is right-most
  store |= (1 << (bitn - 1)); //set bit 5 to '1'.
}

byte clearBit(byte &store, byte bitn) {
  store &= !(1 << (bitn - 1));
}

byte getBit(byte store, byte bitn) {
  byte b = (1 << (bitn - 1));
  return (store & b);
}


It's been a while since I wrote this, but I'll try to explain. I'm not really a programmer at all, so the code is probably a bit of a hack job and it's still a work in progress, but it works for now.

The sound preset (channel, eq and reverb on or off) is stored as byte 'value' which has 5 bits. The least significant bit indicates EQ state, the next three are the preamp channels 1 to 3, bit 5 is the CH1 reverb (although I haven't implemented this in my preamp yet, so it just lights the inbuilt led).

So CH1 with no EQ is 00010, CH1 with EQ is 00011, CH2 without EQ is 00100, CH2 with EQ is 00101, CH3 without EQ is 01000, CH3 with EQ is 01001. The reverb for CH1 is activated by a double click, and the current program is saved by a long press.

Also note that all of these different states are non-latching, so they only give a pulse of 50 ms (controlled by "const unsigned long period"), you might need to change it to operate as latching.

Hope this helps!

Title: Re: Arduino MIDI Switcher
Post by: sixtheory on January 01, 2019, 09:37:12 AM
Ok folks, here's what i got:

I'm needing to adapt this to control a Mesa Roadking Series I head... i Know its a big undertaking.

This amp has 8 remotely-controllable functions via "Latching type, Tip-to-Ground" Logic on  1/4" mono jacks mounted on the rear panel of the amp head. I have a Decibel 11 Midi controller for my guitar rig to control functions via midi.

It might be too much to tackle for me, as i am pretty new to this. I have brief knowledge of programming Arduino for a college project..but thats about it?

Any Help would be greatly appreciated!
Title: Re: Arduino MIDI Switcher
Post by: EeroN on January 02, 2019, 06:59:37 AM
Ok folks, here's what i got:

I'm needing to adapt this to control a Mesa Roadking Series I head... i Know its a big undertaking.

This amp has 8 remotely-controllable functions via "Latching type, Tip-to-Ground" Logic on  1/4" mono jacks mounted on the rear panel of the amp head. I have a Decibel 11 Midi controller for my guitar rig to control functions via midi.

It might be too much to tackle for me, as i am pretty new to this. I have brief knowledge of programming Arduino for a college project..but thats about it?

Any Help would be greatly appreciated!

After a quick google search it seems to be possible to have multiple OneButtons within a single arduino sketch. I'd suggest setting up three of them as follows:

Button1: Channel 1, 2, 3 or 4, long press for program save
Button2: Loop 1, 2 or loop off
Button3: Combinations of Reverb and Solo on/off (4 in total)

It would of course be possible to use a single OneButton, but a total of 255 states is a lot to cycle through :) so from a usability standpoint it's probably worth the trouble to use multiple buttons.

The Nano has a total of 12 digital pins (D2-D13), so you could use the first 8 (D2-D9) for your control outputs and three (D10-D12) for the OneButtons and still have D13 (the inbuilt LED) for an indicator.

The hardware bit also needs some consideration. I've been experimenting with the ULN2003 IC, which I originally got for controlling stepper motors for another unrelated project, but it can be used as a logic buffer too. I'm not sure, but it might be possible to use that instead of having to wire up 8 relays to the arduino. The problem for your project is that the ULN2003 only has 7 inputs but your application needs 8. One solution would just be to use 2 of those, still a lot less PCB real estate than 8 relays.

What you should do is find out the control voltage sent out by the RK.

I can also do a bit of breadboarding for you later today with the Nano and the ULN2003 and report back with my results.
Title: Re: Arduino MIDI Switcher
Post by: EeroN on January 03, 2019, 08:43:26 AM
I breadboarded the Nano with the ULN2003, and it seems to work as I thought. The ULN2003 doesn't pull the pins all the way to ground, but to about 0.7 V, which is probably close enough to trigger switching on the amp.

I also took a look at the RK schematic (version 2, but I believe the switching circuit on version 1 is similar), and the voltage used by the switching circuitry is 12V and that's what I used to test my circuit, so I'd say it's worth a try. The problem is you'd need a separate powersupply to power the Nano. You can't use the voltage from the RK switching circuit.

Of course, the simple solution to the above concerns would be to just use 8 relays, but this will require a much bigger PCB and will cost more. You'd also need to take into consideration the current drawn by the relays, and you'd need to use a separate power source for the circuit regardless.
Title: Re: Arduino MIDI Switcher
Post by: ElectricDruid on January 05, 2019, 05:28:45 AM
If you're just triggering switching in the amp, a simple transistor is probably enough. A relay would be overkill. Use the Arduino to switch a transistor, and then use the transistor to short the tip to ground as if it was a foot switch.

T.
Title: Re: Arduino MIDI Switcher
Post by: EeroN on January 05, 2019, 07:19:27 AM
If you're just triggering switching in the amp, a simple transistor is probably enough. A relay would be overkill. Use the Arduino to switch a transistor, and then use the transistor to short the tip to ground as if it was a foot switch.

T.

Agreed, and that's how I did mine (see image).

(https://i.postimg.cc/zVTYC5m9/IMG-0198-1.jpg) (https://postimg.cc/zVTYC5m9)

That's also why I suggested the ULN2003, it basically has seven transistor switches in one package, resistors and all. Much less soldering than seven individual transistors. The Road King needs eight, but the remaining one can be easily implemented with a separate NPN transistor.