Created
December 25, 2011 23:47
-
-
Save michaeltwofish/1520033 to your computer and use it in GitHub Desktop.
Arduino LEDs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Declare the pins backwards, so we can use pointer | |
// arithmetic without finding the end of the array | |
int ledPins[] = {9,8,7,6,5,4,3,2}; | |
int pinCount = 8; | |
int highValue = 2 << (pinCount - 1); | |
void setup() { | |
// initialize the digital pins as an output. | |
for (int i = 0 ; i < pinCount; i++) { | |
pinMode(ledPins[i], OUTPUT); | |
} | |
} | |
void loop() { | |
// Binary counter | |
for (int i = 0 ; i < 255; i++) { | |
lightPins(ledPins, 7, i, 100); | |
} | |
lightPins(ledPins, pinCount, 0, 1000); | |
// Left retract | |
int n = 0; | |
for (int pin = pinCount * 2; pin > 0; pin--) { | |
if (pin >= pinCount) { | |
n = highValue + (n >> 1); | |
} else { | |
n = n << 1; | |
} | |
lightPins(ledPins, pinCount, n, 100); | |
} | |
lightPins(ledPins, pinCount, 0, 1000); | |
// Left pass through | |
for (int pin = pinCount * 2; pin > 0; pin--) { | |
if (pin > pinCount) { | |
n = highValue + (n >> 1); | |
} else { | |
n = n >> 1; | |
} | |
lightPins(ledPins, pinCount, n, 100); | |
} | |
lightPins(ledPins, pinCount, 0, 1000); | |
// Right retract | |
for (int pin = pinCount * 2; pin > 0; pin--) { | |
if (pin > pinCount) { | |
n = 1 + (n << 1); | |
} else { | |
n = n >> 1; | |
} | |
lightPins(ledPins, pinCount, n, 100); | |
} | |
lightPins(ledPins, pinCount, 0, 1000); | |
// Right pass through | |
for (int pin = pinCount * 2; pin > 0; pin--) { | |
if (pin > pinCount) { | |
n = 1 + (n << 1); | |
} else { | |
n = n << 1; | |
} | |
lightPins(ledPins, pinCount, n, 100); | |
} | |
lightPins(ledPins, pinCount, 0, 1000); | |
// Random numbers | |
for (int i = 0; i < 10; i++) { | |
lightPins(ledPins, pinCount, (rand() % (2 << pinCount)), 1000); | |
} | |
lightPins(ledPins, pinCount, 0, 1000); | |
} | |
void lightPins(int* pins, int count, int pattern, int duration) { | |
digitalWrite(*pins++, pattern % 2); | |
if (count-- == 1) { | |
delay(duration); | |
return; | |
} | |
lightPins(pins++, count, pattern >> 1, duration); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment