I am using an Arduino Uno to create a kinetic sculpture with magnets and electromagnets (EMs). I have a setup with two externally debounced buttons using resistors,capacitor and CMOS 4584 hex inverter. All non-used inputs of the 4584 are grounded. These buttons deliver a clean signal to the board and trigger the two interrupts which set flags to turn LEDs on or off and trigger one or both optoisolators (OIs). These connect to another board with a separate 12V supply. One of the OIs turns a BUZ71 power mosfet on and the other triggers a relay which can reverse the polarity of the power to a small 30 ohm EM coil. This can make the magnet suspended above like a pendulum fly away from the coil...
This works perfectly well with the buttons, the LEDs and the two inputs to the OIs But when I connect the relay/mosfet board to the output of the OIs it stops working properly. button 2 turns off button 1 as well as itself. Button one always turns off and on cleanly But button two triggers ISR2 about 10 times and ISR1 a couple of times, leaving both off. If I disconnect the EM coil the buttons work properly. It seems that switching the polarity is somehow interfering with the Uno maybe due to a fairly hefty inductive spike generated by switching the relay. I have tried using shielded coax to the coil. I have 47000 uF caps on both boards. I tried a crude faraday cage around the relay board - no luck. Could the spike be coming through the household power supply somehow even though the Uno board and the relay board have different power supplies and are fully seperated by the OIs? Here is the sketch I am using:
// Coding by Jeremy 070520
// development for ricochet module
const int coilButton = 2;
const int directionButton = 3;
const int coilPin = 8; //coil driver
const int directionPin = 9; //direction driver
volatile bool coilFlag;
volatile bool directionFlag;
int coilLedState = LOW;
int directionLedState = LOW;
void setup()
{
pinMode(coilButton,INPUT);
pinMode(directionButton, INPUT);
pinMode(coilPin, OUTPUT);
pinMode(directionPin, OUTPUT);
digitalWrite(coilPin,LOW);
digitalWrite(directionPin, LOW);
attachInterrupt(digitalPinToInterrupt(coilButton), ISR1, FALLING);
attachInterrupt(digitalPinToInterrupt(directionButton), ISR2, FALLING);
Serial.begin(9600);
}
void loop(){
if(coilFlag)
{
coilFlag = false;
coilLedState = !coilLedState;
digitalWrite(coilPin, coilLedState);
}
if(directionFlag)
{
directionFlag = false;
directionLedState = !directionLedState;
digitalWrite(directionPin, directionLedState);
}
delay(1); //I'm desperate I will try anything ....
} //end loop
void ISR1()
{
Serial.println("ISR1");
coilFlag = true;
}
void ISR2()
{
Serial.println("ISR2");
directionFlag = true;
}