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");
}
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.
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..
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.
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.
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.