constrain() when mapping negative numbers.

Hi!

I'm trying to use the map- and constrain-function but I have run into some trouble...the code below does not work properly.

yawOut = map(yawRead, 504, 0, 0, -255);
yawOut = constrain(yawOut,  0, -255);

I want it to be mapped like this:

504 ~ 0
503 ~ -1
503 ~ -2

and so on...

With my code it gets the other way around.

504 ~ -255
503 ~ -254
503 ~ -253

When I don't use the constrain-function it works but I want to be sure that I do not get any negative numbers.
Could someone please explain to this noob how it should be done?

Best regards/ Christoffer

This is worth looking into. I didn't check the actual code for 'map' and 'constrain' yet.
Could you fix the low and high input first ? You have low and high the other way around.

yawOut = map(yawRead, 0, 504, -255, 0);  // 0 is low, 504 is high input limit.
yawOut = constrain(yawOut,  -255, 0);       // -255 is low, 0 is high input limit

constrain() is a macro with the ternary operator used twice. There is a special place reserved in hell for people that develop macros where functions are more appropriate.

You should constrain the input to map, not the output from map, to avoid the issue. Or, just use two if statements to do the constraining yourself.

what sort of numbers are you using ? -255 isn't necessarily an achievable number.