Problems with serial Read and AttachtInterrupt

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?

edit: Serial.read() only reads a single byte from the serial buffer

have you tried this?

void serialEvent()
{
  detachInterrupt(1);
  dimming=Serial.parseInt();
  Serial.println(dimming);
  attachInterrupt(1, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}

use Serial.setTimeout() in your setup() function to make the time it dwells shorter. The default is 1000ms.

If time matters then you don't want to read all the serial characters ar once. Use code like is in the Arduino sketch in this demo to read the serial chars without waiting.

What sort of data do you want to send to the Arduino?

...R

Use code like is in the Arduino sketch in this http://forum.arduino.cc/index.php?topic=225329.msg1810764#msg1810764 demo to read the serial chars without waiting.
(with the ] in the right place...)

(with the ] in the right place...)

Or even better, with the = in the right place: demo

BulldogLowell:
edit: Serial.read() only reads a single byte from the serial buffer

have you tried this?

void serialEvent()

{
  detachInterrupt(1);
  dimming=Serial.parseInt();
  Serial.println(dimming);
  attachInterrupt(1, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}




use Serial.setTimeout() in your setup() function to make the time it dwells shorter. The default is 1000ms.

thanks¡¡
I will try and send you my results
a question, Is it really necessary to use the Serial.setTimeout ()? or what is the difference if you use this function or not?
and i want to know why it locks the Serial Monitor qhen i used the attachtInterrupt

if you transmit the integer why a trailing non numeric character, it will process rapidly without having to futz with the timeout.

consider adding that to your transmission

curious to know why are you suspending interrupts while serialEvent()?

because I want to control via the serial phase, and thought that if there is an interruption in reading the serial cause any problems, but if I use the Serial.read ocaciona the same mistake that Serial.event but not clear the interruptions

If time matters don't use Serial.parseint() because it blocks until it has got all it needs.

serialEvent() is no different from

void readData() {
   if (Serial.available > 0) {

  }
}

but it has the disadvantage that it is called even though you may not be ready for it - for example you might wish to wait until a number of bytes have been received.

...R

angeld:
because I want to control via the serial phase, and thought that if there is an interruption in reading the serial cause any problems, but if I use the Serial.read ocaciona the same mistake that Serial.event but not clear the interruptions

an interrupt will

-suspend the code that is processing, in your example processing some serial data
-perform your ISR
-return promptly to digest the serial data you are reading

I can't see a reason why that would affect your serialEvent()

it seems to me that you are potentially missing a zero cross event if you suspend the ISR... particularly for the more trivial serialEvent() or any other routine you are running for that matter. I say that in consideration of your code, which appears to be less depending on the serialEvent() compared to the more mission-focused interrupt you are using.

anyways, your code is stuck where it is so may want to consider parsing the serial event in a non-blocking fashion and leave the interrupt to do its job, in the middle of a Serial.read() or not.