Pause increment count with external interrupt

Hello,

I tried many ways to pause or stop an increment count by using an external interrupt . But no method that I could find online could help me. Am I setting this up wrong?

int count = 0;

void setup() {
 Serial.begin(9600);

attachInterrupt(2, StopCount, FALLING);

}

void loop() {


 Serial.print("Count: ");
 Serial.println(count++);
 if(count > 150)
 count = 0;
 delay(6000);
}



void StopCount(){
  exit(0);
}

You can’t exit() in a microcontroller! The program runs forever.

Set a volatile bool variable in the ISR, check that in the code?

int count = 1;
volatile bool counting = true;

void setup() {
 Serial.begin(9600);
attachInterrupt(2, StopCount, FALLING);
}

void loop() {
Serial.print("Count: ");
 Serial.println(count++);
 if(counting == true and count > 150)
 count = 0;
 delay(6000);
}

void StopCount(){
  counting = false;
}

Please don't litter your code with blank lines. Why do you initialize count to 1, and then initialize it to 0 instead in loop()?

You are incrementing count outside the guard.

bionicnano:
What do you mean by outside the guard?

You test your counting flag to see if the count should be reset but not to see if it should be incremented.

So it should count forever and it should reset the count if it goes over 150 and the interrupt has fired at least once.

Since you never set counting back to true with any sort of logic, the whole thing depends on the interrupt firing once. In that sort of situation you need to make sure you clear the interrupt flag in the EIFR register (by writing 1 to the flag) before you attach the interrupt or it may fire once as soon as you attach.

For Interrupt 0 (on Pin 2)

EIFR |= (1 << INTF0);

No, not even close.

I said to add that line BEFORE you attach the interrupt. Can you find the line in your code that attaches the interrupt? Based on the name of the function it should be blindingly easy.

You also still have the count incrementing in the Serial print statement and that is not under the control of the if statement, so it's still going to count no matter what. If you want that to depend on the if statement then you have to put it inside the block of code that goes with that if statement.

You got the interrupt clear in the right spot. Do you understand what that does? Do you know why it needs to be there? Do you understand that most of the time it won't need to be there? Do you know why this time is special? I don't want to give you a bunch of lines that you are just going to blindly copy because that one can mess you up if you don't know what it is.

But you still have it counting no matter what. The only thing you have under the control of the if statement is the reset of the counter. So as written now it will count forever and when you press the button it will reset to 0 and start only counting to 150 but still counting non-stop.

Is that your desired behavior?

bionicnano: I want the counter to stop incrementing when the interrupt is triggered and display whatever the digit is when the interrupt was triggered.

If you could kindly show me where to insert the line of code that you suggested.

OK. You're so far from understanding that I don't know what to do. Please read this carefully. If you don't follow then re-read until you do.

  1. That line I had you enter was to prevent the interrupt from firing right when you first attach it. With the way you have your code written, your interrupt is a once-and-done deal. So it was firing before you pressed the button. That's what that line I gave you was about. It has nothing to do with your counter.

  2. You have an if statement in your loop. The things inside that if statement only happen when the counting variable is true. The things outside that if statement happen all the time. Can you find the line that increments the count and prints it? Is that line inside the if block or outside the if block? Look at the { and }, do you know what they're for? If not then stop and go learn that first, you can't write code without knowing that.

  3. When your interrupt fires it sets the counting variable to false. At that point everything inside the if block will stop happening. Right now, what is inside the if block? Do those lines increment and print the count? What do those lines in there do? That's the thing that will stop happening when you press the button because counting will be false so that if statement will be false and won't run.

All the stuff that is outside of the if block will run all the time. The interrupt won't stop it. No magic line of code will make that happen. If you want something to stop when the interrupt fires and the counting variable goes false, then that thing needs to be INSIDE the block controlled by the if statement. It can't be outside of it.