Error: cannot convert from float to int,, when I use map()

Hello, I am a beginner and finding a problem. I wrote the following code and had error message “cannot convert from float to int”.

void serialEvent (Serial Port)
{
  brightness = float(port.readStringUntil('\n'));
  n=int(brightness);
  m=map (n,0,40,1,14);
}

I have already before setup function declared m and n as integer. Where is the problem? Thanks everyone.

1 Like
  • According to its reference: Processing.org/reference/map_.html
  • Function map() returns a float datatype value.
  • So we need to convert it to int if we try to assign it to an int variable.
  • There are many ways to do it though. Some examples:
  1. m = (int) map(n, 0, 40, 1, 14);
  2. m = int( map(n, 0, 40, 1, 14) );
  3. m = floor( map(n, 0, 40, 1, 14) );
  4. m = round( map(n, 0, 40, 1, 14) );
2 Likes

Thank you every much:)