# 32 bit binary counter

Hi,
I made a 32 bit binary counter using 5 LEDs etc…
I have attached the code, it is very redundant, but it works.
My question:
is there an easier way to code this? I thought about trying a for loop but can only increment going up or down one at a time.

Thanks

32_bit_binary_Counter.ino (3.29 KB)

Please use code tags instead of attaching your code. I have not seen your code, but a for loop can also increment by two “for (int i = 0; i < range; i+= 2) {}”.

I made a 32 bit binary counter using 5 LEDs

That's quite a compression algorithm you've got.

How do I use the code tags?

Damn there is!

``````const byte LedPins[] = {3, 4, 5, 6, 7};
const byte NrLeds = sizeof(LedPins);
const unsigned int MaxCount = (1 << NrLeds) - 1;

const unsigned int Interval = 400;

unsigned long lastMillis;
unsigned int currentCount;

void setup(){
for(byte i = 0; i < NrLeds; i++){
pinMode(LedPins[i], OUTPUT);
}
}

void loop(){
if(millis() - lastMillis >= Interval){
lastMillis = millis();

if(currentCount < MaxCount){
currentCount++;
}
else{
currentCount = 0;
}

for(byte i = 0; i < NrLeds; i++){
}
}
}
``````

Non blocking and you can scale it up to 16-bit without a problem, just define more pins in LedPins.

``````/* I want to make a binary counter using 5 LEDs, I'm sure that there is an easier
*  way to do this, but I'm not sure what way that is. So I'll try this way and hope
*  for the best...
*  */

int timer = 400;                //sets the pause betweein lighting
int One = 3;                    // LED 1
int Two = 4;                    // LED 2
int Four = 5;                   // LED 3
int Eight = 6;                  // LED 4
int Sixteen = 7;                // LED 5

void setup() {
// put your setup code here, to run once:
pinMode(One, OUTPUT);
pinMode(Two, OUTPUT);
pinMode(Four, OUTPUT);
pinMode(Eight, OUTPUT);
pinMode(Sixteen, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(One, HIGH);      //binary One
delay(timer);
digitalWrite(One, LOW);
digitalWrite(Two, HIGH);      //binary Two
delay(timer);
digitalWrite(One, HIGH);      //binary Three
delay(timer);
digitalWrite(Four,HIGH);      //binary 4
digitalWrite(One, LOW);
digitalWrite(Two, LOW);
delay(timer);
digitalWrite(One, HIGH);      //binary 5
delay(timer);
digitalWrite(Two, HIGH);      //binary 6
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);      //binary 7
delay(timer);
digitalWrite(Eight, HIGH);    //binary 8
digitalWrite(Four, LOW);
digitalWrite(Two, LOW);
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);       //binary 9
delay(timer);
digitalWrite(Two, HIGH);       //binary Ten
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);      // binary eleven
delay(timer);
digitalWrite(Four, HIGH);     //binary twelve
digitalWrite(Two, LOW);
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);      //binary thirteen
delay(timer);
digitalWrite(Two, HIGH);      //binary fourteen
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);      //binary fifteen
delay(timer);
digitalWrite(Sixteen, HIGH);  //binary sixteen
digitalWrite(Eight, LOW);
digitalWrite(Four, LOW);
digitalWrite(Two, LOW);
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);    //binary seventeen
delay(timer);
digitalWrite(Two, HIGH);    // binary eighteen
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);    //binary nineteen
delay(timer);
digitalWrite(Four, HIGH);   //binary twenty
digitalWrite(Two, LOW);
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);    //binary twenty-one
delay(timer);
digitalWrite(Two, HIGH);    //binary twenty-two
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);    //binary twenty-three
delay(timer);
digitalWrite(Eight, HIGH);  //binary twenty-four
digitalWrite(Four, LOW);
digitalWrite(Two, LOW);
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);    //binary twenty-five
delay(timer);
digitalWrite(Two, HIGH);    //binary twenty-six
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);    //binary twenty-seven
delay(timer);
digitalWrite(Four, HIGH);   //binary twenty-eight
digitalWrite(Two, LOW);
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);    //binary twenty-nine
delay(timer);
digitalWrite(Two, HIGH);    //binary thirty
digitalWrite(One, LOW);
delay(timer);
digitalWrite(One, HIGH);    //binary thirty-one
delay(timer);
digitalWrite(Sixteen, LOW); //start counter over
digitalWrite(Eight, LOW);
digitalWrite(Four, LOW);
digitalWrite(Two, LOW);
digitalWrite(One, LOW);
delay(timer);
}
``````

So you are converting a 5 bit value into lighting LED’s? Quite simple…

``````const byte BITPINS[] = {One, Two, Four, Eight, Sixteen};
for (byte i = 1; i <= 32; i++) {
for (byte bit = 0; bit < 5; bit++) digitalWrite(BITPINS[bit], (i & (1 << bit)) ? HIGH : LOW);
delay(timer);
}
``````

EDIT: Corrected a brain fart…

Thank you Danois90, I'll study that, it's a bit more advanced than the course I'm taking, but I'm here to learn.

Thanks again.

Noticed reply #4 as well?

septillion:
Noticed reply #4 as well?

Officially PWND!

I did,
how do I slow down that code? it goes so fast I can't tell what it's doing, the 5th LED flashes and the other 4 stay on. In Danois90's reply he added a delay, I haven't loaded that yet, maybe it will do the same, I don't know.

I used millis() to do the timing. Because delay() will stop the world dead in it's tracks and you're not able to do other stuff. At least that's what I tried but I failed because I messed up the statement. Must be because I'm hungry! (Waiting for the oven to beeeeep).

But fixed it now!

You are using a 400 millisecond delay, this means that the lights change 2.5 times per second. Try to increase your delay to 1000 (1 seconds) or more.

A side note: septillion's code may be easier to understand - but it basically does the same, just without the devil... I mean delay...

See if this works for you, no comments so a learning exercise, hit the books.

``````void setup()
{
DDRD = B11111000;
}

void loop()
{
int interval = 400;
static unsigned long timer;
static byte counter;

if(millis() - timer > interval)
{
timer += interval;
PORTD = counter << 3 & B11111000;
if(++counter > 31)
counter = 0;
}
}
``````

BTW: It’s a 5 bit counter, a 32 bit counter would count to 4294967296.

Would it be too much to ask someone to change the title of this topic?

How do I use the code tags?

HowToPostCode

@ outsider

Wow! that code just blew my mind! Thanks for the introduction to ports, not sure the course I'm taking will go that deep into it, but I will.

And yes, I see, 5 bit counter would have been the correct title for this topic. My bad...

Bare metal, a uC uses ports. But the whole beauty of the Arduino framework is that it makes it easy AND portable. Using ports isn't portable. Yes, it is fast but for a counter you don't need it

Don't say you shouldn't look into it. But there are more interesting / better things to learn as a newbie