Skip to content

Documentation

Crunchy edited this page Jan 15, 2024 · 31 revisions

Target Audience

I never assume my audience are experts, so I will spell out the really obvious basics in places, because Arduino and its kin are as often a beginner's hobby as they are used in high end projects. If you have a high level of microcontroller programming experience, jump straight ahead to the [Quick Start](https://github.com/crunchysteve/EdgieD/wiki/Quick-Start) guide.

Installation

Download this repository as a zip (or clone to git and install, as per the usual way with this method) and open your project in Arduino.

  • Select the Sketch/Include Library/Add .zip Library... menu item,
  • Navigate to your Downloads folder,
  • Select the EdgieD-master.zip file. The Arduino IDE does the rest, unzipping the library, then placing it in the libraries folder in your Arduino folder. The library should now be ready to include.

To add it to a PlatformIO project, simply add the line lib_deps = https://github.com/crunchysteve/EdgieD.git to your project's platformio.ini. This is why I've switched from ArduinoIDE to PlatformIO! Any library available on a git repo can be included with a single line of markdown.

Scope

EdgieD takes two (2) input boolean variables and returns a true or false output state, depending on whether an edge has been detected...

if(state, edge){ //  Do something if condition met }

...is true or false.

Breaking this down, the bool "state" is the current test condition on an input, signal or frequently changing variable. In the case of testing an input, we would store a digital read in a local variable, pass it to the function which, if the new state is different to previous test, stores the current test in a static variable for testing next time around.

The bool edge, is usually populated with one of the library's custom macros, Leading or Trailing (logic 1 and 0, respectively), to denote which edge we are testing for. Leading is the default and may be left out of testing for leading edges.

If the function finds the condition to be met, it returns TRUE, otherwise it returns FALSE.

#Include Directive

Like with any library, official Arduino or otherwise, EdgieD needs to be included near the beginning of a program with the #include preprocessor directive, #include <EdgieD.h> This tells the compiler to include the library in your project.

Creating an Instance

Before we can use EdgieD, we have to "instantiate" it, or create an instance. EdgieD is an object oriented library, so you can create multiple instances of it to test for edges on different signals, states or inputs. Most code will only need one instance, like this

Edge myEdge;

"Edge" is the name of the library's base method, "myEdge" is the name we give to the instance we will call to do an edge or state-change test.

You can instantiate multiple cases, eg...

Edge myEdge1;
Edge myEdge2;
//  etc.

...however, in most cases you'll probably only need the one and call it as you need it.

Calling Your Instance

Before you call the myEdge method, you will need a state to test (such as a button on an input pin, lets say digital pin 2), to know whether you're testing for a LEADING or TRAILING edge and a place to store the tested state for later use. So, first we setup variables to use, state and oldState, then we use pinMode(2,INPUT) in the setup function, and finally, we decide if we''re testing for a LEADING or TRAILING edge. If we're testing an INPUT for state change, we want to know the instant the state has changed, so we'll use LEADING as our edge value. If we'd used INPUT_PULLUP as our pinMode, we'd probably use TRAILING, because we're actually dealing with inverted logic, so the downward edge is the leading edge.

EdgieD Basic Demo

This is the demo included with the library, it works like this...

// included example. you could call it "blink on clock rising edge."

#include <EdgieD.h>                                 //  import library to do edge detection

bool token = false;                                 //  Initialise state variable for a system clock
uint32_t backThen = 0;                              //  Initialise variable to hold previous clock time
uint32_t period = 500;                              //  Initialise variable to set clock toggle period

Edge myEdge;                                        //  Instantiate the EdgieD library as "myEdge"

void setup() {                                      //  Arduino setup function
  Serial.begin(115200);                             //  Start serial conection at 114200 baud
  pinMode(LED_BUILTIN, OUTPUT);                     //  Configure the builtin LED (13) as an output
}

void loop() {                                       // main program loop
  //  The next four lines run a non-blocking clock running at 1Hz (period on, period off)
  uint32_t rightNow = millis();                     //  <-|
  if(rightNow - backThen > period){                 //    |
    token = !token;                                 //    |-  start a software squarewave
    backThen = rightNow;                            //    |
  }                                                 //  <-|

  digitalWrite(LED_BUILTIN,myEdge.detect(token));   //  Lights up LED when pulse is detected
                                                    //  also takes macros 'Rising' or 'Falling'
                                                    //  optionally after token, the default is ```Rising```,
                                                    //  so we're only passing the test value, ```token```

  delay(20);                                        //  20mS delay to make the LED visible
                                                    //  Would leave this out in normal usage.
                                                    //  On an Arduino Uno or Nano, the timing of a 
                                                    //  detected edge is less than 8uS currently.
                                                    //  "Blink" and you'll miss it! ;-)
}