Something weird about the map function

In the code below, I would expect the output to be 5000 50 repeating forever, but what is actually coming out is 5000 100.
Not sure why that is happening.

int tspeed;
void setup()
{
  Serial.begin(115200);
}

void loop()
{
  tspeed = 0;
  tspeed = map(tspeed, 0, 100, 5000, 50);
  Serial.print(tspeed);
  Serial.print(" ");
  
  tspeed = 100;
  tspeed = map(tspeed, 0, 100, 5000, 50);
  Serial.println(tspeed);
  delay(1000);
}

This is what I get:
5000 50
5000 50
5000 50
5000 50
5000 50
5000 50
...

Thanks jremington, I am running this on a Teensy 3.2 so maybe that is the problem. I'll post this over at the Teensy forum and see if anyone there knows why this is happening.

Reply #1 was running on an Arduino Pro Mini, where the map function is defined as

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;
}

The map function may be defined differently on a Teensy.

The folks at the Teensy forum said the map was updated a few times lately. I had the latest stable release of the teensy software so they pointed me to a beta release and that fixed it.

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