Using a pot to set a number between 1 and 3

I am trying to use a pot to set a number between 1 and 3 using the code below but can only get it to read as Bay:1 on a pro micro. Any tips or is there something I am doing wrong?

#define pot1 A2
int North = 1;
int Middle = 2;
int South = 3;
int bay;

void setup() {
Serial.begin(9600);
analogRead(pot1);
}
void loop(){
if (pot1 > 0 && pot1 < 200) {
bay= North;
}
if (pot1 > 200 && pot1 < 400) {
bay = Middle;
}
if (pot1 > 400 && pot1 < 600){
bay = South;
}
Serial.println(analogRead(pot1));
Serial.print("Bay:"); Serial.println(bay);
delay(2000);
}

This does nothing for you:

analogRead(pot1);


if (pot1 > 0 && pot1 < 200) {

pot1 is equal to A1

So you are saying:

if (A1 > 0 && A1 < 200) {

Follow the map() doc to give you 1, 2 or 3. Something like map(a2, 0, 1023, 1, 3);

Yes? I only want it to be "Bay:1" if A1 is between 1 and 200.
and "Bay:2" if it is between 201 and 399. etc

You need to read the analog value that’s sitting on the A1 pin.

int value = analogRead(pot1);
The you can do:
if (value > 0 && value < 200) {

etc.

Thank you! I really should have been able to see that SMH. Appreciate the help

lesser redundancy:

switch (pot1){
  case   1 ... 199 : bay = North;  break;
  case 201 ... 399 : bay = Middle; break;
  case 401 ... 599 : bay = South;  break;
  default: Serial.println(F("kaboom - undefined"));
}
1 Like

The map() function has a place, I assume, but it is always a good idea to check what it is (or is not!) doing for you. In particukar, map() is hard to use with small ranges on either the input or output side.

Here

Wokwi_badge Map function unmasked!

is a demo that shows that map(a2, 0, 1023, 1, 3) will return 3 only when the value being maped is 1023, whilst 1 and 2 will each enjoy being reported for 49.95 percent of the range being mapped.

So it is a bit of a challenge to get 1, 2 and 3 to share that range. I have usually rejected map() after a brief struggle. It also does cheerfully map values that are out of the input range, which is why the constrain() function is often mentioned in the same breath.

The maths of mapping are simple. But in this case, using extended case ranges as @noiasca shares is the clearest expression and the easiest to get right and/or modify. Nice when the buckets aren't all 1/Nth of the total, too.

// https://wokwi.com/projects/361085264872987649
// https://forum.arduino.cc/t/using-a-pot-to-set-a-number-between-1-and-3/1110731

void setup() {
  Serial.begin(115200);
  Serial.println("map function\n");
}

void loop() {
  int a2 = analogRead(A2);
  int m2 = map(a2, 0, 1023, 1, 3);

  Serial.print(a2); Serial.print(" in, ");
  Serial.print(m2); Serial.print(" out ");
  Serial.println();

  delay(333);
}

a7

1 Like

for 1024 possible values it should be feasable to write a short unit test to verify which variant works as desired:

/*
    https://forum.arduino.cc/t/using-a-pot-to-set-a-number-between-1-and-3/1110731/7

    compare the different variants

*/


int North = 1;
int Middle = 2;
int South = 3;

void setup() {
  Serial.begin(115200);
  Serial.println("test it\n");

  Serial.print("i");
  Serial.print('\t');
  Serial.print("if");
  Serial.print('\t');
  Serial.print("map");
  Serial.print('\t');
  Serial.print("switch");
  Serial.println();

  for (int i = 0; i < 1024; i++) {
    Serial.print(i);
    Serial.print('\t');
    Serial.print(convertIf(i));
    Serial.print('\t');
    Serial.print(convertMap(i));
    Serial.print('\t');
    Serial.print(convertSwitch(i));
    Serial.println();
  }

}

void loop() {
}

int convertIf(int value) {
  int bay = -42;
  if (value > 0 && value < 200) {
    bay = North;
  }
  if (value > 200 && value < 400) {
    bay = Middle;
  }
  if (value > 400 && value < 600) {
    bay = South;
  }
  return bay;
}

int convertMap(int value) {
  return  map(value, 0, 1023, 1, 3);
}

int convertSwitch(int value) {
  int bay = -42;
  switch (value) {
    case   1 ... 199 : bay = North;  break;
    case 201 ... 399 : bay = Middle; break;
    case 401 ... 599 : bay = South;  break;
      //default: Serial.println(F("kaboom - undefined"));
  }
  return bay;
}

unit_test - Wokwi Arduino and ESP32 Simulator

1 Like

Nice @noiasca, THX.

Never even noticed, these my favorite lines

.
200	-42	1	-42
.
.
.
400	-42	1	-42
.

a7

1 Like

well, that is what was given in #1 ... :wink:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.