#define DELAY_MS (100)
const uint8_t LED_PINS[] = {5,6,7,8,9,10,11,12};
const uint8_t NUM_LEDS = sizeof(LED_PINS)/sizeof(uint8_t);
const uint8_t NUM_BIT_PATTERNS = 8;
void setup() {
for ( uint8_t i=0; i < NUM_LEDS; i++) {
pinMode( LED_PINS[i], OUTPUT );
}
}
void write_leds( uint8_t bits ) {
for ( uint8_t i=0; i < NUM_LEDS; i++) {
digitalWrite( LED_PINS[i], (bits >> i) & 1 );
}
}
void test1() {
// Use a static variable "counter" to keep track of
// which LED to light up next.
static uint8_t counter = 0;
uint8_t bits = 0x00;
// Determine the pattern of LED lights using the counter variable.
switch(counter) {
case 0: bits = 0b00000001; break;
case 1: bits = 0b00000010; break;
case 2: bits = 0b00000100; break;
case 3: bits = 0b00001000; break;
case 4: bits = 0b00010000; break;
case 5: bits = 0b00100000; break;
case 6: bits = 0b01000000; break;
case 7: bits = 0b10000000; break;
default: break;
}
// Update the LED outputs
write_leds(bits);
// Increment the "counter" variable by 1, followed by a modulo operator.
// The variable's value is constrained to be within the range of 0 to (NUM_BIT_PATTERNS - 1).
counter = (counter+1) % NUM_BIT_PATTERNS;
// Delay for a few milliseconds
delay(DELAY_MS);
}
const uint8_t BITS_LOOKUP_TABLE[] = {
0b00000001, 0b00000010, 0b00000100, 0b00001000,
0b00010000, 0b00100000, 0b01000000, 0b10000000 };
void test2() {
// Use a static variable "counter" to keep track of
// which LED to light up next.
static uint8_t counter = 0;
// Update the LED outputs
uint8_t bits = BITS_LOOKUP_TABLE[counter];
write_leds( bits );
// Increment the "counter" variable by 1, followed by a modulo operator.
// The variable's value is constrained to be within the range of 0 to (NUM_BIT_PATTERNS - 1).
counter = (counter+1) % NUM_BIT_PATTERNS;
// Delay for a few milliseconds
delay(DELAY_MS);
}
void test3() {
static uint8_t bits = 0b00000001;
// Update the LED outputs using the current value of the bits variable
write_leds( bits );
// Prepare the next bit pattern by using a rotate-left shift operation
bits = (bits << 1) | (bits >> 7);
delay(DELAY_MS);
}
void loop() {
test2();
}