MIDI Sprout

Discussion (*ask your questions here!) => Questions => Topic started by: Julio on January 10, 2018, 06:50:48 AM

Title: MidiSprout controlling 8channel Switch Relay
Post by: Julio on January 10, 2018, 06:50:48 AM
Hi Sam,

I'm preparing an art installation with the MidiSprout builded with your tutorials,
My idea is to now change the MIDI output jack of my midi sprout (I do not need midi notes), what I need is something simpler, I want to control in some way an 8-channel relays switch module (the ones that often used with arduino).

at the moment I am controlling the relays with a simple random generator written in an Arduino Uno. the connection and circuit of the relays work very well.

My question is: how could I make the plant define those parameters on and off in the 8 channel relays module? that is: (1) which relay channel it selects and (2) how long that channel remains on, before it is disconnected again.

thanks a lot in advance!
Title: Re: MidiSprout controlling 8channel Switch Relay
Post by: sam on January 10, 2018, 12:14:12 PM
Hello Julio, yes that is a great idea to control a set of relays with the sprout.  How much programming experience do you have, and is this your first electronics project?  Your modifications are very doable, but it will take a bit of solder and code!

1.) what type of relay board are you using?  Many relay boards for arduino have 8 individual digital input pins, some use SPI/I2C, and others can use Serial to trigger the relays.   I think we have 8 digital pins available on the sprout, so even the most basic or diy relay setup should be able to be 'wired' to the sprout.

2.) You can easily modify the code to turn a relay on when a note is triggered and off when an note is updated. 
   a. First you should increase the note polyphony from 5 to 8 (to match your relays). 
   b. Second, In setNote() add  some code to turn on the relay digitalWrite(relayPin[ i ],HIGH);  where relayPin[] is an array containing the pin number for each of the relay inputs and "i" is the currently available 'note' of the 8 available from polyphony
   c. Third, in checkNote() add some code to turn off the relay when the note is expired digitalWrite(relayPin[ i ],LOW);

This method will have the relays mirror the notes and durations as they turn on and off.

Good luck!  please post some more info of your build!

Title: Re: MidiSprout controlling 8channel Switch Relay
Post by: Julio on January 17, 2018, 10:44:34 AM
Hi Sam,
sorry for the late reply, I did not get a notification of your response:)
thanks for your reply

well, programming experience very little, but tomorrow I ll have some help from an expert.
I have more experience with soldering DIY oscillators dub sirens, and simple synths, this is my first one involving code.

1. this is my relay board:
(8 Channel 5V Relay Module Modul for Arduino)
Now it is working with a simple code with a random generator / it is working just fine / but I would like to have a real change of this random generator with the input signal of the Midi Sprout.
This is the code I am using:
#define RELAY1  2
#define RELAY2  3
#define RELAY3  4
#define RELAY4  5
#define RELAY5  6
#define RELAY6  7
#define RELAY7  8
#define RELAY8  9
byte relays[8] = { RELAY1, RELAY2, RELAY3, RELAY4, RELAY5, RELAY6, RELAY7, RELAY8};
void setup() {
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  pinMode(RELAY5, OUTPUT);
  pinMode(RELAY6, OUTPUT);
  pinMode(RELAY7, OUTPUT);
  pinMode(RELAY8, OUTPUT);
void loop() {
  digitalWrite(relays[random(0, 8)], random(0, 4));
  delay(random(500, 5000));

and about my DIY sprout build, here is a picture of it:
(please note I could not find a .0042uF capacitor so instead I use a .0047
also I got so much trouble trying to bootload the ATMEGA328-PU so I use instead the ATMEGA328P-PU from an Arduino UNO, do you think this 2 changes from the original kit would be problematic?, seems to work fine till now)

so, in short, I have this two "devices" working separately.., now I need to somehow make them communicate to each other.. :)

Thanks a lot for the coding suggestions and very creative input!, tomorrow I ll test them with the build, hopefully everything works 0k.

Title: Re: MidiSprout controlling 8channel Switch Relay
Post by: Julio on January 20, 2018, 07:14:49 PM
I have the soldering part done (not using Midi Out + leds)
and using 8 digital out from ATMEGA connected to 8channel relay board:
so we tried to work on editing the original code to switch the relays on/off, with still no luck:(
Title: Re: MidiSprout controlling 8channel Switch Relay
Post by: sam on January 20, 2018, 09:19:15 PM
Send me a link to your code and I'd be happy to help.  This should be a fairly easy modification with the right digital pins.
Title: Re: MidiSprout controlling 8channel Switch Relay
Post by: Julio on January 23, 2018, 03:19:49 PM
hey @sam
thanks a lot!
here is the code:

I am using Pins 12-19 in the ATMEGA
(6-13 digital pins in an Arduino)
Title: Re: MidiSprout controlling 8channel Switch Relay
Post by: sam on January 24, 2018, 09:27:27 PM
Well, after reviewing your code it all looks fine to me.  But i think that you may have cut out too much of the functioning code while removing the LED parts.  Whenever you modify an existing codebase or circuit, i very much recommend sticking with as much of the original design as possible, that way debugging can be very easy.

That is why i gave suggested pin numbers in my last post, you can avoid conflicting pins and still have the debugging/visual feedback from the LEDs. 

I think you will need to change the serial baud rate and add some debug statements to see where your code is not working.  Your relay statements look like they are all in the correct places, but there might be something that is going wrong now that there is no light show and also no MIDI data output.  The LED array is used for a few functions (like the Control Voltage output mod on controlLED) and that is likely the bug why you are not seeing the desired outcomes.  Perhaps comment out checkControl() from the main loop and setControl() in analyzeSample

Or... I'd suggest that if you added the LEDs and changed to the digital control pins i suggested, that way you get both points of feedback during your debugging and setup.  you can always turn off the led light show by setting noteLEDs = 0 (could use a toggling button or a switch polled in the main loop)

One more important thing to note here ... it seems ridiculously dangerous to attach mains voltage relays to a plant ;)  or worse yet to a human.  You will need to be extremely careful with your circuit! The magnetic relays are optically isolated on your board, and that is very good (i think you know what you are doing).  But i just didn't want to underemphasize to others who might see this project, it is very danger to deal with mains voltages!
Title: Re: MidiSprout controlling 8channel Switch Relay
Post by: Julio on January 25, 2018, 12:08:34 PM
WOW! thank you very much for your great help Sam,
will follow your advice and rebuild the hardware without ignoring the LEDs and the midi output, using the digital outputs you indicate at the beginning.
Also, will edit your code once again from scratch, without taking anything out.

Once again, thank you very much for your safety recommendations and great support that I feel towards my project. I am taking the utmost precautions.
It is truly a giant help.
I will post the progress of the project in the coming days.

Have a nice day.
Title: Re: MidiSprout controlling 8channel Switch Relay
Post by: Julio on February 13, 2018, 06:02:42 PM
Here is some documentation screenshots of a video from the sound installation using the midi sprout controlling lights and electric fans with 8channel relay module: