Struggling with Potentiometer task

Hi all,

I am making a controller (keyboard emulator) which has a pot as its switch/controller.

So The pot needs to have 9 ranges:
0-28
29-57
58-85
85-113
114-141
142-169
170-197
198-225
226-255

Basically, what needs to happen is if it goes up one range (any range), it needs to fire function a, and if it goes down a range, it needs to fire func b.
This needs to happen each time it goes up or down a range.

Cant quite work out the best way to achieve it.

Cant find any similar examples.

Any help or guidance would be appreciated.

I presume that you know how to read an analogue input and test its value

So the task becomes one of reading the input, assigning the current reading to what I would call a range number (use a series of if statements if you can't think of a neater way), saving the current range number then going round the loop() function until the range number changes. Once it changes you determine in which direction it has changed and take the appropriate action

Does the pot actually return values between 0 and 255 or do you need to divide the 0 to 1023 that it actually returns by 4 to get 0 to 255 ?

Thanks so far. Yes reading pot values is fine. And i'm using a 0-255 range reading from the pot. (No real reason, just the examples on pot stuff did it that way).

Its the bit about how to test if its going up or down a range that its hard for me.

You would use something like this (pseudocode):

last_range = 0

read_range() begin
  value = read_pot();
  if value in 0..28 then return 0
  if value in 29..57 then return 1
  if value in 58..85 then return 2
  and so on...
end

loop begin
  current_range = read_range()
  if current_range > last_range then range_was_increased()
  if current_range < last_range then range_was_decreased()
  last_range = current_range
end

Thanks Danios90, Im using the concept and its working ok, apart from when first running the app, it prints the 'up'.

//Constants:
const int potPin = A0; //pin A0 to read analog input

//Variables:
int value; //save analog value
int controller;
int last_range = 0;
int current_range;

void setup(){
  pinMode(potPin, INPUT);
  Serial.begin(9600);
}

void loop(){
  
  value = analogRead(potPin);          //Read and save analog value from potentiometer
  value = map(value, 0, 1023, 0, 255); //Map value 0-1023 to 0-255 (PWM)
  delay(100);                          //Small delay

 //set range numbers
  if (value <28 )
    { controller = 1;
    }
  else if (value >= 28 && value <57)
    { controller = 2;
    }
  else if (value >= 58 && value <85)
    { controller = 3;
    }
  else if (value >= 86 && value <113)
    { controller = 4;
    }
  else if (value >= 114 && value <141 )
    { controller = 5;
    }
  else if (value >= 141 && value <169 )
    { controller = 6;
    }
  else if (value >= 170 && value <197 )
    { controller = 7;
    }
  else if (value >= 198 && value <225 )
    { controller =8;
    }
  else if (value >= 225 && value <255 )
    { controller = 9;
    }
    
 //detect if going up or down
  current_range = controller;
  if (current_range > last_range)
   {
    Serial.println("up");
   }
  if (current_range < last_range)
  {
    Serial.println("down");
  }
    last_range = current_range;

}

How exact do this number ranges have to be ?

UKHeliBob:
How exact do this number ranges have to be ?

Not exact at all I think, its just needs to differentiate between the 9 steps.
Its a controller, think of it as gears 1-9. But the host software only knows up or down, not what position its in or anything.

void loop()
{
  int gear = (analogRead(A0) * 9) / 1024;  // Map to the range 0 to 8
  static int oldGear = 0;

  while (gear > oldGear)
  {
    function_a();
    oldGear++;
  }

  while (gear < oldGear)
  {
    function_b();
    oldGear--;
  }
}

In "setup()" you should initialize "last_range" to the current pot-value in order to prevent the "up" from start :slight_smile:

Danois90:
In "setup()" you should initialize "last_range" to the current pot-value in order to prevent the "up" from start :slight_smile:

Thanks, sorted!