Unverified Commit 50560a23 authored by Tomáš Pilný's avatar Tomáš Pilný Committed by GitHub

Functional interrupt fix (#8175)

* Added link to external examples to the doc

* Fixed FunctionalInterrupt.ino + added introduction comment and few outputs
parent 1c3039eb
/*
* This example demonstrates usage of interrupt by detecting a button press.
*
* Setup: Connect first button between pin defined in BUTTON1 and GND
* Similarly connect second button between pin defined in BUTTON2 and GND.
* If you do not have a button simply connect a wire to those buttons
* - touching GND pin with other end of the wire will behave same as pressing the connected button.
* Wen using the bare wire be careful not to touch any other pin by accident.
*
* Note: There is no de-bounce implemented and the physical connection will normally
* trigger many more button presses than actually happened.
* This is completely normal and is not to be considered a fault.
*/
#include <Arduino.h> #include <Arduino.h>
#include <FunctionalInterrupt.h> #include <FunctionalInterrupt.h>
#define BUTTON1 16 #define BUTTON1 16
#define BUTTON2 17 #define BUTTON2 17
class Button class Button{
{
public: public:
Button(uint8_t reqPin) : PIN(reqPin){ Button(uint8_t reqPin) : PIN(reqPin){
pinMode(PIN, INPUT_PULLUP); pinMode(PIN, INPUT_PULLUP);
attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
}; };
~Button() {
void begin(){
attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
Serial.printf("Started button interrupt on pin %d\n", PIN);
}
~Button(){
detachInterrupt(PIN); detachInterrupt(PIN);
} }
void ARDUINO_ISR_ATTR isr() { void ARDUINO_ISR_ATTR isr(){
numberKeyPresses += 1; numberKeyPresses += 1;
pressed = true; pressed = true;
} }
void checkPressed() { void checkPressed(){
if (pressed) { if (pressed) {
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses); Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
pressed = false; pressed = false;
...@@ -36,9 +55,13 @@ private: ...@@ -36,9 +55,13 @@ private:
Button button1(BUTTON1); Button button1(BUTTON1);
Button button2(BUTTON2); Button button2(BUTTON2);
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
while(!Serial) delay(10);
Serial.println("Starting Functional Interrupt example.");
button1.begin();
button2.begin();
Serial.println("Setup done.");
} }
void loop() { void loop() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment