Introduction

Using the library

Including the library

Once you have installed the library, you can include the entire library in your sketch, like so:

  #include <CTRL.h>

But if you only need a portion of the library, you can import that specific part. You can achieve this by using one or more of the includes listed below:

  #include <CtrlBtn.h>
  #include <CtrlEnc.h>
  #include <CtrlPot.h>
  #include <CtrlLed.h>
  #include <CtrlMux.h>
  #include <CtrlGroup.h>

First sketch

Ok, so let's draw up a simple sketch for a button:

#include <CtrlBtn.h>

void onPress() {
  Serial.println("Button pressed");
}

void onRelease() {
  Serial.println("Button released");
}

CtrlBtn button(1, 15, onPress, onRelease);

void setup() {
  Serial.begin(9600);
}

void loop() {
  button.process();
}

Code explanation

So, a short rundown on what's happening in the example above. First, of course, we include the CtrlBtn header. We then create the functionality for when we press down on the button by defining an onPress handler. This will execute every time the button enters a 'down' state. The onRelease handler, in turn, will be triggered as soon as the button is released and goes back 'up'. You can omit the onRelease if you don't need it (the same goes for the onPress, by the way).

After that, we create a button of the type CtrlBtn and pass it some data. The first parameter (1) tells the software what pin the button is hooked up to (this pin will be pulled HIGH on initialization, so make sure you wire the button from pin 1 to the button and from the button to ground). The second parameter (15) is the bounce duration in milliseconds. I usually set this to around 15 as it accommodates the bouncing of most buttons that I use. If you have a very 'noisy' button, you can increase this number. And then the third and fourth parameters are optional and set the onPress and onRelease handlers that we defined earlier.

Finally, all we need to do is to make sure we call the button's process method during looping so that all its data readings and functionality can be continuously processed.

Previous
Installation