using interrupts

Hey All! I am completely new to the Arduino community but find its potential fascinating. I am trying to write code to read a water pulse meter using the interrupt and it is doing VERY strange things. This is just the beginning of my code trying to understand how the interrupt input works.....

I am using a delay of 5 seconds to debounce any input from the reed switch; I use two variables VAR1 and VAR2 to tell when an interrupt has been called; When the interrupt routine is called, VAR1 toggles; when VAR1!=VAR2, the IF loop is executed; this DISABLES interrupts and has a 5 second delay; yet I find MULTIPLE INTERRUPTS continuing to occur during this time as evidenced by multiple monitor reads of the GALLONS variable. Can someone look and see if there is anything obvious I am missing in my code? Thanks!

/*
Water Meter reading program
*/

//
//First define variables:
int LEDpin=13; //output led
int VAR1=HIGH;
int VAR2=HIGH;
float gallons=2.64;
//
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(LEDpin, OUTPUT);
digitalWrite(LEDpin,LOW); //initialize LED
attachInterrupt(0, MeterWrite, FALLING); //signal to pin 2 of arduino
}

// the loop routine runs over and over again forever:
void loop() {
// continually check to see if interrupt has tripped:
noInterrupts();
if (VAR1!=VAR2){
VAR2=!VAR2;
delay(5000);
interrupts();
}
else{
interrupts();
}
}
//Now this is the interrupt routine to write to the LED as well as the OUTPUT:
void MeterWrite(){
digitalWrite(LEDpin,VAR1); //when it changes, change LED
Serial.println(gallons); //serial output to monitor
VAR1=!VAR1; //toggle 'VAR1'
}

I am using a delay of 5 seconds to debounce any input from the reed switch;

5 seconds?

Ask yourself if Serial.println will work in an ISR.

Read this:

.

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the "Code" icon above the posting area. It is the first icon, with the symbol: </>

Also what LarryD said. There are numerous problems with your code.

 noInterrupts();
     if (VAR1!=VAR2){
       VAR2=!VAR2;
       delay(5000);
       interrupts();

Turn off interrupts. Then, call a function that relies on interrupts working. Then, wonder why the function doesn't work. No need to wonder...

All capital letter names are, by convention, reserved for constants. Constants, once initially valued, NEVER appear on the left side of an equal sign.

thank you all; Clearly I have not coded for years and appreciate any help I can get; I now remember about the capital letters being constants; also adjusted code using VOLATILE variables. But still have issues after taking out the serial.println in the ISR...

//First define variables:
  volatile int LEDpin=13;    //output led
  volatile int var1=HIGH;
  volatile int var2=HIGH;
  volatile float gallons=2.64;
  //
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(LEDpin, OUTPUT);
  digitalWrite(LEDpin,LOW);   //initialize LED
  attachInterrupt(0, MeterWrite, FALLING);   //signal to pin 2 of arduino
}
// the loop routine runs over and over again forever:
void loop() {
  // continually check to see if interrupt has tripped:  
     noInterrupts();
     if (var1!=var2){
       var2=!var2;
       delay(5000);
       Serial.println("Interrupt seen!");
       interrupts();
  }
   else{
    interrupts();
   }
}
//Now this is the interrupt routine to write to the LED as well as the OUTPUT:
void MeterWrite(){
  digitalWrite(LEDpin,var1);   //when it changes, change LED
  var1=!var1;     //toggle 'VAR1'

}
noInterrupts();
     if (var1!=var2){
       var2=!var2;
       delay(5000);
       Serial.println("Interrupt seen!");
       interrupts();

You still have the problem of turning off the interrupts and then calling delay which requires a timer interrupt to run. This will hang you up indefinitely.

Your "debounce" does nothing of the kind. In fact, it does nothing useful at all. De-bouncing means making sure an input has been stable for some period of time before acting on any change. All you're doing it delaying the action, without ever making sure the input stabilized. And, if the input glitches an even number of times, so var1 will end up right back here it started, but you will toggle var2, with the result that you'll immediately think the input has changed state again, so your message will print out twice, 5 seconds apart, even if the input has not changed in almost 10 seconds.

Regards,
Ray L.

Why are you using interrupts? They are only for things that repeat in the microseconds or low milliseconds range.
Use polling instead. See the Blink without delay example in the examples page of this site, or in the IDE.
Also see: Demonstration code for several things at the same time - Project Guidance - Arduino Forum

delay() stops your Arduino from doing anything else, until it's timed out.

A spirited discussion! I am just relearning code for a specific purpose; wanted to play with interrupts to see how they worked because when I was an engineer in the 80's did lots of assembly code and it was a familiar term. Interestingly, this does NOT hang me up and that is the big question; if DELAY(5000) hangs up the arduino for 5 seconds, it shouldn't be constantly writing to the monitor....which it does; also, There is a time when the interrupts are enabled (see the 'else' statement) which then create the conditions to satisfy the 'if' statement. Just trying to understand what is going on with this code so I can better understand the workings of the processor..... Thanks all for any input you can give. Ger

if DELAY(5000) hangs up the arduino for 5 seconds, it shouldn't be constantly writing to the monitor....which it does;

You are doing two things wrong there, which happen to cancel out in a way that looks like everything is OK.

Again: Gammon Forum : Electronics : Microprocessors : Interrupts

I discuss in there (reply #10) that timing intervals with delay() is not ideal.