 # map function steps

Hello, I'm new to Arduino I am using an Arduino Uno.

I am trying to lcd.print my potentiometer input (0...1023) in a range of 0...50 and in 0.5 steps.

I found the extended Map-Function and with it I can print the input in 1/1024 steps but not in 0.5.

Function:

``````float extmap(double x, double in_min, double in_max, double out_min, double out_max)
{return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;}
``````

Is there a way to print in 0.5 steps in range 0...50 although your potentiometer input is 0...1023?

Artur

Think about it from a math perspective.

First map your value from 0...1023 to 0...100 with the regular old map function. Then divide by 2.0 into a float if you really must have it as a decimal number in 0.5 steps.

If that's for display purposes then I'd advise keeping up with it in the program as 0 to 100 in steps of 1 because of all the various issues that come with using float (speed, low precision, etc.)

Thanks for the answer I tried that out but it just still shows whole numbers instead of 0.5 steps. It is not an lcd issue, since the same values are found in the Serial Monitor.

``````float pot_print = map(analogRead(pot_input), 0, 1023, 0, 100)/2;
``````

I would not even make it a float

``````const byte PotPin = A0;

void setup(){

}

void loop(){
int val = map(analogRead(PotPin), 0, 1024, 0, 101); //will map nicer to 0-100

lcd.print(val / 2);
lcd.print(".");
lcd.print(val % 2 ? 5 : 0);
}
``````

If you really want to big and slow float way, try dividing by 2.0 Code:

```
size_t printInHalfSteps(Print &printer, int value) {

value += 1023 / 100 / 2; // comment out if you don’t want rounding
uint8_t zeroToHundred = map(value, 0, 1023, 0, 100);
uint8_t zeroToFiftyWhole = zeroToHundred / 2;
uint8_t zeroToFiftyFraction = zeroToHundred % 2;
size_t printed = 0;
printed += printer.printcolor=#000000[/color];
printed += printer.print(zeroToFiftyFraction ? “.5” : “.0”);
return printed;
}

void setupcolor=#000000[/color] {
Serial.begincolor=#000000[/color];
whilecolor=#000000[/color];
for (int i = 0; i < 1024; i++) {
float actual = round(i * 100.0 / 1023.0) / 2.0;
Serial.printcolor=#000000[/color];
Serial.printcolor=#000000[/color];
Serial.printcolor=#000000[/color];
Serial.printcolor=#000000[/color];
size_t printed = printInHalfSteps(Serial, i);
Serial.printcolor=#000000[/color];
Serial.printlncolor=#000000[/color];
}
}

void loopcolor=#000000[/color] { }
```

|

You’ll get a tiny rounding error (in about 1% of the cases, so it should be negligible).

Pieter

Artur_exe:
Thanks for the answer I tried that out but it just still shows whole numbers instead of 0.5 steps. It is not an lcd issue, since the same values are found in the Serial Monitor.

``````float pot_print = map(analogRead(pot_input), 0, 1023, 0, 100)/2;
``````

Because you divided by 2 and not 2.0, the compiler noticed that all the things on the right were integers and used int math to do the math. If you use 2.0 instead of 2, then the compiler will see the floating point number in there and assume you want a floating point result. He doesn’t care to look that you wanted to store the result in a float.

``````float pot_print = map(analogRead(pot_input), 0, 1023, 0, 100)/2.0;
``````

This sketch prints 0-100%
Easy to divide final ‘potpercentage’ by two.
Leo…

``````// converts the position of a 10k lin(B) pot to 0-100%
// pot connected to A0, 5volt and ground

int rawValue;
int oldValue;
byte potPercentage;
byte oldPercentage;

void setup() {
Serial.begin(115200); // set serial monitor to this baud rate, or change the value
}

void loop() {
// ignore bad hop-on region of a pot by removing 8 values at both extremes
rawValue = constrain(rawValue, 8, 1015);
if (rawValue < (oldValue - 4) || rawValue > (oldValue + 4)) {
oldValue = rawValue;
// convert to percentage
potPercentage = map(oldValue, 8, 1008, 0, 100);
// Only print if %value changes
if (oldPercentage != potPercentage) {
Serial.print("Pot percentage is: ");
Serial.print(potPercentage);
Serial.println(" %");
oldPercentage = potPercentage;
}
}
}
``````