Skip to content
Alex Krieg edited this page May 24, 2018 · 6 revisions

Events


Imagine that you have a button that is supposed to trigger a certain activity.
How would you solve this problem?

const byte buttonPin = 3;

void myTriggerFunction();
void setup()
{
  pinMode(buttonPin,INPUT);
}
void loop()
{
  if(digitalRead(buttonPin) == true)
  {
    myTriggerFunction();
  }
}
void myTriggerFunction()
{
  //Button pressed
}

This code could be a solution. If you want to query different events now, such as rising or falling edge or button pressed or not,
so the code could by quickly very large and that only with a button.

Is not it much easier if you don't have to worry about reading the button? I think so, and so some libraries of mine offer you the ability to add a function to an event.


How events work

To use an event, you need a function that can be triggered.
Mostly such functions will not accept or return any parameters.

So you have to define a function that you can pass to the object.
The object stores the address (pointer) of the function. The function is triggered when the **. Update (); ** function is called.
But only if a function has been deposited and the event has occurred.


Make an event

An example of an event looks like this:

#include "button.h"

const byte buttonPin = 3;
Button myButton(buttonPin);

void myTriggerFunction();
void setup()
{
  myButton.OnPressedEdge(myTriggerFunction);
}
void loop()
{
  myButton.update();
}
void myTriggerFunction()
{
  //Button pressed
}
Clone this wiki locally