Implement a 4-bit binary counter

Solving Approach

How do you plan to solve it?

 
 int ledPins[4] = {2,3,4,5};   // LEDs for binary display
int incButton = 6;            // increment button
int decButton = 7;            // decrement button

int counter = 0;

void setup()
{
 for(int i=0;i<4;i++)
 {
   pinMode(ledPins[i], OUTPUT);
 }

 pinMode(incButton, INPUT_PULLUP);
 pinMode(decButton, INPUT_PULLUP);
}

void loop()
{
 // increment
 if(digitalRead(incButton) == LOW)
 {
   counter++;

   if(counter > 15)
     counter = 0;

   delay(200);
 }

 // decrement
 if(digitalRead(decButton) == LOW)
 {
   counter--;

   if(counter < 0)
     counter = 15;

   delay(200);
 }

 displayBinary(counter);
}

void displayBinary(int num)
{
 for(int i=0;i<4;i++)
 {
   digitalWrite(ledPins[i], (num >> i) & 1);
 }
}

 

 

 

Code

/*Paste your code here*/

 

Output

Video

Add a video of the output (know more)

 

 

 

 

 

 

 

 

 

 

Upvote
Downvote

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!