LED Blinking Patterns

Solving Approach

How do you plan to solve it?

 
 

 

 

 

Code

/*Paste your code here*/
const int incButton = 7;
const int decButton = 8;

const int ledPins[4] = {3,4,5,6};

int counter = 0;

bool lastInc = HIGH;
bool lastDec = HIGH;

unsigned long lastIncTime = 0;
unsigned long lastDecTime = 0;

const unsigned long debounceDelay = 50;

void setup() {
  pinMode(incButton, INPUT_PULLUP);
  pinMode(decButton, INPUT_PULLUP);

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

void loop() {
 
  bool incRead = digitalRead(incButton);

  if (incRead != lastInc) {
    lastIncTime = millis();
  }

  if ((lastIncTime) > debounceDelay) {
    if (incRead == LOW && lastInc == HIGH) {
      counter = (counter + 1) & 0x0F;   
      updateLEDs();
    }
  }

 
  bool decRead = digitalRead(decButton);

  if (decRead != lastDec) {
    lastDecTime = millis();
  }

  if ((lastDecTime) > debounceDelay) {
    if (decRead == LOW && lastDec == HIGH) {
      counter = (counter - 1) & 0x0F;  
      updateLEDs();
    }
  }

  lastInc = incRead;
  lastDec = decRead;
}

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



 

 

 

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!