arduino uno 4 leds blinks and other 4 off ?

Thread Starter

a Rob

Joined May 14, 2017
151
Code:
unsigned char led_pattern[] = {
  0x01, 0x02, 0x04, 0x08, 0x04, 0x02, 0x01, 0x00,
  0x06, 0x09, 0x06, 0x09, 0x06, 0x09, 0x06, 0x09,
  0x05, 0x0a, 0x05, 0x0a, 0x05, 0x0a, 0x05, 0x0a
};





void DisplayPattern(unsigned char *pattern, int num_patterns)
{
  static int pattern_num = 0; // keeps count of patterns
  unsigned char mask = 1;     // for testing each bit in pattern

  // do for LEDs on pin 2 to pin 5
for (int i = 2; i <= 9; i++) {
    // check if bit in pattern is set or not and switch LED accordingly
    if (pattern[pattern_num] & mask) {
      digitalWrite(i, HIGH);
    }
    else {
      digitalWrite(i, LOW);
    }
    mask <<= 1;   // adjust mask for checking next bit in pattern
  }
  pattern_num++;  // move to next pattern for next function call
  // keep pattern within limits of pattern array
  if (pattern_num >= num_patterns) {
    pattern_num = 0;
  }
}


i have 8 leds 4 red , 4 blue
only the 1st 4 reds blink randomly but blues are not active ? how come
 

JohnInTX

Joined Jun 26, 2012
4,787
It looks like the upper 4 bits of each byte in the pattern array are 0. When ANDed with the mask bit, the result will always be 0.
 
Last edited:
Top