Binary Counter

A series of LEDs that is used to count in Binary Number System

COMPONENTS AND SUPPLIES

220 Ohm Resistor

6

220 Ohm Resistor

6

Small Breadboard

1

InventrKits HERO

The HERO is a derivative of “Arduino UNO R3 Reference design” we just changed up a few things to make it our own. If you’re curious about what goes into our HERO board we published the open-source hardware files on our GitHub.

1

ABOUT THIS PROJECT

2 Buttons are used to add and subract 1 and posts he result in binary

CODE

				
					
				
			

SCHEMATICS

COMMENTS

Leave a Reply

Your email address will not be published. Required fields are marked *

/* * Binary Counter * * This example uses 6 LEDs to count in binary from 0 to 63. * * There are two buttons. One button will add 1 to the count * each time it is pressed, while the other button will * subtract 1 from the count. * * The 6 LEDs are connected to digital pins 2 through 7. * The “add” button is connected to digital pin 12. * The “subtract” button is connected to digital pin 13. * */ // Here we set some global constants // In other words, variables whose values do not change const int add_button=12, subtract_button=13, NumberOfBits=6; // Here we set some global non-constant variables. // In other words, variables whose values can change throughout execution. int OurSixBitNumber[]={0,0,0,0,0,0}, LED_Selector, ButtonIsPressed; void setup() { pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(7,OUTPUT); pinMode(add_button,INPUT); pinMode(subtract_button,INPUT); } void loop() { ButtonIsPressed = digitalRead(add_button); // Equals 1 if button being pressed, 0 if not. if(ButtonIsPressed == 1) { addButtonPressed(); } delay(100); // To prevent spamming ButtonIsPressed = digitalRead(subtract_button); if(ButtonIsPressed == 1) { subButtonPressed(); } resetSelector(); writeLEDs(); } /**************************/ /* /* ADDITIONAL FUNCTIONS: /* addButtonPressed() /* subButtonPressed() /* writeLEDs() /* resetSelector() /* /**************************/ void addButtonPressed() { while(digitalRead(add_button) == 1) { /* do nothing until let go of button */ } //AKA: Add Button being held down still delay(100); //Delay for 100 milliseconds to prevent spamming of buttons resetSelector(); while(LED_Selector >= 0 && ButtonIsPressed != 0) { if(OurSixBitNumber[LED_Selector] == 1) { OurSixBitNumber[LED_Selector] = 0; LED_Selector–; } else { OurSixBitNumber[LED_Selector] = 1; ButtonIsPressed = 0; } } } void subButtonPressed() { while(digitalRead(subtract_button) == 1) { /* do nothing until let go of button */ } //AKA: Subtract Button being held down still delay(100); //Delay for 100 milliseconds to prevent spamming of buttons resetSelector(); while(LED_Selector >= 0 && ButtonIsPressed != 0) // While the LED Selector is greater than zero and the subtract button is 1, then starting from the last LED to the first, do the following: { if(OurSixBitNumber[LED_Selector] == 0) // IF that LED is OFF, turn it on, and continue to the next LED (aka the one before because backwards) { OurSixBitNumber[LED_Selector] = 1; // Turning LED on LED_Selector–; // Continuing to the next (aka the one before because going backwards) LED } else // ELSE (the LED is ON) turn off the LED and set the button state to NOT pressed. { OurSixBitNumber[LED_Selector] = 0; // Turn it off ButtonIsPressed = 0; // Reset Button State } } } void writeLEDs() { for(int temp = 0; temp < NumberOfBits; temp++) { if(OurSixBitNumber[temp] == 1) { digitalWrite(NumberOfBits - temp + 1, HIGH); } else { digitalWrite(NumberOfBits - temp + 1, LOW); } LED_Selector--; } } void resetSelector() { LED_Selector = NumberOfBits - 1; // because arrays start at 0; }
PROJECT AUTHOR
Leave a comment
TEAM MEMBERS
Alexander Eschenauer

Everything

ADDITIONAL CONTRIBUTORS
Alexander Eschenauer

He did all the coding

TABLE OF CONTENTS
Leave a comment
Share on whatsapp
Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on pinterest
Share on email

SIMILAR PROJECTS YOU MIGHT LIKE

I cascaded four SN74HC595 shift registers to control 32 LEDs on 4 registers using 3 wires. I am adding a I2C 20×4 LCD screen to display the register and led values. I might add a standard 16×2 LCD screen to show up it is done.
A simple light meter. It’s not calibrated to any meaningful scale, but the digital readout shows a higher number the brighter is the light falling on the photoresistor.