Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Maping sensor data for deeper audio processing resolution

  1. #1
    Junior Member
    Join Date
    Dec 2016
    Location
    Berlin
    Posts
    11

    Maping sensor data for deeper audio processing resolution

    Hallo...;

    I`m trying to map 0 to 1024 sensor values in to a range of 0.001 to 1.0 using the modified map()
    within the Audio Library because a lot of their function parameters expect floating points between 0 and 1.0
    Code:
    float level = map(float(AnalogValues),0.0 , 1024.0, 0.001, 1.0);
    Serial.println(level);
    //mixer.gain(0, level);

    The Problem I only get values between 0.01 to 0.99 when I try to evaluate it with the Serial Monitor using:
    Code:
    Serial.println(level);
    So loosing 90% of my resoltuion.
    Expected was 0.001 to 1.0 to have 1000 steps for smoother rise of the level-gain.

    Any ideas?

    ThX
    Last edited by Jupiter Moll; 12-15-2017 at 08:55 AM.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,260
    I tried just now to test, with this code.

    Code:
    void setup() {
      while (!Serial);
      float x = map(float(1023), 0.0, 1024.0, 0.001, 1.0);
      Serial.println(x);
    }
    
    void loop() {
    }
    When I run this on a Teensy 3.6, it prints "1.00" in the serial monitor.


    Hopefully you can see in this message how we work here. The main thing we do is post complete test programs, which demonstrate the problem. Without a complete program which can be copied into Arduino and run on a real board, we're left with only guesswork about the rest of your code and why it's not working.

    But at least I can confirm that giving integer 1023 as input to that map function produces an output which the normal Serial.print with 2 significant digits does round up to 1.00.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,260
    As one more quick test, I tried printing 5 significant digits.

    When I run this program:

    Code:
    void setup() {
      while (!Serial);
      float x = map(float(0), 0.0, 1024.0, 0.001, 1.0);
      Serial.println(x, 5);
      x = map(float(1023), 0.0, 1024.0, 0.001, 1.0);
      Serial.println(x, 5);
    }
    
    void loop() {
    }
    It prints this:

    Code:
    0.00100
    0.99902

  4. #4
    Junior Member
    Join Date
    Dec 2016
    Location
    Berlin
    Posts
    11
    OK....

    THX Paul for the Info with the 5 significant digits:

    Code:
    Serial.println(x, 5);
    Last edited by Jupiter Moll; 12-15-2017 at 08:41 AM.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,260
    For this application, assuming your pot has normal linear taper, what you probably want is a logarithmic map function.

    This might be a good start for mapping from 0 - 1023 to 0 - 1.0 in a way that makes sense for audio levels.

    Code:
    log10f((float)(analogValue) / 113.667 + 1.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •