Problem in Computer controlled AC Alarm control

I want to control a 220/250 V , 1.04A AC emergency bell through computer and arduino. I have arduino uno and 5 volt relay for this purpose. I manage to write asimple arduino program which turns HIGH a digital pin when receives character ‘a’ from serial port. After two seconds delay, it turns LOW that pin.

I wrote the computer program in c#. All works as expected when we use a 100W/200W electric bulb instead of emergency alarm. But when we connect the alarm, after 1 execution, the program hangs, and computer failed to connect to the serial port. To make it work again, I have to replug all power cords.

Here is my arduino code,

int received = 0;

void setup()
{
pinMode(12,OUTPUT);
Serial.begin(115200);
}

void loop(){
if(Serial.available() == 1)
{
received = Serial.read();
switch (received)
{

case 97: // Received ASCII a
digitalWrite(12, HIGH);
delay(2000);
digitalWrite(12, LOW);
received=0;
Serial.flush();
break; //Move on

default:
digitalWrite(12, LOW);
break;
}
}
}

And here is my c# code,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports; // Serial stuff in here.

namespace Serial
{
class Program
{
static SerialPort port;
static void Main(string args)
{
int baud;
string name;
string output=String.Empty;
Console.WriteLine(args);
Console.WriteLine(“Welcome, enter parameters to begin”);
Console.WriteLine(" “);
Console.WriteLine(“Available ports:”);
if (SerialPort.GetPortNames().Count() >= 0)
{
foreach (string p in SerialPort.GetPortNames())
{
Console.WriteLine(p);
}
}
else
{
Console.WriteLine(“No Ports available, press any key to exit.”);
Console.ReadLine();
// Quit
return;
}
Console.WriteLine(“Port Name:”);
name = Console.ReadLine();
Console.WriteLine(” ");
Console.WriteLine(“Baud rate:”);
baud = GetBaudRate();

Console.WriteLine(" “);
Console.WriteLine(“Beging Serial…”);
BeginSerial(baud, name);
// port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
Console.WriteLine(“Serial Started.”);
Console.WriteLine(” ");
Console.WriteLine(“Ctrl+C to exit program”);
Console.WriteLine(“Send:”);

for (; ; )
{
Console.WriteLine(" “);
Console.WriteLine(”> ");
try
{
port.WriteLine(Console.ReadLine());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

}
}

static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
for (int i = 0; i < (10000 * port.BytesToRead) / port.BaudRate; i++)
; //Delay a bit for the serial to catch up
Console.Write(port.ReadExisting());

}

static void BeginSerial(int baud, string name)
{
port = new SerialPort(name, baud);
}

static int GetBaudRate()
{
try
{
return int.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine(“Invalid integer. Please try again:”);
return GetBaudRate();
}
}
}
}

I am really frustrated with this project. If required, I can upload all connection image. Please help me if you can identify any problem in my code. Once more, this code works fine when I use bulbs, but fails in alarm.

    case 97:     // Received ASCII a

or

    case 'a':     // No comment needed
       Serial.flush();

Block until all pending serial data has been sent. Why the hell do you need to do that?

 if(Serial.available() == 1)

Suppose two characters arrive while the alarm is doing it’s thing. From the on, the Arduino can do nothing. Using >= makes much more sense.

                    port.WriteLine(Console.ReadLine());

Send a letter and a carriage return and a line feed. While the letter is being dealt with, two more characters arrive. Well, that explains why the Arduino “hangs”.

            for (int i = 0; i < (10000 * port.BytesToRead) / port.BaudRate; i++)
                ;       //Delay a bit for the serial to catch up

This is as stupid on the PC as it is on the Arduino. DO NOT DELAY WAITING FOR SERIAL DATA TO “CATCH UP”. Serial data is SUPPOSED to be asynchronous.

Thnx bro Paul for such kind reply. I modified my code according to ur suggestion but result is same. Both modified codes are,

int received = 0;

void setup() { pinMode(12,OUTPUT); Serial.begin(115200); }

void loop(){ if(Serial.available() >= 1) { received = Serial.read(); switch (received) {

case 'a': // Received ASCII a digitalWrite(12, HIGH); delay(2000); digitalWrite(12, LOW); received=0; break; //Move on

default: digitalWrite(12, LOW); break; } } }

AND c# Code,

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Ports; // Serial stuff in here.

namespace Serial { class Program { static SerialPort port; static void Main(string[] args) { int baud; string name; string output=String.Empty; Console.WriteLine(args); Console.WriteLine("Welcome, enter parameters to begin"); Console.WriteLine(" "); Console.WriteLine("Available ports:"); if (SerialPort.GetPortNames().Count() >= 0) { foreach (string p in SerialPort.GetPortNames()) { Console.WriteLine(p); } } else { Console.WriteLine("No Ports available, press any key to exit."); Console.ReadLine(); // Quit return; } Console.WriteLine("Port Name:"); name = Console.ReadLine(); Console.WriteLine(" "); Console.WriteLine("Baud rate:"); baud = GetBaudRate();

Console.WriteLine(" "); Console.WriteLine("Beging Serial..."); BeginSerial(baud, name); // port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); port.Open(); Console.WriteLine("Serial Started."); Console.WriteLine(" "); Console.WriteLine("Ctrl+C to exit program"); Console.WriteLine("Send:");

for (; ; ) { Console.WriteLine(" "); Console.WriteLine("> "); try { port.Write(Console.ReadLine()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); }

} }

static void BeginSerial(int baud, string name) { port = new SerialPort(name, baud); }

static int GetBaudRate() { try { return int.Parse(Console.ReadLine()); } catch { Console.WriteLine("Invalid integer. Please try again:"); return GetBaudRate(); } } } }

Error Message after 2 execution,

Can anyone give me a clue?

All works as expected when we use a 100W/200W electric bulb instead of emergency alarm. But when we connect the alarm, after 1 execution, the program hangs, and computer failed to connect to the serial port. To make it work again, I have to replug all power cords.

So, you don’t have a software problem. Why are you looking for a software fix?

I worked hard to debug this problem. I want to pinpoint the problem, so I load the arduino with the example BLINK code so that it ON the alarm for a few seconds automatically without command from pc serial port. This worked absolutely fine. So, I think the hardware is okay, problematic area may be the software.

I appreciate if anybody have any clue.

                try
                {
                    port.Write(Console.ReadLine());
                }

Is the ReadLine() method a blocking method? That is, does it wait for input before returning? If not, you really ought to check that there was something entered before sending it to the Arduino.

If the code works with an LED, it should work with the alarm as well, unless there is a hardware problem. Since you insist that there isn't, I can't help any more. Hopefully, some one else can.