Am I doing this right? :-)

Hi

I have this (in the correct sections)

int SpeedVal;
int SetSpeed;
SpeedVal = pulseIn(IN_PIN4, HIGH);
SetSpeed = map(SpeedVal, 995, 1995, 0.001, 0.01);
String SetSpeed_prn = "Set Speed = ";
Serial.println(SetSpeed_prn + SetSpeed);

SetSpeed is alwas 0 instead of a value between 0.001 and 0.01.
I'm not getting any error messages and I am getting a correct value for SpeedVal from 995 to 1995.
Any idea why?

Thanks in advance
Mat

Ok

So I mis-understood it...

So

SpeedVal = map(SpeedVal, 995, 1995, 0.001, 0.01);

Should work but it isn't... ??

map() does not take floating point numbers but long integers. It also returns a long integer and not a float.

The map reference does not mention this directly. You can find the function prototype in (on a Windows system) C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\Arduino.h

long map(long, long, long, long, long);

and (if interested) the implementation in C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\WMath.cpp

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Further SetSpeed is declared as an integer so can't contain fractional numbers

If you really need floating point results, I would multiply the min and max by 1000.

long x = map(SpeedVal, 995, 1995, 1, 10);
float SetSpeed = x / 1000.0;

Note the use of 1000.0 instead of 1000; this forces the compiler to use floating point math instead of the default 16-bit integer math. Else you would still end up with 0.

map page says this:

The
"map() function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged."

Got it !!!

Thanks...

I adjusted my Maths where I need it!