microcontroller resetting and hanging problem
It is possible that the power supply for your ATmega is unable to supply enough power during those sparkling times when you turn your AC load on.  If the power to the processor drops to below a certain voltage, it will reset. You want to add a big electrolytic capacitor between the Vcc and Gnd pins: you probably have two Vcc and two Gnd pins; add a capacitor for both.

I don't know how you are controlling your AC device: is it possible that some of the line voltage is reaching your ATmega?  That would be bad.

Also as Rick_2047 notes, make sure you are not pulling more than 40mA from each one of your ATmega pins, and even 40mA is a bit much.  If you need to drive loads more than 20mA consider using transistors or FETs. If you are using a relay to switch your AC load it likely needs a decent amount of power, so use a transistor.

Finally, if you are using a relay, you need to snub the transient voltages generated when you turn it off. There needs to be a diode in parallel with the relay contacts to safely handle the transient voltages.