# convert binary view to a counter?

I have a counter that loops thru 32 times. Each time thru it switches runs a process IF the counter value was for example 1 and 2 and 3.

I have it wriiten now as:

if (counter == 1 || counter == 2 counter == 3 || ) { do this }

I have many of these combinations, so writing them each out like this is a bit painful, and not visually that intuitive. I'd like to have the code looking like "1100 0000", as it's easier to visualize.

In reality I have 32 steps, so it would be "1110 0000 0000 0000 0000 0000 0000 0001", to run my process on step 1,2,3, and 32

Any suggestion about how to implement this?

Why not a simple switch:case?

switch (counter){ case 1: // code break; case 2: // code break; case 3: //code break; case 32: // code break; } // end switch

The same code runs anytime the steps match the numbers, so that doesn't simply my sketch to do that way..

Ok, here's another approach (with a sample sketch):

``````typedef void (*fptr_t)(void);

void proc0(void)
{  Serial.println("proc0");
}
void proc1(void)
{  Serial.println("proc1");
}
void proc2(void)
{  Serial.println("proc2");
}
void proc3(void)
{  Serial.println("proc3");
}

fptr_t proc[32] = { proc0, proc1, proc2, proc3 };
fptr_t p;
unsigned char counter;

void setup(void)
{  Serial.begin(115200);
Serial.println();
}
void loop(void)
{  if (p = proc[counter]) p();
counter = ++counter % 32;
}
``````

I don’t understand what your are trying to do but maybe something like this will help

``````char switches[] = "11100000000000000000000000000001";
for (int counter=1; counter<=32; counter++) {
if (switches[counter-1] == '1') {
... do something here ...
}
}
``````

This gives you a visual representation of when your code will execute.
Your code will execute when “counter” is equal to 1, 2, 3, or 32.

Is this what you want?

Randal seems to be closest to what I'm thinking. I have the same procedure to run regardless of the step - just selecting what steps of the counter I want to run it on with this binary looking numerical string.

Much appreciated.

``````if (counter == 1 || counter == 2 counter == 3 || )
``````

1 or 2 or 3 or what?

besides that why not

if (counter < 4)

we cant really solve your problem cause we have no idea what your trying to do, whatever it is there is a simpler way