Serial port freeze

Hi,

I'm using Arduino UNO, and I need to control 24VAC motor via serial port.
I have relay board with 2 opto-isolated relays and separate 5V PS to trigger them, those two relays are controlling AC motor (CW and CCW).
I'm using Arduino Serial monitor to send commands to arduino, as follows: 1 - CW , 2 - CCW , 3 - turn off motor.

Sometimes everything works great for hours, but at random serial port just freezes and TX light stays on.
I need to restart PC or unpplug arduino to make it work again.

I think there is some problem in code, because all wires are "clean and separated(shielded).

int ccw = 8;
int cw = 9;
int incomingbyte;

void setup()
{
  Serial.begin(9600);
  pinMode(ccw,OUTPUT);
  pinMode(cw,OUTPUT);
}

void loop()
{
  if(Serial.available() > 0)
  {
    incomingbyte = Serial.read();
  }

  if(incomingbyte == '1'){
    if (digitalRead(ccw) == HIGH){
    digitalWrite(ccw,LOW);
    }
  delay(100);
  digitalWrite(cw,HIGH);
  }

  if(incomingbyte == '2'){
    if (digitalRead(cw) == HIGH){
    digitalWrite(cw,LOW);
    }
  delay(100);
  digitalWrite(ccw,HIGH);
  }

  if(incomingbyte == '3'){
    if (digitalRead(cw) == HIGH){
        delay(100);
    digitalWrite(cw,LOW);
    }
    if (digitalRead(ccw) == HIGH){
        delay(100);
    digitalWrite(ccw,LOW);
    }
   }

}

You need to put all the stuff about responding to serial command inside the if-statement,
otherwise it'll just repeat the last command indefinitely. incomingByte should be local
to the if:

int ccw = 8;
int cw = 9;

void setup()
{
  Serial.begin(9600);
  pinMode(ccw,OUTPUT);
  pinMode(cw,OUTPUT);
}

void loop()
{
  if(Serial.available() > 0)
  {
    int incomingbyte = Serial.read();

    if(incomingbyte == '1'){
      if (digitalRead(ccw) == HIGH){
        digitalWrite(ccw,LOW);
      }
      delay(100);
      digitalWrite(cw,HIGH);
    }

    if(incomingbyte == '2'){
      if (digitalRead(cw) == HIGH){
        digitalWrite(cw,LOW);
      }
      delay(100);
      digitalWrite(ccw,HIGH);
    }

    if(incomingbyte == '3'){
      if (digitalRead(cw) == HIGH){
        delay(100);
        digitalWrite(cw,LOW);
      }
      if (digitalRead(ccw) == HIGH){
        delay(100);
        digitalWrite(ccw,LOW);
      }
    }
  }
}

Can't see obvious problem that would jam the serial line, could be software related
on the PC, could be hardware related (running the serial cable near other wiring)

MarkT:
You need to put all the stuff about responding to serial command inside the if-statement,
otherwise it'll just repeat the last command indefinitely. incomingByte should be local
to the if:

int ccw = 8;

int cw = 9;

void setup()
{
  Serial.begin(9600);
  pinMode(ccw,OUTPUT);
  pinMode(cw,OUTPUT);
}

void loop()
{
  if(Serial.available() > 0)
  {
    int incomingbyte = Serial.read();

if(incomingbyte == '1'){
      if (digitalRead(ccw) == HIGH){
        digitalWrite(ccw,LOW);
      }
      delay(100);
      digitalWrite(cw,HIGH);
    }

if(incomingbyte == '2'){
      if (digitalRead(cw) == HIGH){
        digitalWrite(cw,LOW);
      }
      delay(100);
      digitalWrite(ccw,HIGH);
    }

if(incomingbyte == '3'){
      if (digitalRead(cw) == HIGH){
        delay(100);
        digitalWrite(cw,LOW);
      }
      if (digitalRead(ccw) == HIGH){
        delay(100);
        digitalWrite(ccw,LOW);
      }
    }
  }
}




Can't see obvious problem that would jam the serial line, could be software related
on the PC, could be hardware related (running the serial cable near other wiring)

Unfortunately, same thing happens even with this code..
I'm using latest Arduino IDE,.

What is being sent as 1, 2, and 3 from the serial monitor are characters. You might try the incomingbyte as a character instead of an integer.

That either didn't work..I will try to unhook AC motor from relays and see what happens when they are unloaded.
Thanks everyone!

Yep, it seems that I have EMI issues.
I have unhooked AC motor form relays and no more freezing.

What can I do about that?

Add decoupling to the motor's power supply. You might also fit a 0.1 uF ceramic capacitor across the motor as close ass you can get.

I was thinking to put 1N4007 diode to motor power supply, but I'm not sure how to wire it.
AC motor has three wires, one is common and other two's are connected to 25VAC (I'm switching phase(direction-CCW,CW) with capacitor on those two wires).

Where should I put diode, or eventually 0.1uF capacitor?
Thanks.

Problem is definitely in EMI caused by AC motor operation.
I draw quick "schematic" in paint (Arduino side isn't relevant).

Where should I put capacitor or diode to get rid of those issues?
Thanks.

bump

You will not find a 100uF cap capable of working at mains voltage.
You do not need diodes.
Fit a small capacitor 0.1uF as close to the motor terminals as possible.
You might want to look at snubber circuits that include inductors and capacitors that make the phase not so disruptive.
You can also try winding some of the wiring through ferrite rings.