Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************
- * Arduino MultiFx LED Controller *
- * Dale Higgs / [email protected] *
- *********************************/
- // 6 PWM Pins on Arduino Micro
- const int LED[] = {3,5,6,9,10,11};
- // Brightness values
- int high = 255;
- int med = 15;
- int low = 3;
- int off = 0;
- // Fx flags to sequence properly
- int minFx = 0;
- int maxFx = 17;
- int curFx = 0;
- int level[6];
- void setup() {
- delay(2500);
- for (int i=0; i<6; i++) {
- level[i] = off;
- analogWrite(LED[i], high);
- delay(50);
- analogWrite(LED[i], off);
- delay(25);
- }
- }
- void loop() {
- strobeLeftRight();
- }
- void playFx() {
- switch (curFx) {
- case 0:
- solid();
- break;
- case 1:
- chaseRight();
- break;
- case 2:
- chaseLeft();
- break;
- case 3:
- chaseLeftRight();
- break;
- case 4:
- strobeChaseRight();
- break;
- case 5:
- strobeChaseLeft();
- break;
- case 6:
- strobeChaseLeftRight();
- break;
- case 7:
- strobe();
- break;
- case 8:
- strobeLeft();
- break;
- case 9:
- strobeRight();
- break;
- case 10:
- strobeLeftRight();
- break;
- case 11:
- strobeIn();
- break;
- case 12:
- strobeIn2();
- break;
- case 13:
- strobeOut();
- break;
- case 14:
- strobeOut2();
- break;
- case 15:
- strobeInOut();
- break;
- case 16:
- knightRider();
- break;
- case 17:
- knightRiderRev();
- break;
- default:
- all(off);
- break;
- }
- }
- void nextFx() {
- curFx++;
- if (curFx > maxFx)
- curFx = minFx;
- }
- void cycle() {
- for (int i=1; i<=5; i++)
- chaseRight();
- for (int i=1; i<=5; i++)
- chaseLeft();
- for (int i=1; i<=5; i++)
- chaseLeftRight();
- for (int i=1; i<=5; i++)
- strobeChaseRight();
- for (int i=1; i<=5; i++)
- strobeChaseLeft();
- for (int i=1; i<=5; i++)
- strobeChaseLeftRight();
- for (int i=1; i<=5; i++)
- strobe();
- for (int i=1; i<=5; i++)
- strobeLeft();
- for (int i=1; i<=5; i++)
- strobeRight();
- for (int i=1; i<=5; i++)
- strobeLeftRight();
- for (int i=1; i<=5; i++)
- strobeIn();
- for (int i=1; i<=5; i++)
- strobeIn2();
- for (int i=1; i<=5; i++)
- strobeOut();
- for (int i=1; i<=5; i++)
- strobeOut2();
- for (int i=1; i<=5; i++)
- strobeInOut();
- for (int i=1; i<=5; i++)
- knightRider();
- for (int i=1; i<=5; i++)
- knightRiderRev();
- }
- // Solid
- void solid() {
- all(high);
- }
- // Chase right
- void chaseRight() {
- // Let's take a timeout and step
- int timeout = 1;
- int step = 5;
- // Turn all LEDs low
- all(low);
- // Chase
- for (int i=1; i<=7; i++) {
- if (i > 1)
- led(i - 1, low);
- if (i <= 6)
- led(i, high, timeout, step);
- }
- delay(100);
- }
- // Chase left
- void chaseLeft() {
- // Let's take a timeout and step
- int timeout = 1;
- int step = 5;
- // Turn all LEDs low
- all(low);
- // Chase
- for (int i=6; i>=0; i--) {
- if (i < 6)
- led(i + 1, low);
- if (i >= 1)
- led(i, high, timeout, step);
- }
- delay(100);
- }
- // Chase left & right
- void chaseLeftRight() {
- chaseLeft();
- chaseRight();
- }
- // Chase right & right
- void chaseRightLeft() {
- chaseRight();
- chaseLeft();
- }
- // Strobe all LEDs
- void strobe() {
- // Turn all LEDs off
- all(off);
- // Strobe LEDs 1-6
- for (int i=1; i<=4; i++) {
- all(255);
- delay(25);
- all(0);
- delay(25);
- }
- }
- // Strobe left side
- void strobeLeft() {
- // Turn all LEDs off
- all(off);
- // Strobe LEDs 1-3
- for (int i=1; i<=4; i++) {
- for (int j=1; j<=3; j++)
- led(j, high);
- delay(25);
- for (int j=1; j<=3; j++)
- led(j, off);
- delay(25);
- }
- }
- // Strobe right side
- void strobeRight() {
- // Turn all LEDs off
- all(off);
- // Strobe LEDs 4-6
- for (int i=1; i<=4; i++) {
- for (int j=4; j<=6; j++)
- led(j, high);
- delay(25);
- for (int j=4; j<=6; j++)
- led(j, off);
- delay(25);
- }
- }
- // Strobe left and right
- void strobeLeftRight() {
- strobeLeft();
- delay(250);
- strobeRight();
- delay(250);
- }
- // Strobe out to in
- void strobeIn() {
- // Turn all LEDs off
- all(off);
- // Strobe LEDs from out to in
- for (int j=1; j<=3; j++) {
- int led1 = j;
- int led2 = 6 - (j - 1);
- for (int i=1; i<=4; i++) {
- led(led1, high);
- led(led2, high);
- delay(25);
- led(led1, off);
- led(led2, off);
- delay(25);
- }
- }
- strobe();
- strobe();
- strobe();
- }
- // Strobe out to in, keep on
- void strobeIn2() {
- // Turn all LEDs off
- all(off);
- // Strobe LEDs from out to in
- for (int i=1; i<=4; i++) {
- led(1, high);
- led(6, high);
- delay(25);
- led(1, off);
- led(6, off);
- delay(25);
- }
- for (int i=1; i<=4; i++) {
- led(1, high);
- led(2, high);
- led(5, high);
- led(6, high);
- delay(25);
- led(1, off);
- led(2, off);
- led(5, off);
- led(6, off);
- delay(25);
- }
- strobe();
- strobe();
- }
- // Strobe out
- void strobeOut() {
- // Turn all LEDs off
- all(off);
- // Strobe LEDs from out to in
- for (int j=3; j>=1; j--) {
- int led1 = j;
- int led2 = 6 - (j - 1);
- for (int i=1; i<=4; i++) {
- led(led1, high);
- led(led2, high);
- delay(25);
- led(led1, off);
- led(led2, off);
- delay(25);
- }
- }
- strobe();
- strobe();
- strobe();
- }
- // Strobe in to out, keep on
- void strobeOut2() {
- // Turn all LEDs off
- all(off);
- // Strobe LEDs from out to in
- for (int i=1; i<=4; i++) {
- led(3, high);
- led(4, high);
- delay(25);
- led(3, off);
- led(4, off);
- delay(25);
- }
- for (int i=1; i<=4; i++) {
- led(2, high);
- led(3, high);
- led(4, high);
- led(5, high);
- delay(25);
- led(2, off);
- led(3, off);
- led(4, off);
- led(5, off);
- delay(25);
- }
- strobe();
- strobe();
- }
- // Strobe inside, then outside
- void strobeInOut() {
- // Turn all LEDs off
- all(off);
- // Strobe LEDs from out to in
- for (int i=1; i<=8; i++) {
- led(3, high);
- led(4, high);
- delay(25);
- led(3, off);
- led(4, off);
- delay(25);
- }
- for (int i=1; i<=8; i++) {
- led(1, high);
- led(2, high);
- led(5, high);
- led(6, high);
- delay(25);
- led(1, off);
- led(2, off);
- led(5, off);
- led(6, off);
- delay(25);
- }
- }
- // Strobe & chase right
- void strobeChaseRight() {
- // Turn all LEDs low
- all(off);
- // Strobe & chase
- for (int i=1; i<=6; i++) {
- for (int j=1; j<=4; j++) {
- led(i, high);
- delay(25);
- led(i, off);
- delay(25);
- }
- }
- }
- // Strobe & chase left
- void strobeChaseLeft() {
- // Turn all LEDs low
- all(off);
- // Strobe & chase
- for (int i=6; i>=1; i--) {
- for (int j=1; j<=4; j++) {
- led(i, high);
- delay(25);
- led(i, off);
- delay(25);
- }
- }
- }
- // Strobe & chase left & right
- void strobeChaseLeftRight() {
- strobeChaseLeft();
- strobeChaseRight();
- }
- // Strobe & chase right & left
- void strobeChaseRightLeft() {
- strobeChaseRight();
- strobeChaseLeft();
- }
- // Knight rider
- void knightRider() {
- // Turn all LEDs off
- all(off);
- int sleep = 25;
- int timeout = 1;
- int step = 10;
- // Start center, go out
- for (int j=3; j>=1; j--) {
- int led1 = j;
- int led2 = 6 - (j - 1);
- led(led1, high, timeout, step);
- led(led2, high, timeout, step);
- delay(sleep);
- }
- // Start out, go center
- for (int j=1; j<=3; j++) {
- int led1 = j;
- int led2 = 6 - (j - 1);
- led(led1, off, timeout, step);
- led(led2, off, timeout, step);
- delay(sleep);
- }
- }
- // Knight rider reversed
- void knightRiderRev() {
- // Turn all LEDs off
- all(off);
- int sleep = 25;
- int timeout = 1;
- int step = 10;
- // Start out, go center
- for (int j=1; j<=3; j++) {
- int led1 = j;
- int led2 = 6 - (j - 1);
- led(led1, high, timeout, step);
- led(led2, high, timeout, step);
- delay(sleep);
- }
- // Start center, go out
- for (int j=3; j>=1; j--) {
- int led1 = j;
- int led2 = 6 - (j - 1);
- led(led1, off, timeout, step);
- led(led2, off, timeout, step);
- delay(sleep);
- }
- }
- // LED control functions
- // Control all LEDs
- void all(int target) {
- for (int i=1; 6<=6; i++)
- led(i, target);
- }
- // Defaults: target, timeout, step
- void led(int n) {
- int index = n - 1;
- if (level[index] > 0)
- led(n, 0);
- else
- led(n, 255);
- }
- // Defaults: timeout, step
- void led(int n, int target) {
- led(n, target, 0);
- }
- // Defaults: step
- void led(int n, int target, int timeout) {
- led(n, target, timeout, 1);
- }
- void led(int n, int target, int timeout, int step) {
- int index = n - 1;
- if (timeout == 0) {
- // No fade
- analogWrite(LED[index], target);
- delay(1);
- } else {
- // Fade
- if (level[index] < target) {
- // Fade in
- for (int x=level[index]; x<=target; x+=step) {
- analogWrite(LED[index], x);
- delay(timeout);
- }
- analogWrite(LED[index], target);
- delay(1);
- } else {
- // Fade out
- for (int x=level[index]; x>=target; x-=step) {
- analogWrite(LED[index], x);
- delay(timeout);
- }
- analogWrite(LED[index], target);
- delay(1);
- }
- }
- // Save current level
- level[index] = target;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement