Can someone explain map function?

So im learning how to use the map function to control a DC motor

the code follows as : Int val map(bits, 150, 0, 0, 255)

can someone explain in the simplest way for me thanx

It maps the input range from 150 to 0 into the output range of 0 to 255. So, if you input 150 it outputs 0. If you input 0 it outputs 255. So if you input 75 you get half the output range or 128.

the code follows as : Int val map(bits, 150, 0, 0, 255)

That doesn't have a hope in hell of compiling. There are at least three errors. Post your real code that compiles.

Int val map(bits, 150, 0, 0, 255)

That code will not compile.
If you were to write

int val = map(bits, 150, 0, 0, 255);

it would compile and would take a value between 150 and 0 in the bits variable, convert it to the range 0 to 255 and assign the result to the val variable. Note that the map() function does not do any bounds checking so you may want to investigate the constrain() function too.

UKHeliBob:

Int val map(bits, 150, 0, 0, 255)

That code will not compile.
If you were to write

int val = map(bits, 150, 0, 0, 255);

it would compile and would take a value between 150 and 0 in the bits variable, convert it to the range 0 to 255 and assign the result to the val variable. Note that the map() function does not do any bounds checking so you may want to investigate the constrain() function too.

when you say convert it to the 0 to 255 what does that mean, sorry im new to the arduino world.

Let's say the value of bits was 150 then after the map() function the value of val would be 0. If bits were 0 then val would be 255 and proportionally for the intermediate values of bits.

We both have a computer so why not make it do some work to illustrate what I mean.

void setup()
{
  Serial.begin(115200);
  Serial.println("bits\tval");
  for (int bits = 150; bits >= 0; bits--)
  {
    int val = map(bits, 150, 0, 0, 255);
    Serial.print(bits);
    Serial.print("\t");
    Serial.println(val);
  }
}

void loop()
{
}

Get some graph paper. Put a point at 0,255. Put another at 150,0. Draw a straight line between the points. when you give map() the value on the X axis, it will return the value on the Y axis.

for example:

// the lowest value i could achieve is 0 and highest 150

// there for if i set it to 50,100, now the lowest value i can get is 50 and 100 would be the highest

int val = map(bits, 0, 150, 50, 100)

Am i right?

Am i right?

Modify my program, run it and see.