mapFrom01()

Re-maps a number in the range [0, 1] to another range. That is, a value of 0 would get mapped to toLow, a value of 1 to toHigh, values in-between to values in-between, etc.

mapFrom01(x, toLow, toHigh)

is equivalent to:

mapFloat(x, 0, 1, toLow, toHigh)

By default, does not constrain output to stay within the [fromHigh, toHigh] range, because out-of-range values are sometimes intended and useful. In order to constrain the return value within range, use the CONSTRAIN argument as the last parameter:

mapFrom01(x, toLow, toHigh, CONSTRAIN)

See mapFloat() for more details.

Example

#include <Plaquette.h>

SineOsc modulator(10.0);

SquareOsc oscillator(1.0);

DigitalOut led(13);

void begin() {
}

void step() {
  // Change duty-cycle of oscillator in range [0.2, 0.8].
  float dutyCycle = mapFrom01(modulator, 0.2, 0.8); // alternative: modulator.mapTo(0.2, 0.8)
  oscillator.dutyCycle(dutyCycle);

  // Send to LED.
  oscillator >> led;
}

Reference

float pq::mapFrom01(double value, double toLow, double toHigh, uint8_t mode = UNCONSTRAIN)

Re-maps a number in range [0, 1] to a new range.

Parameters:
  • value – the number to map (in [0,1])

  • toLow – the lower bound of the value’s target range

  • toHigh – the upper bound of the value’s target range

  • mode – set to CONSTRAIN to constrain the return value between toLow and toHigh or WRAP for the value to wrap around

Returns:

the mapped value in [toLow, toHigh]

See Also