MIDI(to serial) -> Solenoid problem

Hi! This is my first post here, I just started fiddling around with the arduino and I have very basic programming skills. What I am trying to do is having two solenoids trigger seperately when the arduino recieves midi messages(via hairless midi) on either midi ch.1 or ch.2. It works just fine when I use just one channel, and one solenoid. But as soon as I add the second solenoid and try to use two midi channels it completely ignores one of them, or confuses them and play them at the same time or randomly alternate between them.

I could as well use one midi channel, and trigger the two solenoids using different notes. But I don't quite understand how to do that with the information I've found.

Here's my code

MIDI_CREATE_DEFAULT_INSTANCE();

#define LED1 13
#define LED2 12

void setup()
{
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
    MIDI.begin(1);          // Launch MIDI and listen to all channel 1
    MIDI.begin(2);          // Launch MIDI and listen to all channel 2
    Serial.begin(115200);
}

void loop()
{
    if (MIDI.read(1))                // If we have received a message on channel 1
    {
        digitalWrite(LED1,HIGH); // Play solenoid 1
        delay(10);		        // Wait
        digitalWrite(LED1,LOW);
    }
    
       if (MIDI.read(2))                // If we have received a message on channel 2
    {
        digitalWrite(LED2,HIGH); // Play solenoid 2
        delay(10);		        // Wait
        digitalWrite(LED2,LOW);
    }
    
}

You need to post all your code. Missing is the bit where you load in the library you are using.
Supply a link to that as well.

Can your solenoid respond to a 10mS pulse?

Grumpy_Mike:
You need to post all your code. Missing is the bit where you load in the library you are using.
Supply a link to that as well.

Can your solenoid respond to a 10mS pulse?

Of course, my bad! I'm using the arduino midi library which can be found here: Arduino Playground - HomePage

Yes, the solenoids work as intended when I use a simple code to trigger them. This was the first thing I did to check that all connections worked etc. The problem arises when I add the midi stuff into the code. It's like it gets confused when I want to use two midi channels, hence why I wanted to try to use one channel and different midi notes for the solenoids instead. However I can't figure out how to get that working so I just went with using seperate midi channels instead, hehe. :slight_smile:

Also, I'm using an arduino uno!

I could as well use one midi channel, and trigger the two solenoids using different notes. But I don't quite understand how to do that with the information I've found.

Like this:-

#include <MIDI.h>
#define LED1 13
#define LED2 12

void setup()
{
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
  MIDI.begin(1);
  MIDI.setHandleNoteOn(HandleNoteOn);
  Serial.begin(115200);	
}

void loop() {
  MIDI.read();
}

void HandleNoteOn(byte channel, byte pitch, byte velocity){
   if(pitch==65){  // or what ever note you want
        digitalWrite(LED1,HIGH); // Play solenoid 1
        delay(10);		        // Wait
        digitalWrite(LED1,LOW);
   } 
      if(pitch==65){  // or what ever note you want
        digitalWrite(LED2,HIGH); // Play solenoid 1
        delay(10);		        // Wait
        digitalWrite(LED2,LOW);
   } 
}

Works exactly as I wanted it to work now!! Thank you so much Grumpy_Mike, you saved my day :slight_smile:

I'm back! This time I'm trying to do the same thing but with six solenoids, programming works just fine it seems. All I've done is added more copies of the same code that controls the relays. But hairless midi is randomly losing connection with the arduino it seems! It just randomly stops working, and when I bring up the hairless midi window and choose the arduino (in the serial port drop down menu) it works for a few seconds again, then stops working, I click it again and it works for a couple of seconds. Hairless midi isn't crashing, the arduino just doesn't respond to what it's sending. I'm using an external powersupply to power the solenoids so that shouldn't be a problem?

It seems to have something to do with the power, even though it's external. It works when the power (going through the terminals on the relays) is not plugged in(only triggering the relays without any power going through), as soon as I plug the power in it stops working after a while again.

You might be getting interference from the solenoids.

Do you have a reverse biased diode across each solenoid?

Have you got some large capacitors across the solenoid's power supply?

I have neither of those, this is what happens when you give a clueless musician the tools to make his own electronics hah.. Thanks for helping a newbie out! So one reverse biased diode across each solenoid and just one capacitor for the power supply, how "large" should the capacitor be? The solenoids are fed 5V DC.

Thank you once again!

At least 100uF, maybe use two one each end of the chain of parts.

With capacitor(s) added the solenoids don't fire at all, is it because the power only comes through to them when the relays are closed? So that they don't get enough power to charge up properly? The capacitors are rated 6.3V 100uF. I've wired them in series, but perhaps they should be wired parallel?

I totally misunderstood what you meant by "across each solenoid"! It seems to be working now, thank you so much :slight_smile:

With capacitor(s) added the solenoids don't fire at all,

Then you have wired them up wrong. The capacitors go from the positive supply going into the solenoid to the ground.

The capacitors are rated 6.3V 100uF.

That is only OK if the solenoid supply is 5V.

iamrucke:
I totally misunderstood what you meant by "across each solenoid"! It seems to be working now, thank you so much :slight_smile:

Across = in parallel with
in line = in series with