 # Counting evens inside a for loop

Hi Guys and Girls,
I’m having a bit of a issue with some code I’m playing with, I am not a expert with c++ so maybe it’s something obvious, but I didn’t know which ‘keywords’ to be using for a google search. Any ways to the point, my codes a tad long so I am just posting the believed culprit of one of three problems I’m having:

``````//Part of earlier code like defines and stuff//
#include <ht1632c.h>
#include <FHT.h>
#define LOG_OUT 1
#define FHT_N 64

ht1632c dotmatrix = ht1632c(PORTD, 7, 6, 4, 5, GEOM_32x16, 2);

countX = 0;
countY = 0;
//For Loop where code is messing up//

void loop() {
while(1) {
cli();
for (int i = 0 ; i < FHT_N ; i++) {
if ( (i & 0x01) == 0 ){countX++; if(countX >= 31){countX = 0;} }
//^^ Here is where it's screwing up, It counts up every even number because there are 64 Fht and the odds are either duplicates or
//      zero
while(!(ADCSRA & 0x10));
ADCSRA = 0xf5;
byte m = ADCL;
byte j = ADCH;
int k = (j << 8) | m;
k -= 0x0200;
k <<= 6;
fht_input[i] = k;
}
...............
}
``````

Now the problem is the counting, it goes to up to 30 and goes to zero, I have tried replacing the

``````if(countX >= 31){countX = 0;} }
``````

with

``````if(countX >= 32){countX = 0;} }
``````

and

``````if(countX > 31){countX = 0;} }
``````

but when I do that the counter either doesn’t reset or doesn’t count at all. Any help?

``````for ( int i = 0; i < FHT_N; i++ )
{
if ( (i & 0x01) == 0 )
{
countX = ++countX % 30;
}
``````

Thanks, I'll check it out in a few, question though, what is the code doing? this part

countX = ++countX % 30;

I forgot to mention, I need 0-31 on the countX, so would I be changing the 30 to 31?

Well actually, change it to 32.

'%' preforms a modulo returning the remainder of a, in this case, division by 32.

So we increment countX with '++countX' (a 'pre-inccrement' of countX) preform the modulo and assign the value back to 'countX'.

lloyddean:

``````for ( int i = 0; i < FHT_N; i++ )
``````

{
if ( (i & 0x01) == 0 )
{
countX = ++countX % 30;
}

countX = ( FHT_N / 2 ) % 30; // ? or 32?

Rather than testing whether the current value is even, it would be better to write the loop so that it only generated even numbers. You can do that just by incrementing the loop counter by two rather than one.

``````for ( int i = 0; i < FHT_N; i += 2 )
``````

Is there somehow a way that half a series of sequential numbers started from an even number won't be even, even rounded down if the end value is odd?

I agree with GoForSmoke. Maybe this could be done without the for loop.

``````countX = ( FHT_N / 2 ) % 30; // ?  or 32?
``````

Unless countX doesn't start at 0. Then it should be

``````countX = (countX + ((FHT_N + 1) / 2) % 32;  // Add one to include boundary condition
``````

I'm not so sure.

0 1 2 3 4 5 has 3 even numbers if you count 0

0 1 2 3 4 5 6 has 4

3 4 5 6 7 8 has 3

3 4 5 6 7 8 9 has 3

so how about (last number - first number) / 2 + 1?

What I am sure of is that there is a simple algebraic solution.