Problem interrupting Serial.write

Hi, I’m writing the code for a project that will interface an arduino with an engine control unit of a car.
Basically I send a byte to the unit and it responses with another byte with the info I want.
The info is printed in a LCD and now I want use some pushbuttons to navigate by the lcd using interrupts.
The problem is that when I trigger the button interrupt while the arduino is doing Serial communication with the unit, seems that all done in the ISR (for example change a variable value) is not executed or something like that.

wrote a simple code where this problem happens, I do my serial stuff in the loop inside the else statement and at the end I write the value of ‘a’, if button is pressed the if statement will notice it and will update the ‘a’ variable.

#include "Wire.h"
#include "LiquidCrystal.h"

LiquidCrystal lcd(7, 8, 12, 11, 13, A0);

volatile int button1 = 0;
int debouncing = 100;
int a = 1;

unsigned int ecuTimeout=10000;
unsigned long last_interrupt_time=0;

void setup() {
  Serial.begin(38400);  
  lcd.begin(20,4); 
  pinMode(2,INPUT);
  digitalWrite(2,LOW);
  attachInterrupt(0, button1ISR, LOW);  
  lcd.clear();  
}

void loop() { 
 
  if (button1 == 1){ //hey! I noticed the button have been pushed    
    a=a+1;
    button1=0;  
  }
  else{
   //here I am doing my standard serial things...    
   Serial.flush();
   Serial.write(0x20);
   unsigned int time=0;
   while(Serial.available() == 0 && time < ecuTimeout) time+=1;
   lcd.print(a);    
  }   
    
}

void button1ISR(){  
  unsigned long interrupt_time = millis();  
  if ((interrupt_time - last_interrupt_time > debouncing)){   
    button1=1;      
  } 
  last_interrupt_time = interrupt_time;
}

This code prints ‘1’ all the time, doesn’t matter you press the button or not, but if I delete Serial.write(0x20); line it works ok, so I assume that something is happening when arduino is doing a serial.write and you interrput it.

any ideas?

The info is printed in a LCD and now I want use some pushbuttons to navigate by the lcd using interrupts.

Why? Why isn't polling fast enough?

PaulS:

The info is printed in a LCD and now I want use some pushbuttons to navigate by the lcd using interrupts.

Why? Why isn't polling fast enough?

This code is only for show my problem, In the real one I do lots of Serial.write() and stuff like that without passing by the loop() in some considerable time so in many cases arduino was too late for read my pulse (pushing the button).

LOW is a really bad choice for the interrupt type. FALLING, if LOW is what pressed means, is a much better choice.

PaulS: LOW is a really bad choice for the interrupt type. FALLING, if LOW is what pressed means, is a much better choice.

you are right, falling is much better interrupt type for what I want, anyway don't know exactly why but this solve the problem. Thanks!

anyway don't know exactly why but this solve the problem.

Most likely because the LOW was causing your interrupt handler and the Serial.write() interrupt handler to be fighting for priority. LOW causes your ISR to be called over and over as long as the pin is LOW, not just once as it transitions to LOW, as FALLING does.

PaulS:

anyway don't know exactly why but this solve the problem.

Most likely because the LOW was causing your interrupt handler and the Serial.write() interrupt handler to be fighting for priority. LOW causes your ISR to be called over and over as long as the pin is LOW, not just once as it transitions to LOW, as FALLING does.

I understand, thanks for a perfect help!!