Timer interrupt and LiquidcrystalI2C

Hi Folks

I am working on a little project and stumpled into a problem:

I have a interrupt reading whenever a RISING is happening on pin 2. Counting theese and when my timer interrupt tells to it calculates the rpm of an engine.

When printing to Serial, the code works perfect. But I want to see the result on a 16X2 lcd (http://www.banggood.com/IICI2C-1602-Yellow-Green-Backlight-LCD-Display-Module-For-Arduino-p-950728.html).

And here the problem comes !

In the code I can init the display (and still see results in serial monitor when debugging), but as soon as I start writing to the display nothing happens and everything stops ?

Any suggestions ?

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

int counts =0;

void setup(){
 // Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  
cli();//stop interrupts

  TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
  TCNT1  = 0;//initialize counter value to 0
  // set compare match register for 1hz increments
  OCR1A = 7812;// = Interrupt interval
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS12 and CS10 bits for 1024 prescaler
  TCCR1B |= (1 << CS12) | (1 << CS10);  
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);

sei();//allow interrupts

attachInterrupt(digitalPinToInterrupt(2),calc,RISING);

}//end setup

ISR(TIMER1_COMPA_vect){
detachInterrupt(2);

lcd.setCursor(1,1);
lcd.print("       ");
lcd.setCursor(1,1);
lcd.print(counts*2*60);
/*
Serial.print(counts*2*60);
Serial.println(" RPM");
*/
counts=0;
attachInterrupt(digitalPinToInterrupt(2),calc,RISING);
}
  
void calc(){
  counts++;
}

void loop(){

  //do other things here
}

but as soon as I start writing to the display nothing happens and everything stops ?

I2C uses interrupts. Interrupts are disabled while your ISR is running. Writing to the LCD is waiting for its interrupt to be handled, but it never will be, because interrupts are not happening.

Do not write to the LCD in your interrupt handler.

There is no reason to keep attaching the interrupt handler.

Thats it. Perfect, Thanks :slight_smile:

I moved the lcd.print to the loop and now it works.