Arduino Project – 8 Bit Counter

Hey everyone I got a hold of an Arduino Uno from Ebay recently and guess what its time to get my geek on!

Well since this is the first project that’s going to be done I started off something simple because well…like everything there is a learning curve! This project is a simple 8 bit counter using LEDs to show their progress. Its a good way to see just how 0 to 255 is processed in your computers.

What is Binary?

Oks lets get a lil nerdy in that in the world of computers there is only two answers you can ever have, a 1 or 0. Sorry, I feel sorry for Bender, he saw a 2! (Futurama reference). Meaning for electricity purposes you either have a voltage or dont simple as that. And through that you get all the things on your computer.

So in this project I have 8 lines going to resistors that go to my LEDs, just sending out a voltage or not on those lines. You can do the same thing with 8 AA batteries. Think of having some of them connected and the others not connected to make up your 0s and 1s.

Schematic

8 bit counter schematic

The resistors are 470 ohm and the LEDs that Im using at the 3mm kind, using 3V @ 20 mA. Pretty low power consumption, but super bright! And remember LEDs are diodes, meaning they will let power go only in one way. They have two legs, one longer than the other, the longer one points toward power (cathode), shorter one toward ground (anode).

Program

And here is the lovely program!

long previousMillis = 0;
 
//interval in milliseconds till the next loop
long interval = 500;
 
//initalizing the pin var
int pin[] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
void setup()
{
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
}
 
void loop()
{
  //gets timing in milliseconds
  unsigned long currentMillis = millis();
 
  //tests if the time from the previous loop and this one have met requirements
  //of the specified time
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;
 
    //first loop will go through 0 to 255
    for(int x=0; x<255; x++)
    {
      //makes the temp contain a binary version of the number x
       String temp = String(x,BIN);
      //this loop reads the characters in each element
      for(int y=0;y<8; y++)
      {
        //adds in the correct amount of zeros so that it will be a true
        //8-bit digit
        if (temp.length()<8)
        {
          for(int i=temp.length(); i<8; i++){
            //forces a 0 in front of temp
            temp="0"+temp;
          } 
        }
        //statment determining if the character is a 1 or 0 and sets pin 
        //accordingly
        if(temp.charAt(y)=='1')
        {
          pin[y]=HIGH;
        }
        else
        {
          pin[y]=LOW;
        }
        delay(10);
      }
      digitalWrite(2,pin[0]);
      digitalWrite(3,pin[1]);
      digitalWrite(4,pin[2]);
      digitalWrite(5,pin[3]);
      digitalWrite(6,pin[4]);
      digitalWrite(7,pin[5]);
      digitalWrite(8,pin[6]);
      digitalWrite(9,pin[7]);
    } 
  }
}

Sadly I dont have a picture of this but you’ll see a nice graphical way of how binary works going from 0 to 255. I had it set up so that it was vertical the top most would be 2^8 and the the most bottom one would be 2^0.

What’s Binary?

Oks for those who’ve been reading it and its late at night and dont fully remember what binary is…they are 0s and 1s. Very simple but here is the thing…you have 8 of them in a row like 00011100.

The interesting thing is that normally bits like this are put in from the highest value to the lowest so that 2^8 starts then its 2^7, 2^6….2^0. When you add the sum of 2^8 all the way to 2^0 it comes out to 255. To get the numbers in between you add numbers like 2^1+2^0 to get 3.

More Arduino projects to come!