Using the map() function with limits?

Hi

I'm wondering if it's possible to put limits into the map fanuction.

If I use this;

val = map(average, 0, 1023, 15, 255);

The output will look like this,

However what I really need is this

I have modified the map like to look like this,

val = map(average, 0-400 , 1023, 15, 255);

and to my surprise it verifys but I can't actually test it yet.
Will it work in a Nano or do I need to set the lower limit another way?

Many thanks, Rich.

The code will evaluate the parameter values before using them in the map() so what you have written is the same as

val = map(average, -400 , 1023, 15, 255);

Is that what you actually want ? I suspect not

Take a look at the constrain() function if you want to limit the range of the output of map()

2 Likes

Hi @richierich1, welcome to the forum.

Perhaps you mean something like this?

val = max(15, map(average, 400, 1023, 15, 255));
1 Like

Two good suggestions striaght away, and from reading the documentation both look like they'd work just fine.
Many thanks for swifting putting me back on the right track.

@richierich1

Have a look at my fastMap library. It is developed for floats and the performance for integers is worse on some platforms.

It implements exactly the functions you need: lowerConstrainedMap().
Give it a try

1 Like

Indeed lowerConstrainedMap from @robtillaart's library is perfect.

Saves me the time of saying this can be easily programmed. Here's what the function looks like

float FastMap::lowerConstrainedMap(float value)
{
    if (value <= _in_min) return _out_min;
    return this->map(value);
}

@richierich1 can do the same thing "by hand". No objection to libraries, but there are things that should just be taken in stride sometimes; eveyone should know how sling a few lines of code

    int value = analogRead(A0);   // for example

    if (value <= knee) return zeroValue;
    else return map(value - knee, 1024, zeroValue, maxValue);

map() often finds itself nearby to calls to constrain(). map will happily extrapolate, so constrain() can be necessary.

I don't like map at all and only use it when I am too lazy to do without. For "real" mapping, there are better libraries, like @robtillaart's, available.

a7

2 Likes

Okay, I'll bite. :grinning_face_with_smiling_eyes:

Over the weekend I'll set up a test nano and play around with all the options you've all shown me as it'll be a good learning exercise.

The actual project this is used on is just a dimmer on some accessory lights on my motorcycle, so speed or accuracy isn't that important. I just noticed yesterday evening that it would better if the auto dimming ramped down sooner to the minimum brightness.

Changing the code in the nano on this motorcycle is a PITA as it's buried in the bike, and I have already broken off (and re-soldered) the micro USB connector once. I'm not sure how many more chances I'll get to plug back into it before it breaks again and needs replacing.

I've just been testing out the suggestion made by @UKHeliBob and @jfjlaros and both work great, but I'll probably use @jfjlaros suggested code as it is the simplest.

@robtillaart Many thanks for linking to your library. It's a bit of sledgehammer to crack a nut for this simple project but I will try to use it on future projects..
Just have to work out how to use GitHub first.

@alto777 Many thanks for your input here, I'll try your suggestions when I come back to looking at the FastMap..

So.. thanks again everyone.
Rich.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.