I haven't found anyone who did it like this. It doesn't use modulo or binary masks, or any fancy library functions.

Basically, the idea is that any given bit will go from zero to one if all the bits that are less-significant than it were ones. So if you find *"n" * continuous ones beginning from the least significant bit, flip the *"n+1" * least significant bits.

Here's one way I did it:

/*

This uses the 10 LED display

It's a binary counter written by Andrew Ghattas

*/

/* sorry, I didn't bother checking if this boolean array was necessary or if I could use a pin output state in an "if" statement

*/

bool pinstates[] = {false,false,false,false,false,false,false,false,false,false};

// you can arrange these the opposite way, depending on which way your circuit is "looking" at you

int ledPins[] = {12,11,10,9,8,7,6,5,4,3};

// the setup function runs once when you press reset or power the board

void setup() {

// initialize digital pins as outputs

for (int i = 0; i < 10;i++) {

pinMode(ledPins[i], OUTPUT); }

}

// the loop function runs over and over again forever

void loop() {

delay(1000);

if (pinstates[8] && pinstates[7] && pinstates[6] && pinstates[5] && pinstates[4] && pinstates[3] && pinstates[2] && pinstates[1] && pinstates[0])

fliploop(9);

else if (pinstates[7] && pinstates[6] && pinstates[5] && pinstates[4] && pinstates[3] && pinstates[2] && pinstates[1] && pinstates[0])

fliploop(8);

else if (pinstates[6] && pinstates[5] && pinstates[4] && pinstates[3] && pinstates[2] && pinstates[1] && pinstates[0])

fliploop(7);

else if (pinstates[5] && pinstates[4] && pinstates[3] && pinstates[2] && pinstates[1] && pinstates[0])

fliploop(6);

else if (pinstates[4] && pinstates[3] && pinstates[2] && pinstates[1] && pinstates[0])

fliploop(5);

else if (pinstates[3] && pinstates[2] && pinstates[1] && pinstates[0])

fliploop(4);

else if (pinstates[2] && pinstates[1] && pinstates[0])

fliploop(3);

else if (pinstates[1] && pinstates[0])

fliploop(2);

else if (pinstates[0])

fliploop(1);

else

fliploop(0);

}

//starting from least significant bits

void fliploop(int numberofbitstoflip) {

for (int i = 0; i <= numberofbitstoflip; i++)

flipbit(i);

}

void flipbit(int i) {

pinstates[i] = ! pinstates[i];

if (pinstates[i])

digitalWrite(ledPins[i], HIGH);

else

digitalWrite(ledPins[i],LOW);

}

A more elegant way of writing it looks like this:

/*

This uses the 10 LED display to display a binary counter.

Written by Andrew Ghattas.

*/

int ledPins[10] = {12,11,10,9,8,7,6,5,4,3};

const int numberofleds = 10;

/* pins start at zero

sorry, I didn't bother checking if this array was necessary or if I could use pin output states in an "if" statement

*/

int pinstates[10] = {0,0,0,0,0,0,0,0,0,0};

// the setup function runs once when you press reset or power the board

void setup() {

// initialize digital pins as outputs.

for (int i = 0; i < numberofleds;i++)

pinMode(ledPins[i], OUTPUT);

}

// the loop function runs over and over again forever

void loop() {

delay(100);

fliploop(leastsignificantones());

}

int leastsignificantones() {

int i = 0;

while (pinstates[i] == 1 && i <= numberofleds - 2) {

i = i + 1;

}

return i ;

}

//starting from least significant bits

void fliploop(int numberofbitstoflip) {

for (int i = 0; i <= numberofbitstoflip; i++)

flipbit(i);

}

void flipbit(int i) {

if (pinstates[i] == 0) {

digitalWrite(ledPins[i], HIGH);

pinstates[i] = 1;

}

else {

digitalWrite(ledPins[i],LOW);

pinstates[i] = 0;

}

}

I hope you find this code insightful... or at least entertaining!