Arduino IDE: RBD_Light Example Blink Without DelayNeed help writing code? You can hire Robots + Big Data to write Arduino software for your project! Click here to get started...
Arduino

ThresholdAPI DOCUMENTATION

Set and check numeric quantile scales.

See Example  Public Methods  Release Notes
Overview

Set and check numeric quantile scales. Provide an input domain and this library will convert it to a numeric quantile output range. This is different from the Arduino map() function because this library can handle a non-uniform input range.

Example Setup
  • Install this library and load the example sketch onto an Arduino
  • Open a serial connection at 115200 baud
  • Watch as converted values are printed to serial output

This example takes an input domain of:

  • [under 0]
  • [0 - 10.49]
  • [10.5 - 19.99]
  • [20 - 30]
  • [over 30]

and converts it to the quantile output range:

  1. UNDER
  2. LOW
  3. MEDIUM
  4. HIGH
  5. OVER
Example SketchArduinoC++
#include <RBD_Threshold.h>

RBD::Threshold threshold(3); // 3 levels

void setup() {
  Serial.begin(115200);
  threshold.setLevel(1,0);    // low
  threshold.setLevel(2,10.5); // medium
  threshold.setLevel(3,20);   // high
  threshold.setMaxLevel(30);  // max
}

void loop() {
  Serial.println(threshold.computeLevel(-1));   // 0 under
  Serial.println(threshold.computeLevel(8));    // 1 low
  Serial.println(threshold.computeLevel(10.6)); // 2 medium
  Serial.println(threshold.computeLevel(30));   // 3 high
  Serial.println(threshold.computeLevel(31));   // 4 over
}
Public Methods

RBD::Threshold

RBD::Threshold constructor(level_count)

Create a new instance and provide an integer for the maximum number of threshold levels. For example; set the level_count to 3 if you have levels of low, medium, and high. If you need more levels, this library will dynamically allocate an array to accommodate the size you provide the constructor.

EXAMPLE SKETCH
RBD::Threshold threshold(3) // 3 levels

void setup() {
  ...
}
threshold
.setLevel(index, value)

Provide an integer for a level index and an integer, float, or double for the threshold value. This is not zero based, the first level starts at 1. You must also call setMaxLevel() at the end to set an upper bounds of your last level.

EXAMPLE SKETCH
void setup() {
  threshold.setLevel(1,0);    // [0 - 10.49]
  threshold.setLevel(2,10.5); // [10.5 - 19.99]
  threshold.setLevel(3,20);   // [20 - max]
}
threshold
.setMaxLevel(value)

Provide an integer, float, or double to set the upper-bounds threshold of the last level.

EXAMPLE SKETCH
void setup() {
  threshold.setMaxLevel(30); // [max = 30]
}
threshold
.getLevel(index)

Provide an integer for the level index, and this returns the value originally provided to setLevel().

EXAMPLE SKETCH
void loop() {
  Serial.println(threshold.getLevel(3); // returns 20
}
threshold
.getMaxLevel()

Returns the value originally provided to setMaxLevel().

EXAMPLE SKETCH
void loop() {
  Serial.println(threshold.getMaxLevel()); // returns 30
}
threshold
.computeLevel(value)

Provide an integer, float, or double value to compare against the thresholds for setLevel() and setMaxLevel(). This returns an integer for the computed level.

  • it returns 0 for values that are under the first level
  • 1 for your first level
  • 2 for your second level
  • n for your nth level
  • sizeof(n) + 1 for any value greater than getMaxLevel()
  • -1 if the value was not found
EXAMPLE SKETCH
void loop() {
  Serial.println(threshold.computeLevel(17)); // returns 2 (medium)
}