Changing global variable during an interrupt

Hello, I am trying to find the time elapsed between interrupts using a stopwatch library.

I have a stopwatch running and when interrupted it stores the current time into the elaspedTime variable and then resets the stopwatch. I control the interrupts by touching pin 20 to ground. The stopwatch's time is displayed on the first line of my LCD and the elaspedTime variable is displayed on the second line.

The stopwatch's time increments correctly every second and resets when I touch pin 20 to ground so we know the interrupt is occurring. The problem is that elapsedTime doesn't update correctly during the interrupt and always displays 0.

Looking at previous threads, it seems that I should declare elapsedTime as volatile which I have done.

Any suggestions would be greatly appreciated and I will try to provide any relevant information!

Running on a Mega 2560

#include <LiquidCrystal.h>
#include <StopWatch.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);           // select the pins used on the LCD panel
#define LCD_BACKLIGHT_PIN         3  // D3 controls LCD backlight
int InterruptPin = 20;
volatile unsigned long elapsedTime;
StopWatch MySW;

void voltageChange(){
  elapsedTime = MySW.elapsed();
  MySW.stop();
  MySW.reset();
  MySW.start();
} 

void setup(){
  digitalWrite( LCD_BACKLIGHT_PIN, HIGH ); // Turn the backlight on
  pinMode(InterruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(InterruptPin), voltageChange, RISING);
  lcd.begin(16, 2);               // start the library
  lcd.setCursor(0,0);             // set the LCD cursor   position 
  MySW.start();                   // Start the stopwatch
  elapsedTime = 0;
}
  
void loop(){
lcd.clear();
lcd.print(MySW.elapsed());
lcd.setCursor(0,1);
lcd.print(elapsedTime);
delay(1000);
}

Hi,

Which Arduino are you using? On an Uno (or Pro Mini) the external pin-change interrupts are pins 2 and 3.

best,
Michael

Edit: A follow-on thought. You use CHANGE in attachInterrupt. Your interrupt will be triggered when you connect to ground and again when you unconnect, resetting in between. Might be better to use FALLING.
--M

I'm running it on a Mega 2560, I'll update the top post! Mentioning change interrupts made me realize that I only want an interrupt on a rising voltage instead of changing. Thanks for the help! However the timeElapsed variable still doesn't update correctly.

Edit: Using either FALLING or RISING results in the same behavior as CHANGE. It resets the stopwatch when I both connect and disconnect pin 20 to ground.

I'm unclear on why you want rising instead of falling. But either way, you will want a pullup/pulldown resistor on that pin (either declared in setup or an actual resistor) to keep it from floating.

Don't know if that's the specific problem you're posting about, though. Is the variable still printing as zero after fixing "CHANGE"?

best,
Michael

My interrupt pin is assigned as a pullup in setup(), I don't think it would matter which I chose but FALLING would probably be more logical. The variable will randomly print a non-zero value for a brief moment and then return to zero when I connect or disconnect the interrupt pin however I'm unable to replicate this repeatedly.

On further inspection it appears that my code is working as intended, I believe the problem is that I've manually been touching a jumper cable to the ground pin and I must be bumping it multiple times which resets the stopwatch rapidly. When I'm very careful about touching it the program works as intended. I'll have to test it without manually making and breaking the connection.

Thanks for your insight with FALLING vs. CHANGE Michael! I'll keep you posted!

Glad to be of help.

--M

mjszekely:
On further inspection it appears that my code is working as intended, I believe the problem is that I've manually been touching a jumper cable to the ground pin and I must be bumping it multiple times which resets the stopwatch rapidly. When I'm very careful about touching it the program works as intended. I'll have to test it without manually making and breaking the connection.

Thanks for your insight with FALLING vs. CHANGE Michael! I'll keep you posted!

Common problem. Google debounce a switch.

Or, if you prefer not to insult your switch, debounce it.

(Couldn't resist)

--Michael

mjward:
Hi,

Which Arduino are you using? On an Uno (or Pro Mini) the external pin-change interrupts are pins 2 and 3.

You are confusing external interrupt pins (2 & 3 on the Uno) and pin-change interrupts which
are another thing altogether and cover every pin on the Uno grouped by port.

Thanks, yes, my terminology was sloppy.
--Michael