Map function weird behaviour

Hello,

I'm trying to use my Arduino to log Throttle Possition Sensor reading from my motorcycle to SD Card.

But readings returned by map value are weird.

Thats how I do that:

const int TPSin = A0;
byte TPS = 0;

void setup(){
pinMode(TPSin, INPUT);

}

void loop(){
int TPSReading = analogRead(TPSin);
TPS = map(TPSReading, 0, 1023, 0, 100);
}

And then map return good values most of the time, but sometimes that give me 255 (max byte value). Why?

Have you tried printing TPSReading before using the map() function ?

What exactly is connected to the analogue input ?

No, I haven't. I'll try today.

I've connected TPS output to analog input. TPS (3-wire) is potentiometer where resistances change with throttle openning.

Thing is, in the example you don’t even print the outcome… :wink:

Extra note, map(x, 0, 1023, 0, 100) looks okay but actually doesn’t make for a nice curve. Only value 1023 will map to 100 (where as for example the whole range 52 to 60 maps to 5). map(x, 0, 1024, 0, 101) will give a nicer result :slight_smile:

Yeah, I know example doesn't print, because that's just part for mapping.

Actually I use ESP32, then map for me looks like:

TPS = map(TPSreading, 250, 2130, 0, 100);

And most of the time that give me nice 0 - 100% :slight_smile:

Yeay, it will indeed output 100 if the input is 2130. But it will ONLY output 100 when input is 2130. Where as it will output 1 for the full input range of 269 to 287. Doing map(TPSreading, 250, 2131, 0, 101); fixes that :slight_smile:

Actually I use ESP32, then map for me looks like:

TPS = map(TPSreading, 250, 2130, 0, 100);

What are you expecting map() to return if TPSreading is 249?