Falling Edge Detection Using Interrupt Doesn't Work

I created a simple program for my homework that involves pushing a button which, after being released should do a falling edge function. I used the attachInterrupt() function.

But, after I uploaded it into my Arduino Uno R3, I pressed the button, only to see the message on my Serial Monitor telling me that it detected a falling edge, which shouldn't happened because I didn't program a rising edge function.

Any help would be appreciated, Thanks!

const byte interruptPin = 2;
bool runOnce = false;

void setup() {
  Serial.begin(9600);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), doFalling, FALLING);
  Serial.println("Waiting for input...");
}

void loop() {
  int checkPin = digitalRead (interruptPin);
  if (checkPin == HIGH and runOnce == false){
    Serial.println ("Button Pressed");
    runOnce = true;   
  }
  if (checkPin == LOW and runOnce == true){
    Serial.println ("Button Released");
    runOnce = false;
  }
  
}

void doFalling() {
  Serial.println ("Falling Detected");
}

sketch_dec02a.ino (621 Bytes)

Serial Monitor Output.PNG

Read the guiding advice at the top telling how to send qurstions, how to attach code and other information.
I can't download Your code so I can't help You now.
Also check Your text regarding falling and raising edges. It makes no sence.

OP's code.

const byte interruptPin = 2;
bool runOnce = false;

void setup() {
  Serial.begin(9600);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), doFalling, FALLING);
  Serial.println("Waiting for input...");
}

void loop() {
  int checkPin = digitalRead (interruptPin);
  if (checkPin == HIGH and runOnce == false){
    Serial.println ("Button Pressed");
    runOnce = true;   
  }
  if (checkPin == LOW and runOnce == true){
    Serial.println ("Button Released");
    runOnce = false;
  }
  
}

void doFalling() {
  Serial.println ("Falling Detected");
}

First thing that has to be sorted is the use of a button.
If you use INPUT_PULLUP, then you make the pin "normally HIGH".
You must then connect the button between pin and GROUND.
A button press forces the pin LOW.
Now look at your if() statements.

Read this why you have to use && instead of AND in your if() statements.

Seems you only use interrupts for printing one line, not for the actual button state.
You should not print inside an interrupt routine (takes too long).
You shouldn't even use interrupts for human button presses, unless it's for educational purposes.
Leo..

So I can't use interrupt to do falling edge detection? Is there another way to do a falling edge detection without using interrupt?

With bouncing buttons you'll get many unwanted interrupts, a very stupid (nOOb) idea. Use proper debounce code and forget about interrupts, see Debounce or StateChangeDetection examples.

So I can't use interrupt to do falling edge detection?

Of course you can. But using interrupts introduces many new problems that almost always outweigh the advantages.

Use polling and state change detection to avoid those problems.

DrDiettrich:
With bouncing buttons you'll get many unwanted interrupts, a very stupid (nOOb) idea. Use proper debounce code and forget about interrupts, see Debounce or StateChangeDetection examples.

thankyou master....

Hi,
Welcome to the forum,

Did you fix the && instead of and in your if statements?
How have you got your switches wired?

Tom... :slight_smile:

Did you fix the && instead of and in your if statements?

I do not believe that this is an issue. "and" is a valid alternative operator for &&
https://en.cppreference.com/w/cpp/language/operator_alternative

The logic of the isr, and the logic of the conditionals are not consistent with each other and the INPUT_PULLUP pin mode.

I pressed the button, only to see the message on my Serial Monitor telling me that it detected a falling edge, which shouldn't happened because I didn't program a rising edge function.

If the button were wired correctly between the interrupt pin and ground, then the interrupt should be triggered on a press. The Serial prints based on digitalRead() if the interrupt pin are not consistent with pressed and released transitions.

Use of "and" instead of "&&" is fine.

This logic is wrong, because if the (correctly wired) button is pressed, the pin will read LOW. Likewise for the next if clause.

  if (checkPin == HIGH and runOnce == false){
    Serial.println ("Button Pressed");