The initial project blinks the LEDs in ascending order, and then repeats. Wiring the board took longer than building the project
The original project source
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; void setup() { for (int i = 0; i < 8; i++) { pinMode(ledPins[i], OUTPUT); } } void loop() { oneAfterAnotherNoLoop(); } void oneAfterAnotherNoLoop() { int delayTime = 75; for (int i = 0; i < 8; i++) { digitalWrite(ledPins[i], HIGH); delay(delayTime); digitalWrite(ledPins[i], LOW); delay(delayTime); } }With the board wired up, the blinking LEDs didn't satisfy me. I created a few other projects that used the same wiring. Adding few utility functions, and an amplification array. With this basic set up, each project need only to implement an in_order() function
/** * Finds the number of elements in an array. */ #define NELE(array) (sizeof(array) / sizeof(array[0])) /** * The amount to decay each pin by */ #define DECAY_AMOUNT 75 /** * The number of miliseconds to delay by */ #define DELAY_MS 80 int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; int ledAmp[NELE(ledPins)]; /** * setup * * Invoked by the arduino infrastructure to initialize state. */ void setup() { for (int i = 0; i < NELE(ledPins); i++) { pinMode(ledPins[i], OUTPUT); ledAmp[i] = 0; } } /** * Neverending loop */ void loop() { in_order(); } /** * Decays all the pin outs that are in use * * @param[in] amount the amount to decay the pins by */ void decay_pins(int amount) { for (int i = 0; i < NELE(ledPins); i++) { ledAmp[i] -= amount; if (ledAmp[i] < 0) { ledAmp[i] = 0; } } } /** * Writes all the new pin values */ void write_pins() { for (int i = 0; i < NELE(ledPins); i++) { analogWrite(ledPins[i], ledAmp[i]); } }The high-top fade, the LEDs are faded left to right
Source to in_order
void in_order() { for (int i = 0; i < NELE(ledPins); i++) { ledAmp[i] = 255; /* Set the pin high */ write_pins(); /* Write all pin values */ delay(DELAY_MS); decay_pins(DECAY_AMOUNT); /* Decay all pins */ write_pins(); delay(DELAY_MS); } }
The Battle Star Galactica, where the LEDs are fade left to right, then right to left.
Source to in_order
void in_order() { for (int i = 0; i < NELE(ledPins) - 1; i++) { ledAmp[i + 1] = 255; /* Set the pin high */ ledAmp[i] = 255; /* Set the pin high */ write_pins(); /* Write all pin values */ delay(DELAY_MS); decay_pins(DECAY_AMOUNT); /* Decay all pins */ write_pins(); delay(DELAY_MS); } for (int i = NELE(ledPins); i > 0 ; i--) { ledAmp[i] = 255; /* Set the pin high */ ledAmp[i - 1] = 255; /* Set the pin high */ write_pins(); /* Write all pin values */ delay(DELAY_MS); decay_pins(DECAY_AMOUNT); /* Decay all pins */ write_pins(); delay(DELAY_MS); } }
Source to in_order
void in_order() { int center = NELE(ledPins) / 2; for (int i = 0; i < NELE(ledPins) / 2; i++) { ledAmp[center - i] = 255; /* Set the pin high */ ledAmp[center + i] = 255; /* Set the pin high */ write_pins(); /* Write all pin values */ delay(DELAY_MS); decay_pins(DECAY_AMOUNT); /* Decay all pins */ write_pins(); delay(DELAY_MS); } if (NELE(ledPins) % 2 == 0) { /* NELE(ledPins) is even */ ledAmp[0] = 255; } }
No comments:
Post a Comment