How to set potentiometer in steps

I want to set a minimum pressure for a setup.
I want to do this with a potentiometer in steps of 15, from 15 to 900hPa.
I have a code that uses the potmeter's value, but it changes per 0,01 hPa and not per 15 hPa.
Can someone help?

This is the code for now. It's in dutch (druk=pressure):

int analogValue = analogRead(A0);
float minDruk = floatMap(analogValue, 0, 1023, 15, 900);
Serial.print ("Minimum druk ");
Serial.println(minDruk);

Read the pot and use the map() function to change the range to the number of steps requuired. Multiply the mapped range by the size of the required steps to get the output value

goedemorgen
try this sketch proposal .

struct TIMER {
  unsigned long stamp;
  unsigned long duration;
};
TIMER timer {0,1000};

float mapFloat(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void setup() {
  Serial.begin(9600);
  Serial.println("en hier gaan we");
}
void loop() {
  if (millis()-timer.stamp>=timer.duration){
    timer.stamp=millis(); 
    Serial.println((int)(mapFloat(analogRead(A0),0.0,1023.0,15.0,900.0)/15.0)*15);
  }
}

Even easier than my original suggestion if you take advantage of the integer division done by the compiler

const byte potPin = A7;
int potValue;


void setup()
{
  Serial.begin(115200);
  while (!Serial);
}

void loop()
{
  potValue = analogRead(potPin);
  Serial.print(potValue);
  Serial.print("\t");
  Serial.println(potValue / (1024 / 15));
}
1 Like
void setup ()
{
  Serial.begin ( 115200 );
  for ( word n = 0; n <= 1023; n ++ ) // simulate analogRead
  {
    word D = 15 * ( 1 +  n * 60 / 1024 ); // 60 = 900 / 15
    Serial.print ( n );
    Serial.print ( "\t" );
    Serial.println ( D );
  }
}

void loop ()
{}

Serial data:
0 15
1 15
2 15
3 15
....
1020 900
1021 900
1022 900
1023 900

Divide the values by 15, then multiply by 15 to get steps of 15:

int analogValue = analogRead(A0);
float minDruk = 15.0 * map(analogValue, 0, 1023, 15/15, 900/15);
Serial.print ("Minimum druk ");
Serial.println(minDruk);

Hysteresis can eliminate some annoying behaviour of a potentiometer set up to emulate a multi-pole rotary switch, as may be the case here.

If you on the edge of N and N + 1, the output can go back and forth randomly.

is a good start if you are curious.

a7