Serial monitor collapse using 2 interrupts

Hello all,
I try to build a RPM meters that work simultaneous with 2 interrupts. the first interrupt by using IR sensor and the other one will be 5V pulse after a voltage divider that I made with 40K and 10K resistors.

the frequency that I want to measure is fast, about 5msec average time between 2 interrupts.

I'm sure the the IR sensor works well and I believe that the other interrupt is ok, I checked with DVM and make sure that the voltage is fine.

by unknown reason for me, the Serial Monitor after about 15 second since start is collapse, the Arduino should handle this speed.

It is important to my project to get the specific accurate time of each interrupt, this is the reason that I print it and use micros function, the numbers 1 and 2 are just to distinguish the interrupts, maybe there are syntex errors, this is because I write it from other computer without upload it to the Arduino.

the code is attach , I will be grateful if you can help me solve the problem.

thank you,
Idan

const byte pin2 = 2;
const byte pin3 = 3 ;
int time1 = 0;

void setup()
{
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(pin2), isr1, RISING);
attachInterrupt(digitalPinToInterrupt(pin3), isr2, RISING);
}

void isr1()
{
detachInterrupt(digitalPinToInterrupt(pin2));//to avoid a new interrupt while the function is running
detachInterrupt(digitalPinToInterrupt(pin3));//to avoid a new interrupt while the function is running

 time1 = micros();
 Serial.println(time1); 
 Serial.println("    1\n");

}

void isr2()
{
detachInterrupt(digitalPinToInterrupt(pin3));//to avoid a new interrupt while the function is running
detachInterrupt(digitalPinToInterrupt(pin2));//to avoid a new interrupt while the function is running
time1 = micros();
Serial.println(time1);
Serial.println(" 2\n");
}

void loop(){
attachInterrupt(digitalPinToInterrupt(pin2), isr1, RISING);
attachInterrupt(digitalPinToInterrupt(pin3), isr2, RISING);
}

You know when we tell you not to do serial I/O in interrupt context?

Detach interrupt does NOT stop interrupts. It merely postpones the until interrupts are enabled again.
Paul

thank you for the answer.
I am sorry but I didn't understand what you mean.

this is what I tried to get, make sure that while the function isr1 and isr2 are running all the function lines will complete and another interrupt will not disturb it.
then it will go to the void loop again and if another interrupt will happen it will possible to go again to isr1 or isr2

Start here

Interrupts are disabled while an ISR is in progress; no need to do funny stuff yourself.

===

Please edit your opening post, select all code and click the </> button to apply code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

  1. Never do I/O from an interrupt
  2. Even if you could do I/O from an interrupt you would destroy your timing because every time you print a single character it takes a millisecond at 9600.
  3. It would be better to count pulses for a period of time and calculate the frequency. Just put a counter in the ISR routine and do the calculation in loop(). A 5ms period is actually not fast, only 200HZ.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.