I'm working on a project where I use midi notes to switch 8 old lamps using the Arduino Uno. I've built a case with 8 wall sockets that are linked up to a relay board for the Arduino. I am using the Hairless midi serial bridge to send midi notes via USB to the Arduino.
This all works until I put power on the sockets with my Uno. 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. When there isn't 220 volts going through the relays it all works great.
(My schematics are below.) 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.
Code:
#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();
}
It's based on code I've found for processing serial midi.
I'm really confused as to why this happens. I want to know why my Arduino and relay board freeze when there is 220 volts going through the relays.
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