Hey everyone,
I’m working on a project where I use midi notes to switch 8 old lamps using the Arduino Uno. I’ve build a case with 8 wall sockets that are linked up to a relay board for the Arduino. I use the Hairless midi serial bridge to send midi notes via USB to the Arduino. This serial data is used to send digital outputs to the relay board. This board has eight relays that can be switched with 5 volts.
This all works good and fast until I put power on to the sockets.
Then, after about 5~10 seconds the Arduino freezes. The relay shield stays in it’s current state and the indication lights for serial communication stop flashing. The Hairless serial midi bridge does still show activity. When there isn’t 220 volts going through the relays it all works great. The relay board I’m using had 8 LED’s for indication and they respond as intended without the power switched on.
I tried to make a simple schematic to clarify my setup. The Arduino is powered via USB. I also tried powering the arduino with an additional adapter of 5V and 500mA but that didn’t make a difference.
I’m really confused as to why this happens and I really hope someone can help me. I’m a novice so I might be doing something stupid here (highly probable).
Materials:
- Arduino Uno revision 3
- Relay board with 8 relays switched with 5 volt. Capable of handling 10A. Bought here: https://iprototype.nl/products/components/buttons-switches/relay-board-8-channels-5v
- 8 normal wallsockets powered with 220 volts
- 8 light bulbs with E27 fitting
This is the code that is running on my Arduino. It’s based on code I’ve found for processing serial midi.
#include <digitalWriteFast.h>
byte incomingByte=0;
byte notebyte=0;
byte velocitybyte=0;
byte statusbuffer=0;
byte NOTE_ON = 144;
byte NOTE_OFF = 128;
boolean arp_triggernext=false;
boolean firstbyte;
void MIDI_Poll(){
if (Serial.available() > 0) {
do {
// read the incoming byte:
incomingByte = Serial.read();
if (incomingByte>247) {
// this is where MIDI clock stuff is done
switch (incomingByte){
}
}
else if (incomingByte>240) {
statusbuffer = 0;
//sysex stuff done here
}
else if (incomingByte>127) {
statusbuffer = incomingByte;
firstbyte = true;
notebyte = 0;
velocitybyte = 0;
}
else if (statusbuffer!=0) {
if (firstbyte == true) {
// must be first byte
notebyte = incomingByte;
firstbyte = false;
}
else {
// so must be second byte then
velocitybyte = incomingByte;
//process the message here
if (statusbuffer == NOTE_ON && velocitybyte != 0) {
switch (notebyte) {
case 60:
digitalWriteFast2(2, HIGH);
break;
case 61:
digitalWriteFast2(3, HIGH);
break;
case 62:
digitalWriteFast2(4, HIGH);
break;
case 63:
digitalWriteFast2(5, HIGH);
break;
case 64:
digitalWriteFast2(6, HIGH);
break;
case 65:
digitalWriteFast2(7, HIGH);
break;
case 66:
digitalWriteFast2(8, HIGH);
break;
case 67:
digitalWriteFast2(9, HIGH);
break;
}
}
else if (statusbuffer == NOTE_OFF || (statusbuffer == NOTE_ON && velocitybyte == 0)) {
switch (notebyte){
case 60:
digitalWriteFast2(2, LOW);
break;
case 61:
digitalWriteFast2(3, LOW);
break;
case 62:
digitalWriteFast2(4, LOW);
break;
case 63:
digitalWriteFast2(5, LOW);
break;
case 64:
digitalWriteFast2(6, LOW);
break;
case 65:
digitalWriteFast2(7, LOW);
break;
case 66:
digitalWriteFast2(8, LOW);
break;
case 67:
digitalWriteFast2(9, LOW);
break;
}
}
//now clear them for next note
notebyte = 0;
velocitybyte = 0;
firstbyte = true;
}
}
} while (Serial.available() > 0);
}
}
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
}
void loop() {
MIDI_Poll();
}
Thanks in advance!
- Jwktje