Go Down

Topic: Communication to I2C LCD 'breaks' timer interrupt (Read 1 time) previous topic - next topic

I'm trying to do a project where several aggregated data sources are displayed on a LCD at regular intervals (500ms). I want to use a timer which breaks out of the main program loop to access the LCD to display this data. The strange thing is that it works OK to have the timer interrupt blink a LED on pin 13 at regular intervals, or have the timer interrupt print something on the serial output.
However as soon as I put a LCD function (lcd.print, lcd.clear, or anything else) the program 'freezes'.
I'm using the TimerThree library on an Arduino Mega. Below the bare minimal code which reproduces the problem:

Code: [Select]
#include <TimerThree.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,20,4);

void setup()
{
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  pinMode(13,OUTPUT);
  Timer3.initialize(500000);
  Timer3.attachInterrupt(updateLCD);
}
void updateLCD()
{
  digitalWrite(13,digitalRead(13)^1);
  Serial.println("Timer");
  lcd.setCursor(0,0);  //when inserted this line no more blinking of LED and Serial output
}
void loop()
{
}


Any suggestions?

I've been playing around with this timer interrupt thing related to I2C LCD for a while now. Finally got it going. It roughly looks like this:

Code: [Select]

int    varneedLCD=0;

void setup()
{
  Timer1.initialize(1000000);
  Timer1.pwm(9,512);
  Timer1.attachInterrupt(needLCD);
}
void loop()
{
  if (varneedLCD==1){updateLCD();varneedLCD=0;}
}

void needLCD()
{
  varneedLCD=1;
}

void updateLCD()
{
  //do stuff with the LCD
}




As I have read through some forum posts it appears to have something to do with the fact that you can't to too much processing in the interrupt handler. But what I still don't understand is why it's OK to do this outside the interrupt handler? Be aware that I'm quite new to this interrupt-thing so any dummy-proof explanation might be very welcome ;-)

Now I attached also some interrupts on button input and the user experience is quite snappy along with polling a GPS, Compass and pressure sensor on regular basis.

The problem is solved, but I'm just curious...

Go Up