How to disable/enable only pin change interrupt - robot?

Hello all!

I'm making a robot with Mega 2560 that stores the path into SDCard.
I have also two encoders on the wheels - left and right (pins 2 and 18).
The problem is that if an interrupt from encoders occurs during the SDWrite with the time the file corrupts.

I wanna disable the interrupts from both pins, perform the write on the SD and enable again. No problem if I lost some pulses.

Can anyone help me?

Thanks!

LeoTakahatata:
Can anyone help me?

A question without code can only really be answered by an answer without code.

You have to disable the interrupts while you are writing to the SD card. So turn the pin change interrupts off, write to your SD, and turn the pin change interrupts back on.

How did you turn them on in the first place? Did you hit the bits in the register to enable them or are you using one of the libraries for working with pin change interrupts?

Why don't you use the hardware interrupts? Then you can just detachInterrupt and attachInterrupt in the places you want to turn them off and on.

Thanks man, this is the code:

Initialization:

  attachInterrupt(digitalPinToInterrupt(19), encoderR, RISING);
  attachInterrupt(digitalPinToInterrupt(3), encoderL, RISING);

Scope:

void encoderR()
{
  if (digitalRead(18))
    rightEncCount++;
  else
    rightEncCount--;
}

void encoderL()
{
  if (digitalRead(2))
    leftEncCount++;
  else
    leftEncCount--;
}

Oh, so you are using hardware interrupts. Just call detachInterrupt on them before you write to your SD card. When the SD card writing is finished, call attachInterrupt with them again to turn them back on.

Partial code get's partial code:

detachInterrupt(19);
detachInterrupt(3);

  // write to SD card
 
attachInterrupt(digitalPinToInterrupt(19), encoderR, RISING);
attachInterrupt(digitalPinToInterrupt(3), encoderL, RISING);

Many thanks Delta_G, exactly what I needed!!!

Note that often "pin change interrupt" refers to the 24 pins that share 3 ISRs controlled by
the pin change interrupt registers, not INT0 and INT1, which are just the interrupt pins.

The normal interrupts can be disabled by bits in the EIMSK register in the ATmega series,
as well as via the attach/detachInterrupt methods.

Delta_G:
You have to disable the interrupts while you are writing to the SD card.

Why?

jboyton:
Why?

Because he said it was corrupting his data. Presumably the interrupt is changing something that he's writing while he is writing it. Or maybe his ISR is too long and is messing up the timing of the SD. He hasn't shown us all the code so all I can go on is what he said. If he'd show his code the fix may be simpler. But I can't make them show the code. I asked.

Delta_G:
Because he said it was corrupting his data.

I see.

I thought you were making a more general statement. It's my understanding that SdFat is very tolerant of interrupts. Basically, SPI doesn't care if you take a long time. And the delays in SdFat only require a minimum amount of time. If you increase it with interrupts it still works.

I suspect the (invisible) code is poorly written.