1. /*     ---------------------------------------------------------
  2.  *     |  Arduino Experimentation Kit Example Code             |
  3.  *     |  CIRC-02 .: 8 LED Fun :. (Multiple LEDs)   |
  4.  *     ---------------------------------------------------------
  5.  *  
  6.  *  A few Simple LED animations
  7.  *
  8.  * For more information on this circuit http://tinyurl.com/d2hrud
  9.  *
  10.  */
  11.  
  12. /*
  13.  * Which pins have LEDs attached.
  14.  */
  15. static int ledPins[] = {3,4,5,6,7,8,9,10};
  16.  
  17. /* NUMPINS == the numner of elements in the ledPins[] array */
  18. #define NUMPINS (sizeof(ledPins)/sizeof(ledPins[0]))
  19. unsigned int state = 0;
  20.  
  21. /*
  22.  *   Flash all the LEDs on/off once.
  23.  */
  24. void allOnOff() {
  25.     for (int i = 0; i < NUMPINS; i++)
  26.         digitalWrite(ledPins[i], HIGH);
  27.     delay(200);
  28.     for (int i = 0; i < NUMPINS; i++)
  29.         digitalWrite(ledPins[i], LOW);
  30. }
  31.  
  32. /*
  33.  *   Turn all the LEDs off.
  34.  */
  35. void allOff() {
  36.     for (int i = 0; i < NUMPINS; i++)
  37.         digitalWrite(ledPins[i], LOW);
  38. }
  39.  
  40. /*
  41.  * oneAfterAnotherLoop() - Will light one LED then delay for delayTime then light
  42.  * the next LED until all LEDs are on it will then turn them off one after another
  43.  *
  44.  * this does it using a loop which makes for a lot less typing.
  45.  * than oneOnAtATimeNoLoop() does exactly the same thing with less typing
  46.  */
  47. void oneAfterAnotherLoop(){
  48.     int delayTime = 100; //the time (in milliseconds) to pause between LEDs
  49.                          //make smaller for quicker switching and larger for slower
  50.  
  51.     //Turn Each LED on one after another
  52.     for(int i = 0; i <= 7; i++){
  53.         digitalWrite(ledPins[i], HIGH);  //Turns on LED #i each time this runs i
  54.         delay(delayTime);                //gets one added to it so this will repeat
  55.     }                                  //8 times the first time i will = 0 the final
  56.     //time i will equal 7;
  57.  
  58.     //Turn Each LED off one after another
  59.     for(int i = 7; i >= 0; i--){  //same as above but rather than starting at 0 and counting up
  60.         //we start at seven and count down
  61.         digitalWrite(ledPins[i], LOW);  //Turns off LED #i each time this runs i
  62.         delay(delayTime);                //gets one subtracted from it so this will repeat
  63.     }                                  //8 times the first time i will = 7 the final
  64.     //time it will equal 0
  65. }
  66.  
  67. /*
  68.  * oneOnAtATime() - Will light one LED then the next turning off all the others
  69.  */
  70. void oneOnAtATime(){
  71.     int delayTime = 100; //the time (in milliseconds) to pause between LEDs
  72.     //make smaller for quicker switching and larger for slower
  73.  
  74.     for(int i = 0; i <= 7; i++){
  75.         int offLED = i - 1;  //Calculate which LED was turned on last time through
  76.         if(i == 0)          //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
  77.             offLED = 7;        //turn on LED 2 and off LED 1)
  78.         //however if i = 0 we don't want to turn of led -1 (doesn't exist)
  79.         //instead we turn off LED 7, (looping around)
  80.         digitalWrite(ledPins[i], HIGH);     //turn on LED #i
  81.         digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time
  82.         delay(delayTime);
  83.     }
  84. }
  85.  
  86. /*
  87.  * inAndOut() - This will turn on the two middle LEDs then the next two out
  88.  * making an in and out look
  89.  */
  90. void inAndOut() {
  91.     int delayTime = 100; //the time (in milliseconds) to pause between LEDs
  92.     //make smaller for quicker switching and larger for slower
  93.  
  94.     //runs the LEDs out from the middle
  95.     for(int i = 0; i <= 3; i++){
  96.         int offLED = i - 1;  //Calculate which LED was turned on last time through
  97.         if(i == 0)          //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
  98.             offLED = 3;        //turn on LED 2 and off LED 1)
  99.                             //however if i = 0 we don't want to turn of led -1 (doesn't exist)
  100.         //instead we turn off LED 7, (looping around)
  101.         int onLED1 = 3 - i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED
  102.         //#0 when i = 3
  103.         int onLED2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED
  104.         //#7 when i = 3
  105.         int offLED1 = 3 - offLED; //turns off the LED we turned on last time
  106.         int offLED2 = 4 + offLED; //turns off the LED we turned on last time
  107.  
  108.         digitalWrite(ledPins[onLED1], HIGH);
  109.         digitalWrite(ledPins[onLED2], HIGH);    
  110.         digitalWrite(ledPins[offLED1], LOW);    
  111.         digitalWrite(ledPins[offLED2], LOW);        
  112.         delay(delayTime);
  113.     }
  114.  
  115.     //runs the LEDs into the middle
  116.     for(int i = 3; i >= 0; i--){
  117.         int offLED = i + 1;  //Calculate which LED was turned on last time through
  118.         if(i == 3)          //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
  119.             offLED = 0;        //turn on LED 2 and off LED 1)
  120.                             //however if i = 0 we don't want to turn of led -1 (doesn't exist)
  121.         //instead we turn off LED 7, (looping around)
  122.         int onLED1 = 3 - i;       //this is the first LED to go on ie. LED #3 when i = 0 and LED
  123.         //#0 when i = 3
  124.         int onLED2 = 4 + i;       //this is the first LED to go on ie. LED #4 when i = 0 and LED
  125.         //#7 when i = 3
  126.         int offLED1 = 3 - offLED; //turns off the LED we turned on last time
  127.         int offLED2 = 4 + offLED; //turns off the LED we turned on last time
  128.  
  129.         digitalWrite(ledPins[onLED1], HIGH);
  130.         digitalWrite(ledPins[onLED2], HIGH);    
  131.         digitalWrite(ledPins[offLED1], LOW);    
  132.         digitalWrite(ledPins[offLED2], LOW);        
  133.         delay(delayTime);
  134.     }
  135. }
  136.  
  137. /*
  138.    Progression 1: In ascending order, turn on an LED and
  139.    then turn on/off for all LEDs following it.  Finally,
  140.    turn off the initial LED and move to the next one
  141.    until all are exhausted.
  142.    0,1,2,3,4,5,6,7
  143.    1,2,3,4,5,6,7
  144.    2,3,4,5,6,7
  145.    etc
  146.    */
  147. void progression1() {
  148.     for (int i = 0; i < NUMPINS; i++) {
  149.         digitalWrite(ledPins[i], HIGH);
  150.         for (int j = i + 1; j < NUMPINS; j++) {
  151.             digitalWrite(ledPins[j], HIGH);
  152.             delay(100);
  153.             digitalWrite(ledPins[j], LOW);
  154.         }
  155.  
  156.         digitalWrite(ledPins[i], LOW);
  157.     }
  158. }
  159.  
  160.  
  161. /*
  162.    Progression 2: In ascending order, turn on an LED and
  163.    then swing up and back through the remaining lEDs, turning
  164.    each on/off in order.  Finally, turn off the initial LED
  165.    and move on to the next one until all are exhausted:
  166.    0,1,2,3,4,5,6,7,6,5,4,3,2,1,0
  167.    1,2,3,4,5,6,7,6,5,4,3,2,1
  168.    2,3,4,5,6,7,6,5,4,3,2
  169.    etc.
  170.    */
  171. void progression2() {
  172.     for (int i = 0; i < NUMPINS; i++) {
  173.         digitalWrite(ledPins[i], HIGH);
  174.  
  175.         for (int j = i + 1; j < NUMPINS; j++) {
  176.             digitalWrite(ledPins[j], HIGH);
  177.             delay(100);
  178.             digitalWrite(ledPins[j], LOW);
  179.         }
  180.  
  181.         for (int j = NUMPINS - 1; j > i; j--) {
  182.             digitalWrite(ledPins[j], HIGH);
  183.             delay(100);
  184.             digitalWrite(ledPins[j], LOW);
  185.         }
  186.  
  187.         digitalWrite(ledPins[i], LOW);
  188.     }
  189. }
  190.  
  191.  
  192. /*
  193.    Progression 3: In ascending order, turn on an LED and
  194.    then turn on/off all remaining LEDs in order including
  195.    the ones that proceed it:
  196.    0,1,2,3,4,5,6,7
  197.    1,2,3,4,5,6,7,0
  198.    2,3,4,5,6,7,0,1
  199.    etc.
  200.    */
  201. void progression3() {
  202.     for (int i = 0; i < NUMPINS; i++) {
  203.         digitalWrite(ledPins[i], HIGH);
  204.  
  205.         for (int j = i + 1; j < i + NUMPINS; j++) {
  206.             digitalWrite(ledPins[j% NUMPINS], HIGH);
  207.             delay(100);
  208.             digitalWrite(ledPins[j%NUMPINS], LOW);
  209.         }
  210.  
  211.         digitalWrite(ledPins[i], LOW);
  212.     }
  213. }
  214.  
  215.  
  216. /*
  217.    Progression 4: In ascending order, turn on an LED
  218.    and then swing through all the remaining LEDs
  219.    including the ones that precede the starting point:
  220.    0,1,2,3,4,5,6,7,6,5,4,3,2,1,0
  221.    1,2,3,4,5,6,7,0,7,6,5,4,3,2,1
  222.    2,3,4,5,6,7,0,1,0,7,6,5,4,3,2
  223.    etc.
  224.    */
  225. void progression4() {
  226.     for (int i = 0; i < NUMPINS; i++) {
  227.         digitalWrite(ledPins[i], HIGH);
  228.  
  229.         for (int j = i + 1; j < i + NUMPINS; j++) {
  230.             digitalWrite(ledPins[j % NUMPINS], HIGH);
  231.             delay(100);
  232.             digitalWrite(ledPins[j % NUMPINS], LOW);
  233.         }
  234.  
  235.         for (int j = i + NUMPINS - 2; j > i; j--) {
  236.             digitalWrite(ledPins[j % NUMPINS], HIGH);
  237.             delay(100);
  238.             digitalWrite(ledPins[j % NUMPINS], LOW);
  239.         }
  240.  
  241.         digitalWrite(ledPins[i], LOW);
  242.     }
  243. }
  244.  
  245.  
  246. /*
  247.    Progression 5: In ascending order, turn on an LED and then each
  248.    following it without turning every one.  Once the loop is complete
  249.    turn all LEDs off and move to the next LED in order and repeat
  250.    0,1,2,3,4,5,6,7, all off
  251.    1,2,3,4,5,6,7, all off
  252.    2,3,4,5,6,7, all off
  253.    etc.
  254.    */
  255. void progression5() {
  256.     for (int i = 0; i < NUMPINS; i++) {
  257.         for (int j = i; j < NUMPINS; j++) {
  258.             digitalWrite(ledPins[j], HIGH);
  259.             delay(100);
  260.         }
  261.  
  262.         delay(100);
  263.         allOff();
  264.     }
  265. }
  266.  
  267.  
  268. /*
  269.    Progression 6: Swing back and forth through the LEDs
  270.    moving inwards with each iteration, sort of like a
  271.    swing losing its momentum
  272.    0,1,2,3,4,5,6,7,6,5,4,3,2,1,0
  273.    1,2,3,4,5,6,5,4,3,2,1
  274.    2,3,4,5,4,3,2
  275.    3,4,3
  276.    */
  277. void progression6() {
  278.     for (int i = 0; i < NUMPINS/2; i++) {
  279.         for (int j = i; j < NUMPINS-i; j++){
  280.             digitalWrite(ledPins[j], HIGH);
  281.             delay(100 * (i + 1));
  282.         }
  283.  
  284.         for (int j = NUMPINS - 1 - i; j > i; j--) {
  285.             digitalWrite(ledPins[j], LOW);
  286.             delay(100 * (i + 1));
  287.         }
  288.         allOff();
  289.     }
  290. }
  291.  
  292.  
  293. /*
  294.  * cylonScan() - Scan left to right to left to right..
  295.  */
  296. void cylonScan() {
  297.     //the time (in milliseconds) to pause between LEDs
  298.     // make smaller for quicker switching and larger for slower
  299.     int delayTime = 100;
  300.  
  301.     for(int i = 0; i <= 7; i++) {
  302.         int offLED = i - 1;     // Calculate which LED was turned on last time through
  303.         if(i == 0)              // for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
  304.             offLED = 7;         // turn on LED 2 and off LED 1)
  305.         // however if i = 0 we don't want to turn of led -1 (doesn't exist)
  306.         // instead we turn off LED 7, (looping around)
  307.         digitalWrite(ledPins[i], HIGH); //turn on LED #i
  308.         digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time
  309.         delay(delayTime);
  310.     }
  311.  
  312.     for(int i = 7; i >= 0; i--) {
  313.         int offLED = i + 1;     // Calculate which LED was turned on last time through
  314.         if(i == 7)              // for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
  315.             offLED = 0;         // turn on LED 2 and off LED 1)
  316.         // however if i = 0 we don't want to turn of led -1 (doesn't exist)
  317.         // instead we turn off LED 7, (looping around)
  318.         digitalWrite(ledPins[i], HIGH); //turn on LED #i
  319.         digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time
  320.         delay(delayTime);
  321.     }
  322. }
  323.  
  324. void randomized(){
  325.     const int maxDelay = 250, totalTime = 5000;
  326.     int delayTime, randomLED;
  327.  
  328.     for (int i=0; i < totalTime; i+=delayTime) {
  329.         randomLED = random(0,NUMPINS);
  330.         delayTime = random(0, maxDelay);
  331.         digitalWrite(ledPins[randomLED], HIGH);
  332.         delay(delayTime);
  333.         digitalWrite(ledPins[randomLED], LOW);
  334.     }  
  335. }
  336.  
  337. /*
  338.  * setup() - this function runs once when you turn your Arduino on
  339.  * We the three control pins to outputs
  340.  */
  341. void setup() {
  342.     for(int i = 0; i < NUMPINS; i++)
  343.         pinMode(ledPins[i],OUTPUT);
  344.     // pinMode(buttonPin, INPUT);
  345.  
  346.     Serial.begin(115200);
  347. }
  348.  
  349. /* Declare an array of function pointers and statically
  350.  * assign all the functions to it.
  351.  */
  352. static void (*functions[])(void) = {
  353.     oneAfterAnotherLoop,
  354.     oneOnAtATime,
  355.     inAndOut,
  356.     cylonScan,
  357.     randomized,
  358.     progression1,
  359.     progression2,
  360.     progression3,
  361.     progression4,
  362.     progression5,
  363.     progression6,
  364. };
  365. #define NUMFUNCS (sizeof(functions)/sizeof(functions[0]))
  366.  
  367. /*
  368.  * loop() - this function will start after setup finishes and then repeat
  369.  * we call a function called oneAfterAnother(). if you would like a different behaviour
  370.  * uncomment (delete the two slashes) one of the other lines
  371.  */
  372. void loop() {
  373.     // Reset the LED with a brief all on all off flash
  374.     delay(500);
  375.     allOnOff();
  376.     delay(500);
  377.  
  378.     // Do the new function
  379.     functions[state]();
  380.  
  381.     // update the function pointer off set for next time.
  382.     state=(state+1)%NUMFUNCS;
  383. }