Problems with arduino attachInterrupt and serial port?

hello
i whant to do a phase control that can be control by the serial port so i made up this program

int AC_LOAD = 8; // Output to Opto Triac pin
int dimming = 115; // Dimming level (0-128) 0 = ON, 128 = OFF
void setup()
{
Serial.begin(9600);
pinMode(8, OUTPUT); // Set the AC Load as output
attachInterrupt(1, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}
void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
// Firing angle calculation :: 50Hz-> 10ms (1/2 Cycle)
// (10000us - 10us) / 128 = 75 (Approx)
int dimtime = (64*dimming);
delayMicroseconds(dimtime); // Off cycle
digitalWrite(AC_LOAD, HIGH); // triac firing
delayMicroseconds(10); // triac On propogation delay
digitalWrite(AC_LOAD, LOW); // triac Off
}
void loop()

{
}
void serialEvent()
{
detachInterrupt(1);
dimming=Serial.read();
Serial.println(dimming);
attachInterrupt(1, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}
in this program i used "Serial Event" to change the value of "dimming" the issue is that when i change the value in the serial monitorthe arduino junps to ten and it wont accept another number.
i have tried with "parseInt", "SerialEvent", "Serial Read" disabling interrupts and none of them worked
somebody please can help me?

I cannot see where are you calling serialEvent(). The function is defined but I didnt find a call in the routine.

mart256:
I cannot see where are you calling serialEvent(). The function is defined but I didnt find a call in the routine.

serialEvent() is special...

angeld:
in this program i used "Serial Event" to change the value of "dimming" the issue is that when i change the value in the serial monitorthe arduino junps to ten and it wont accept another number.
i have tried with "parseInt", "SerialEvent", "Serial Read" disabling interrupts and none of them worked
somebody please can help me?

this will only read one byte at a time from serial buffer:

dimming = Serial.read();

you will need to parse the input if you want to set dimming from the serial monitor to a number with more than one digit

BulldogLowell:
Serial.event() is special...

Wow, thanks for the info.

You can't use delay, of any kind in an ISR.

Mark

You can't use delay, of any kind in an ISR.

You can't use delay(), but you can use delayMicroseconds(). That doesn't mean that you should, but, in the case of triggering a triac at the proper time after the zero crossing happens, it is necessary.

OP: You could use Serial.parseInt() to read values that are more than one digit long. Using some kind of delimiter after the value will minimize the time parseInt() blocks waiting for the end of the integer to arrive. The CR, LF, or both that the Serial Monitor can add are ideal delimiters.

I'll take a look at delayMicros() it's interesting to hear that it will work when all interrupts have been disabled.

For PaulS and the OP here is a kink to atmel data sheet on zero crossing detection which does NOT use a delay. The mehod works at "mains" voltages (US and UK) without any need to reduce the mains voltage 240VAC direct to a digital pin!

Mark

holmes4:
… The mehod works at “mains” voltages (US and UK) without any need to reduce the mains voltage 240VAC direct to a digital pin!..

Wow, that would definitely let the smoke out. You failed to mention the 1 megohm resistors in series with the two mains connections thereby limiting the current and ultimately the voltage that appears on the AVR’s input pin.

Personally, I would never use such a down and dirty approach to zero crossing detection. An opto coupler and a few discreet devices is all it takes to do it safely.