map function steps

Hello, I'm new to Arduino :slight_smile:
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?

Thanks in advance
Artur

Thanks for the answer :slight_smile:

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 :wink:

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

This sketch prints 0-100%
Trims bad pot extremes, even spacing, pre-read, deadband, etc.
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() {
  // read input twice
  rawValue = analogRead(A0);
  rawValue = analogRead(A0); // double read
  // ignore bad hop-on region of a pot by removing 8 values at both extremes
  rawValue = constrain(rawValue, 8, 1015);
  // add some deadband
  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;
    }
  }
}