Inefficient use of If / Else If?

I've just started learning with my Arduino, and one of my first projects was a combination of the excellent CIRC-02 tutorial from oomlout and the potentiometer tutorial from arduino.cc.

I realized that I wanted to have the pot not provide such fine adjustments, so knowing that there are 1023 different "steps", I wrote the following:

actualPotValue = analogRead(potPin);

if (actualPotValue >= 0 && actualPotValue <= 100)
{
smoothPotValue = 250;
}
else if (actualPotValue >= 101 && actualPotValue <= 200)
{
smoothPotValue = 225;
}
else if (actualPotValue >= 201 && actualPotValue <= 300)
{
smoothPotValue = 200;
}
else if (actualPotValue >= 301 && actualPotValue <= 400)
{
smoothPotValue = 175;
}
else if (actualPotValue >= 401 && actualPotValue <= 500)
{
smoothPotValue = 150;
}
else if (actualPotValue >= 501 && actualPotValue <= 600)
{
smoothPotValue = 125;
}
else if (actualPotValue >= 601 && actualPotValue <= 700)
{
smoothPotValue = 100;
}
else if (actualPotValue >= 701 && actualPotValue <= 800)
{
smoothPotValue = 75;
}
else if (actualPotValue >= 801 && actualPotValue <= 900)
{
smoothPotValue = 50;
}
else if (actualPotValue >= 901)
{
smoothPotValue = 25;
}

Everything works exactly as I hoped it would, and I'm very happy with it, but my question is this: Is there a more efficient way of writing that? It seemed like a lot of repetitive typing, and I've noticed that in programming, there are usually ways around having to do that. As a side note, in the future I'd have definitely used shorter variables, as I suppose that was definitely part of the problem too.

A "for" loop with an "if" would do the trick, at the possible expense of latency.

Or "map" and a multiply.

If your boundaries were more like 0-99, 100-199, 200-299, etc... then you
could do something like this:

smoothPotValue =  25 * (10 - (analogRead(potPin)/100) );

but this would give you a zero value for actualPotValue >= 1000;
If that bothers you then you could follow that line with this:

if (smoothPotValue == 0)
    smoothPotValue = 25;

You can also use the map() function to create an index into an array of smooth pot values.

int smoothPotArray[] = { 250, 225, 200, 175, 150, 125, 100, 75, 50, 25 };

int i = map(actualPotValue, 0, 1023, 0, 9);
smoothPotValue = smoothPotArray[i];

In your original code, the length of the variable names is not a problem when you use copy + paste to replicate lines of code.

I really like both of those solutions! Since I'm not especially mathematically inclined, it looks like the map() function will be what I make more use of.

Thanks for taking the time to point me in new directions!

Can I further suggest a generalized solution?

All of the solutions above worked for your specific situation. That's because what you were doing inside each if ... else if ... was a simple assignment and nothing else.

As a general solution, the switch / case statements (http://www.arduino.cc/en/Reference/SwitchCase) can really be the best way to replace a lot of if ... else if ... statements.

One problem with that, and it applies to your situation here, is that the case statement can only test for equality. In your example, you want to test for a range of values.

So a combination of the map() function and the switch / case structure can be a generalized solution to replace a series of if ... else if ... structures.

  int var = map(actualPotValue, 0, 1023, 1, 10);

  switch (var) {
    case 1:
      //do something when var equals 1
      break;
    case 2:
      //do something when var equals 2
      break;

    . . .

    default: 
      // if nothing else matches, do the default
      // default is optional
  }

the switch / case statements (http://www.arduino.cc/en/Reference/SwitchCase) can really be the best way to replace a lot of if ... else if ... statements.

However, in many cases you will get compiled code that is very similar to what you'd get with an extended chain of if/else if statements. And in the "if" case, you can organize the order of evaluation to check the "most likely" candidates first, while a switch will leave it up to the compiler...