# Binary Counter

## Introduction

This project is very similar to the project “Marching” LEDS. However, we will use the six LEDs to output a six bit binary number instead of “Marching.” The LEDs will act like bits in the sense that when an LED is on, that bit is considered a 1. Similarly, when an LED is off, that bit is considered a 0.

##### After you're done, you should:
• Have a better understanding of binary numbers and how they relate to decimal numbers.
• Understand how to use a for loop to simplify repetitive code.

## Inventory:

Qty Description Typical Image Schematic Symbol Breadboard Image
6 LEDs
6 Resistor (3.3 kΩ)

## Step 1: Circuit Setup

This circuit is the same as in the project “Marching” LEDs, with six LEDs and resistors connected to six different pins.

1. Connect the six wires to pins 2 through 7.
2. Connect the other end of the wires to the breadboard, but with a row between each wire.
3. Connect the LEDs across the valley of the breadboard, with the anode (long end) connected to the wires to the pins.
4. Place the resistors so that they connect to the cathode (short end) of the LEDs and directly to the ground rail.
5. Finally, use a wire to connect ground on the board to the ground rail that is connected to the resistors.

The bitRead(x,n) function works exactly as the name suggests; it reads a bit of a number.

We will use the number 173 as an example for bitRead(). We can convert 173 into an 8 bit binary number, which is 1010 11012. bitRead(173,1) reads the number in the first argument (173) and returns the value of the bit position given by the second argument (1). The value of bit position 1 is 0, since the rightmost bit position is 0. If you need a refresher on how binary conversion works, click the tab on the right or revisit the previous project on “Marching” LEDs.

## Step 2. Software Design

There is only one variable that is required, and it is used to store the binary number to be displayed on the LEDs. Since it is a number, it should be an int. We will name it countGlb since we are using it as a counter and because it is a global variable.

#### The setup() Function

We will continue to apply the same methods used in the previous projects by initializing the variables and pins we need for a sketch in the setup() function. Giving our global variables a starting value is a good way to start the sketch, so let's give countGlb a starting value of 0.

Next, we will set the pinMode and digitalWrite for all the pins we are going to use. However, instead of doing what we have done in the previous projects where we write out each function call for each pin, we can instead use a for loop (similar to what was done in the “Marching” LEDs project) since the LEDs are connected to a contiguous series of pins. Thus, instead of writing out pinMode() and digitalWrite() for each pin, we can just write those functions once and the loop will initialize each pin.

Since we are using this for loop to initialize the pins we are using, the increment should start at our first pin and end at the last pin. The first pin is pin 2, so we will write that the increment variable i starts at 2. The last pin is pin 7. We now have two ways to write the conditional, either inclusive (i<=7) or exclusive (i<8). The code in this project uses the exclusive, but both are logically the same and can be interchanged without issue. Finally, we have the variable increment. Since we want each pin in the loop to be initialized, we will have the simple increment (i++).

This gives us the code below:

                  void setup()
{
countGlb = 0;
for(int i=2; i<8; i++){
pinMode(i,OUTPUT);
digitalWrite(i,LOW);
}
}


#### The loop() Function

In the loop() function, we want to do several things: display the binary number countGlb to the LEDs, increment countGlb for the next loop, and delay() for half a second.

The first thing we do in the loop() function is to write the bits of countGlb to the LEDs. We can use the same process we used in the setup() function, modified slightly due to different starting position (bits start at 0 and our pins start at 2). Since we are iterating through bit positions, we will start at 0 and end at 5. Once again, for the conditional part of the for loop we can either use an inclusive or exclusive conditional statement (either will work without issues).

The important thing about the for loop is the code that it runs. What we wish to do inside the loop is actually pretty simple; we check to see if the ith bit is a 1 or 0, and then write the appropriate value to the corresponding LED. Also, due to the difference between the pin numbers and bit numbers, we must modify i when writing to the pin so that the value goes to the correct pin (The difference is 2, so we just add that).

                  void loop()
{
for(int i=0; i<6; i++){
digitalWrite(i+2,HIGH);
}else{
digitalWrite(i+2,LOW);
}
}
// Increment countGlb and wait a half a second
countGlb++;
delay(500);
}


## Step 3. Compiling the Sketch

Using the new function bitRead() from above, we can directly write the binary representation of a number to some LEDs in a row.

                  int countGlb;

void setup(){
for (int i=2;i<8;i++){
pinMode(i,OUTPUT);
digitalWrite(i,LOW);
}
countGlb = 1;
}

void loop(){
// Go through the six bits of countGlb
for(int i=0;i<6;i++){
/* If the i-th bit of countGlb is a one,
*  turn on the corresponding LED, otherwise
*  turn that LED off.
*/
digitalWrite(i+2,HIGH);
}else{
digitalWrite(i+2,LOW);
}
}
// Increment countGlb and delay half a second.
countGlb++;
delay(500);
}


## Summary:

In this project, we used the same circuit setup as the previous exercise on “Marching” LEDs. Instead of making the LEDs “march,” we will use the six LEDs to output a six bit binary number. When an LED is turned on, that bit is considered a 1. When an LED is turned off, that bit is considered a 0.

##### Core Concepts:
• Binary numbers