serial commmunication while attaching interrupt

I use two arduino boards,a and b, B output an frequency signal connect with A’s interrupt pin, then A read the signal as attachInterrupt.

When A attachInterrupt and calculate the RPM of interrupt signals, ouput another frequency signal according to RPM. it maps RPM to its EEPROM value.

example:
RPM=0~1000 read the value of EEPROM address 0, ouput another signal value times

if EEPPROM address 0 =3 and RPM=500, arduino A will output another signals 3 times before next interrupt coming.

if EEPPROM address 1 =2 and RPM=1500, arduino A will output another signals 2 times before next interrupt coming.

if EEPPROM address 2 =1 and RPM=2500, arduino A will output another signals 1 times before next interrupt coming.

key point: While arduino A is processing these action in loop(), it also use Serial.available() to received the C++ program’s command to modify eeprom value.

now i have problem , when the interrupt RPM goes higher and higher, the eeprom modify request always wrong , and receive error data or lost some serial data. But it receives data from c++ program and modify the eeprom correctly while using pushbutton to use detachInterrupt() to stop receiving the RPM interrupt. So I add the detachInterrupt() at the initial of the Serial.available() block,then add the attachInterrupt() after receiving data and modifying EEPROM to resume the interrupt,but it is still not well.

Source code snippet

Arduino A:
void setup()
{
attachInterrupt(0,interrupt,RISING);//B’s signal connect to pin 2
}
void interrupt()
{
…acc the interrupt counts and record the interrupt interval time

for(int i=0;i<EEPROM.read(RPM/1000);i++)
{//map RPM to EEPROM address every 1000 RPM
//then ouput signal X times while X stored in EEPORM.read(address)
digitalWrite(outputPin,HIGH);
delayMicrosecond(1000);
digitalWrite(outputPin,LOW);
delayMicrosecond(1000);
//note: delay() is not available in interrupt function,but delayMicrosecond() does.
}
}
void loop()
{
…calculate RPM here according to the function’s interrupt count and interval.

if(Serial.available()>0)
{//receive the serial data send from c++ program
… decode the data receive c++ program eeprom modify commad
…modify the eeprom
}
}

It comes as no surprise you are having these problems you are simply trying to do too much. The EEPROM writing takes quite a long time and locks out the rest of the processor. I think your best bet is if you try and come up with a less heavy solution to what ever problem you are looking to do.

thanks for reply.
but i add some code to stop attach interrupt while writing EEPROM.
but still the same situation, why???
Arduino A:
void setup()
{
attachInterrupt(0,interrupt,RISING);//B’s signal connect to pin 2
}
void interrupt()
{
…acc the interrupt counts and record the interrupt interval time

for(int i=0;i<EEPROM.read(RPM/1000);i++)
{//map RPM to EEPROM address every 1000 RPM
//then ouput signal X times while X stored in EEPORM.read(address)
digitalWrite(outputPin,HIGH);
delayMicrosecond(1000);
digitalWrite(outputPin,LOW);
delayMicrosecond(1000);
//note: delay() is not available in interrupt function,but delayMicrosecond() does.
}
}
void loop()
{
…calculate RPM here according to the function’s interrupt count and interval.

if(Serial.available()>0)
{//receive the serial data send from c++ program
detachInterrupt(0);
… decode the data receive c++ program eeprom modify commad
…modify the eeprom
attachInterrupt(0,interrupt,RISING);
}
}

for(int i=0;i<EEPROM.read(RPM/1000);i++)
 {//map RPM to EEPROM address every 1000 RPM
   //then ouput signal X times while X stored in EEPORM.read(address)
   digitalWrite(outputPin,HIGH);
   delayMicrosecond(1000);

You’ve got a two millisecond delay in an interrupt service routine? :o

What range of RPM values are you dealing with? It might be necessary to read all the EEPROM values into a global array in setup(), and then use (and modify) those values in loop() and/or the ISR, rather than trying to read the values in the ISR.

And the delay in the ISR is clearly not a good thing.

What range of RPM values are you dealing with? from 0~9999 as below 0 ~999 read EEPROM address 0 1000~1999 read EEPROM address 1 2000~2999 read EEPROM address 2 3000~3999 read EEPROM address 3 4000~4999 read EEPROM address 4 5000~5999 read EEPROM address 5 6000~6999 read EEPROM address 6 7000~7999 read EEPROM address 7 8000~8999 read EEPROM address 8 9000~9999 read EEPROM address 9 It might be necessary to read all the EEPROM values into a global array in setup(), and then use (and modify) those values in loop() and/or the ISR, rather than trying to read the values in the ISR. Thanks for the suggestion of dealing with global array instead of EEPROM.Read() in the ISR or loop. In the actual experiment, EEPROM.Read() is ok to not delay to load the RPM mapping value,but the EEPROM.Write() action command which recived from software is often unavailable. I think it may cause to the arduino is handling the interrupt while received serial data. this is tough to deal with.

And the delay in the ISR is clearly not a good thing. The purpose i want to use delay as it is that i want to output another digital HIGH signal for X times (X refers to mapping value) every ISR,is like the ignition spark modification system. Ignition is high and more powerful when igniting more than one time every revolution of engine.