I am busy building a table with three electric drawers in it.
One drawer is connected like in this scematic

Pin 6 is connected to the negative lead of the optocoupler
Also the arduino is connected to a QT110 touch sensor(on pin 4) who is build so it acts like: Touch= -5v Not touching=+5v(for just one drawer,the other drawers are operated with a fysical switch,but this one needs to be able to be controlled by iphone over time(not yet))
the other drawers are not connected to the arduino,the relay's are switched on/off by a normal flip switch
the arduino program is this:
int i=0;
int a=0;
void setup()
{
Serial.begin(9600);
pinMode(4, INPUT);
pinMode(6, OUTPUT);
}
void loop()
{
if (i == 0){ digitalWrite(6, LOW);} else{digitalWrite(6, HIGH);}
if (i == 0 && digitalRead(4) == HIGH && a == 1){i = 1;} else{} // LED moet aan
if (i == 1 && digitalRead(4) == HIGH && a == 0){i = 0;} else{} // LED moet uit
if (i == 0 && digitalRead(4) == LOW){a = 1;} else{} // Test LED=uit en S4 is losgelaten
if (i == 1 && digitalRead(4) == LOW){a = 0;} else{} // Test LED=aan en S4 is losgelaten
Serial.print("a=");
Serial.print(a);
Serial.print(" i=");
Serial.print(i);
Serial.print(" I4=");
Serial.println(digitalRead(4));
}
Now the problem is this,
When the switch is flipped on one of the two fysicaly switched drawers and it hits an end switch (S1 or S2 on the scheme), the drawer that is rigged with the arduino always opens or closes. It seems to me the reverse peak current when the motor switches of is somehow making the arduino switch pin 6 on or off OR making the QT110 switch,but i doubt that because when I leave the arduino out, the problem does not occur anymore.
How could i solve this problem and how could a 12v circuit interfere with an arduino?
The 12v for all three motors is a 14amp source
The 5v for the QT110 coming from a stabilised 7805 circuit who is fed by the 12v source
The Arduino is powered by a usb cable to my computer
Thanks in advance