All submissions

Implement a 4-bit binary counter

Solving Approach

How do you plan to solve it?

 
 

 

 

 

Code

/*Paste your code here*/
#define LED_1 8
#define LED_2 9
#define LED_3 10
#define LED_4 11

#define bush_button_1 2
#define bush_button_2 3
#define NUMBER_OF_PATTERNS 4

volatile int pattern_counter = 0;

int LED_ARRAY[4] = {LED_1, LED_2, LED_3, LED_4};


void IncrementInterruptHandler(void)
{
  delay(150);

  pattern_counter++;

  if(pattern_counter>=16)
  {
    pattern_counter=0;
  }

}


void DecrementInterruptHandler(void)
{
  delay(150);

  pattern_counter--;

  if(pattern_counter<0)
  {
    pattern_counter=0;
  }

}


void bit_4_conter(uint8_t counter)
{
  for(int i=0; i<4; i++)
  {
    digitalWrite(LED_ARRAY[i],(counter&1));
    counter = counter>>1;
  }

}

void setup() {
  Serial.begin(115200);
  pinMode(LED_1,OUTPUT);
  pinMode(LED_2,OUTPUT);
  pinMode(LED_3,OUTPUT);
  pinMode(LED_4,OUTPUT);
  pinMode(bush_button_1,INPUT_PULLUP);
  pinMode(bush_button_2,INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(bush_button_1), IncrementInterruptHandler, FALLING);
  attachInterrupt(digitalPinToInterrupt(bush_button_2), DecrementInterruptHandler, FALLING);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  bit_4_conter(pattern_counter);
  Serial.print("Counter: ");
  Serial.println(pattern_counter);
  delay(5);
  

}

 

Output

Video

Add a video of the output (know more)

 

 

https://youtube.com/shorts/GyX-3KVtxUs?feature=share

 

 

 

 

 

 

 

Submit Your Solution

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