LED Blinking Patterns

Solving Approach

How do you plan to solve it?

 Pattern Array: This 2D array will store patterns of led blinking in uint8_t and stores value in hex (0xFF). There are five rows which defines pattern number (patter1, pattern2, ... etc.) and 4 columns which defines led blinking pattern (All ON, all OFF, etc). 

Place Value: This function controls led stats based on uint8_t argument it tooks. A particular row and column value of pattern array is send as argument to the function. Delay of overall cycle is 2 second so delay of 500ms is given in the function.

Button functions: Assigning but (GPIO2) as input pullup. but_val integer is used to store numbers from 0 to 4 which decides which pattern should be displayed.

Loop Function: Code executes here. If the button is pressed, the but_val incremented by 1 with modulo 5. This prevents but_val to leave 0 to 4 range. Inside for loop, bitAssign function is called to display pattern of LED. The argument of the function is 'but_val' row and ith column. Button press is only recognised after the for loop.

Code

int but = 2;
int but_val = 0;

int led_arr[4] = {8,9,10,11};
uint8_t pattern[5][4] = {
  {0x0F, 0x00, 0x0F, 0x00},
  {0x05, 0x0A, 0x05, 0x0A},
  {0x01, 0x02, 0x04, 0x08},
  {0x03, 0x0C, 0x03, 0x0C},
  {0x09, 0x06, 0x09, 0x06},
};

void bitAssign(uint8_t val){
  for(int i=0; i<4; i++){
    digitalWrite(led_arr[i], (val>>i)&1);
  }
  delay(500);
}

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

void loop(){
  if(digitalRead(but) == 0){
    but_val = (but_val+1)%5;
  }
  for(int i=0; i<4; i++){
    bitAssign(pattern[but_val][i]);
  }
}

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!