map() not working

I would like to map a time to a 0 to 100% value.

I tried:

    unsigned long m2_currentTime = millis()-m2_logStart;
    Serial.print("Currenttime:");
    Serial.print(m2_currentTime);
    Serial.print(" sample:");
    Serial.println(m2_sampleTime);
    int m2_barCalc = map(m2_currentTime,0,m2_sampleTime,0,100);
    Serial.print("MAP:");
    Serial.println(m2_barCalc);

And watched the serial monitor:

Currenttime:51996 sample:60000
MAP:0

the map function does not output any number at all.

Why is that? What i am doing wrong here? :cry:

It does work (integer truncated output though) and you might want to constrain the range of output between 0 and 100

Don't post snippet - we don't know what the variables are

this works

    unsigned long m2_currentTime = 51996L;
    unsigned long m2_sampleTime = 60000L;
    int p = map(m2_currentTime, 0, m2_sampleTime, 0, 100 );
    client.println(p);

prints 86

void setup() {
    Serial.begin(9600);
    
    unsigned long m2_currentTime = millis()-m2_logStart;
    Serial.print("Currenttime:");
    Serial.print(m2_currentTime);
    Serial.print(" sample:");
    Serial.println(m2_sampleTime);
    int m2_barCalc = map(m2_currentTime,0,m2_sampleTime,0,100);
    Serial.print("MAP:");
    Serial.println(m2_barCalc);
}

void loop() {}

Could you please include enough of a snippet to make a valid sketch?

/Users/john/Documents/Arduino/sketch_jul30a/sketch_jul30a.ino: In function 'void setup()':
sketch_jul30a:5: error: 'm2_logStart' was not declared in this scope
     unsigned long m2_currentTime = millis()-m2_logStart;
                                             ^
sketch_jul30a:9: error: 'm2_sampleTime' was not declared in this scope
     Serial.println(m2_sampleTime);
                    ^
exit status 1
'm2_logStart' was not declared in this scope

Yesterday it worked / not worked on a random level. So i decided to get rid of map() and made my own piece of code that calculates percent in a byte variable

byte barmap(unsigned long a, unsigned long b) {

  float initPercent = (float(100) / float(a));
  float currentPercent = (initPercent * float(b));
  byte result = currentPercent;
  return result;


}

this seems to be working all the time.

That's a way to do it - you could write 100.0 instead of float(100) and if you declare your function parameters a and b as float instead of unsigned long you don't even need to cast them to (float) in your math formula. Having them as double will lead to even better precision on some arduinos (on a uno a float and double use the same number of bytes though)

but map does work and does what it does (with integer)

You need to understand that when you do
map(m2_currentTime,0,m2_sampleTime,0,100);

If m2_currentTime is not within the interval [0, m2_sampleTime] then your result won't be between 0 and 100.

you only posted a snippet and no evidence of what you are saying nor explanation of what is wrong so hard to provide info to help out