0

I want to call some functions at the rising edge and falling edge of a square wave pulses. I used attachInterrupt(digitalPinToInterrupt(interruptPin), udara, RISING) for rising edge and attachInterrupt(digitalPinToInterrupt(interruptPin), blank,FALLING) for falling edge. But I didn`t get the serial outputs of rise and fall conservatively. what is the answer for the problem? My code is written as follows.

enter code here
const byte interruptPin = 2;
void setup() {
 Serial.begin(9600);
 pinMode(interruptPin, INPUT);
}
void loop() {
 attachInterrupt(digitalPinToInterrupt(interruptPin), udara, RISING);
 attachInterrupt(digitalPinToInterrupt(interruptPin), blank, FALLING);
}
void udara() {
 Serial.println("rise");
}`
void blank() {
 Serial.println("fall");
}
Cœur
39k25 gold badges206 silver badges281 bronze badges
asked May 1, 2018 at 12:22
2
  • Why are you attaching in 'loop()'? Wouldn't it make more sense to attach once in 'setup()'? Commented May 1, 2018 at 12:56
  • Better code formatting. Commented May 1, 2018 at 21:53

2 Answers 2

1

The attachInterrupt() should be part of setup(), not the loop(), as it is used to setup the event trigger and callback.

const byte interruptPin = 2;
void setup() {
 Serial.begin(9600);
 pinMode(interruptPin, INPUT);
 attachInterrupt(digitalPinToInterrupt(interruptPin), udara, RISING);
 attachInterrupt(digitalPinToInterrupt(interruptPin), blank, FALLING);
}
void loop() {
}
void udara() {
 Serial.println("rise");
}
void blank() {
 Serial.println("fall");
}
answered May 1, 2018 at 13:16
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the answer. Since there is nothing in the loop the code runs only ones and the output can be seen as only one rise and continuous falls. I need the answer as rise, fall, rise, fall likewise. What are the modifications?
0

Serial uses interrupts to push out the data. Those interrupts are disabled during your ISR. For that reason, it is best to avoid using Serial in an ISR. Change the code to set a flag in the ISR and do the printing from loop in response to the flag.

answered May 1, 2018 at 23:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.