Interrupts "Arduino"

Hey Guys,
I´m not a good english student, but i´ll try my best.

I have got a problem with interrupts. --> they work that´s not the problem

The problem is that in my interrupt routine a Serial.println is. When the interrupt trigger the interrupt i get two times the output of it. why?

My attachInterrupt is:

void top()
{ state=1;
  digitalWrite(13, state);
  Serial.println("T");
}

Please post your complete sketch.

Interrupt routines need to be as short as possible. increment a counter set a flag or so but not somethins as slow as a serial print which uses interrupts. As Arduino is single threaded it cannot handle it.

Solution: set a flag and do the printing in the main loop if the flag is set.

(and your English is fine!)

I have got a Arduino Mega 2560. How do I set a Flag?

int Pin = 2; 

volatile int state = LOW;

void setup()
{
  Serial.begin(9600);
  pinMode(Pin, INPUT);
  pinMode(ledPin, OUTPUT);
  attachInterrupt(0, top, CHANGE);
  attachInterrupt(1, low, CHANGE);
}
void loop()
{ 
}

void top()
{ state=1;
  digitalWrite(13, state);
  Serial.println("T");
}
void low()
{ state=0;
  digitalWrite(13, state);
  Serial.println("L");
}

When the interrupt trigger the interrupt i get two times the output of it. why?

Because attachInterrupt(0, top, CHANGE);

Sure I forgot... you are right ]:slight_smile: RISING

sorry for the question. stupid question. :frowning:

// not tested

int Pin = 2; 

volatile int state = LOW;
volatile boolean stateChanged = false;;

void setup()
{
  Serial.begin(9600);
  pinMode(Pin, INPUT);
  pinMode(ledPin, OUTPUT);
  attachInterrupt(0, top, RISING);
  attachInterrupt(1, low, RISING);
}

void loop()
{ 
  if (stateChanged) 
  {
    staeChanged = false;
    if (state == 1)  Serial.println("T");
    if (state == 0)  Serial.println("L");
  }
}

void top()
{ 
  state=1;
  stateChanged = true;
  digitalWrite(13, state);
}

void low()
{ 
  state=0;
  stateChanged = true;
  digitalWrite(13, state);
}