#define SPEED_MS 300
// change LED patterns here
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 setup() {
// set pin 2 to 5 as outputs
for (int i = 2; i <= 5; i++) {
pinMode(i, OUTPUT);
}
}
void loop() {
DisplayPattern(led_pattern, sizeof(led_pattern));
delay(SPEED_MS);
}
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 <= 5; 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;
}
}