After interrupt function unexpected value

Yeah im shore its an easy problem somehow, but i am a shamed of how long im trying to fix it…

I am counting pulses from a kilwatthour meter, it works like a charm. 8)

But it waits untill the next pulse to update the “wattage” on the lcd. (logical)
So if theres a load of lets say 1500watt and you unplug that (so no load) than the lcd will display 1500watt forever…

So all that is needed is to count if the current time waiting (pulseWait) on a new pulse is longer than the last time “between” the pulses.

Here is the code that doesn’t have a problem and still needs the function:

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 6);



//Number of pulses, used to measure energy.
long pulseCount = 0;   

//Used to measure power.
unsigned long pulseTime,lastTime;

unsigned long between,current,pulseWait;


//power and energy
double power, elapsedkWh;

//Number of pulses per wh - found or set on the meter.
int ppwh = 1; //1000 pulses/kwh = 1 pulse per wh

int watt;  


void setup()
{
    analogWrite(10, 200);
  pinMode(13, OUTPUT);

  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);

    lcd.setCursor(4, 0);
  lcd.print("w");
    lcd.setCursor(13, 0);
  lcd.print("w");
    lcd.setCursor(4, 1);
  lcd.print("w");
    lcd.setCursor(13, 1);
  lcd.print("Kwh");
  
  
Serial.begin(9600);

// KWH interrupt attached to IRQ 0 = pin2
//attachInterrupt(0, onPulse, FALLING);

attachInterrupt(0, checkPulse, FALLING);

}


void loop()  
{
  
current = micros();

pulseWait = current - pulseTime;  // all to count how long its waiting on a new pulse


if (between > pulseWait)  // this i added to make visual if it changes when i want it to, and it does.. 
{
    delay(55);
lcd.setCursor(0, 1);
lcd.print("neej");
}
else 
{
    delay(55);
lcd.setCursor(0, 1);   // but... problems start when i change this to: (0, 0)   the whole display goes blanc at some point.
lcd.print("  JA");
}
  
}


// The interrupt routine    
void checkPulse()
{

  
//used to measure time between pulses.
lastTime = pulseTime;     
pulseTime = micros();

//pulseCounter
pulseCount++;

between = pulseTime - lastTime;

//Calculate power
power = (3600000000.0 / between)/ppwh;



//Find kwh elapsed
elapsedkWh = (1.0*pulseCount/(ppwh*1000)); //multiply by 1000 to pulses per wh to kwh convert wh to kwh

//Print the values.
Serial.print(power,4);
Serial.print(" ");
Serial.println(elapsedkWh,3);



watt = (int) power;     // cast >convert   Print watt to> lcd

delay (100);
lcd.setCursor(0, 0);  // 
lcd.print("    w");

if (watt > 999)
{
lcd.setCursor(0, 0);  // 
}
else if (watt > 99)
{
lcd.setCursor(1, 0);  // 
}
else 
{
lcd.setCursor(2, 0);  // 
}

lcd.print(watt);



}

The problem in this code is that after the interrupt it executes the “else” part in the loop, overwriting the new wattage written to the lcd. why does it do that? is “between < pulseWait” somehow really true? (it just does it once, nomatter what if’s i make up)

The weird thing is if i dont overwrite the positions on the lcd where the wattage is displayed (0, 0) all works as acpected as in the code above…

If anyone would like to shine his light on this it would be nice, thanks in advance!

Interrupt service routines need to be fast. Yours is anything but.

You should not do serial output in an ISR, because that relies on interrupts happening, and interrupts don't happen during an ISR.

You most certainly can not delay() during an ISR.

Writing to the ssslllooowww lcd during an ISR is also not advised.

Thanks, i will try again.

worked with a KWH meter some time ago - http://www.arduino.cc/playground/Main/EEM12L-32AKWhMonitoring -