Best way to code a whole bunch of ranges..

Hi All,

I have a need to check a sensor, and depending on the range the values for all, take some action....

The action will be for stepper motors, and i will actually reference a 2D array (depending on the sensor value), and then position the steppers accordingly....pretty straight forward...you will agree.

I am looking for what would be the best way to program these ranges, without maybe having to write a whole bunch of if() then statements for all the possible ranges the sensor values will fall in...I was thinking that the map function might accomplish this, but I have partial success...here is what I mean, if I do this:

long count = analogRead(A1);
//
long range_1 = map (count, 101000, 102000, 1,3);
  switch (range_1){
    case 1:
      Serial.println("this is 1");
      break;
    case 2:
      Serial.println("This is 2");
      break;
    case 3:
       Serial.println("This is 3");
       break;
  }
  //
  long range_2 = map (count, 141000, 143405, 4,8);
  switch (range_2){
    case 1:
      Serial.println("this is 4");
      break;
    case 2:
      Serial.println("This is 5");
      break;
    case 3:
      Serial.println("This is 6");
       break;
    case 4:
      Serial.println("this is 7");
       break;
    case 5:
      Serial.println("This is 8");
       break;
  }

It works for when my sensor falls in the first range of values, but it does not when the sensor now falls into the second set of values/ranges. As you can see, since I need like 3 to 4 different values within a range, this would result in a whole bunch of if then statements, to take care of the range values....

So I was wondering if there is a more efficient way to do this?

You can use a lot of ‘if-statements’, I don’t have a problem with that. It’s easy to read and to maintain.
The other option is to set the ranges in an array, and go through the array and test the value. That is what I would do (an array with elements of 3 integers or a struct), but please add good comments, so make clear what the values in the array are.

First off, analogRead() returns an integer in the range of 0-1023, not a long - and it will certainly never return a value greater than 1023 - so your map() calls are meaningless...?

Secondly, in your second map() call, you are mapping a value from one range (never mind the issue of the range I have already noted) to the values 4 through 8, but then doing a switch-case comparison to check to see if the value from the map will be the numbers 1, 2, 3, 4, or 5 (of which, only the last two could possibly match).

You might want to revisit and refactor your code, first - before we even think about going further with your question...

...Yes you are right on the long vs int....I will correct that.... DUAAAHHHH :blush:

That rest is a simple mistake I made writing to the forum......I should have cut and pasted the code....but in my sketch, I have the values correct....

thank you for taking the time to respond, really do appreciate it. I am just looking for the most efficient way to do this, not because I am lazy in typing it out, but want it to be as efficient as possible....

I should have cut and pasted the code....but in my sketch, I have the values correct....

So how about we get to see the real code?

But it seems that despite the use of map() you still wind up with a case for every possible value, in which case there's no point in using map() I think.

How many values can there be? That will effect how the job is done. 10 is one thing, 1024 is quite another.


Rob

What are the actual ranges? It may be feasible to calculate an index into your stepper action array by arithmetic means.