Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ---------------------------------------------------------
- * | Arduino Experimentation Kit Example Code |
- * | CIRC-02 .: 8 LED Fun :. (Multiple LEDs) |
- * ---------------------------------------------------------
- *
- * A few Simple LED animations
- *
- * For more information on this circuit http://tinyurl.com/d2hrud
- *
- */
- /*
- * Which pins have LEDs attached.
- */
- static int ledPins[] = {3,4,5,6,7,8,9,10};
- /* NUMPINS == the numner of elements in the ledPins[] array */
- #define NUMPINS (sizeof(ledPins)/sizeof(ledPins[0]))
- unsigned int state = 0;
- /*
- * Flash all the LEDs on/off once.
- */
- void allOnOff() {
- for (int i = 0; i < NUMPINS; i++)
- digitalWrite(ledPins[i], HIGH);
- delay(200);
- for (int i = 0; i < NUMPINS; i++)
- digitalWrite(ledPins[i], LOW);
- }
- /*
- * Turn all the LEDs off.
- */
- void allOff() {
- for (int i = 0; i < NUMPINS; i++)
- digitalWrite(ledPins[i], LOW);
- }
- /*
- * oneAfterAnotherLoop() - Will light one LED then delay for delayTime then light
- * the next LED until all LEDs are on it will then turn them off one after another
- *
- * this does it using a loop which makes for a lot less typing.
- * than oneOnAtATimeNoLoop() does exactly the same thing with less typing
- */
- void oneAfterAnotherLoop(){
- int delayTime = 100; //the time (in milliseconds) to pause between LEDs
- //make smaller for quicker switching and larger for slower
- //Turn Each LED on one after another
- for(int i = 0; i <= 7; i++){
- digitalWrite(ledPins[i], HIGH); //Turns on LED #i each time this runs i
- delay(delayTime); //gets one added to it so this will repeat
- } //8 times the first time i will = 0 the final
- //time i will equal 7;
- //Turn Each LED off one after another
- for(int i = 7; i >= 0; i--){ //same as above but rather than starting at 0 and counting up
- //we start at seven and count down
- digitalWrite(ledPins[i], LOW); //Turns off LED #i each time this runs i
- delay(delayTime); //gets one subtracted from it so this will repeat
- } //8 times the first time i will = 7 the final
- //time it will equal 0
- }
- /*
- * oneOnAtATime() - Will light one LED then the next turning off all the others
- */
- void oneOnAtATime(){
- int delayTime = 100; //the time (in milliseconds) to pause between LEDs
- //make smaller for quicker switching and larger for slower
- for(int i = 0; i <= 7; i++){
- int offLED = i - 1; //Calculate which LED was turned on last time through
- if(i == 0) //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
- offLED = 7; //turn on LED 2 and off LED 1)
- //however if i = 0 we don't want to turn of led -1 (doesn't exist)
- //instead we turn off LED 7, (looping around)
- digitalWrite(ledPins[i], HIGH); //turn on LED #i
- digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time
- delay(delayTime);
- }
- }
- /*
- * inAndOut() - This will turn on the two middle LEDs then the next two out
- * making an in and out look
- */
- void inAndOut() {
- int delayTime = 100; //the time (in milliseconds) to pause between LEDs
- //make smaller for quicker switching and larger for slower
- //runs the LEDs out from the middle
- for(int i = 0; i <= 3; i++){
- int offLED = i - 1; //Calculate which LED was turned on last time through
- if(i == 0) //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
- offLED = 3; //turn on LED 2 and off LED 1)
- //however if i = 0 we don't want to turn of led -1 (doesn't exist)
- //instead we turn off LED 7, (looping around)
- int onLED1 = 3 - i; //this is the first LED to go on ie. LED #3 when i = 0 and LED
- //#0 when i = 3
- int onLED2 = 4 + i; //this is the first LED to go on ie. LED #4 when i = 0 and LED
- //#7 when i = 3
- int offLED1 = 3 - offLED; //turns off the LED we turned on last time
- int offLED2 = 4 + offLED; //turns off the LED we turned on last time
- digitalWrite(ledPins[onLED1], HIGH);
- digitalWrite(ledPins[onLED2], HIGH);
- digitalWrite(ledPins[offLED1], LOW);
- digitalWrite(ledPins[offLED2], LOW);
- delay(delayTime);
- }
- //runs the LEDs into the middle
- for(int i = 3; i >= 0; i--){
- int offLED = i + 1; //Calculate which LED was turned on last time through
- if(i == 3) //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
- offLED = 0; //turn on LED 2 and off LED 1)
- //however if i = 0 we don't want to turn of led -1 (doesn't exist)
- //instead we turn off LED 7, (looping around)
- int onLED1 = 3 - i; //this is the first LED to go on ie. LED #3 when i = 0 and LED
- //#0 when i = 3
- int onLED2 = 4 + i; //this is the first LED to go on ie. LED #4 when i = 0 and LED
- //#7 when i = 3
- int offLED1 = 3 - offLED; //turns off the LED we turned on last time
- int offLED2 = 4 + offLED; //turns off the LED we turned on last time
- digitalWrite(ledPins[onLED1], HIGH);
- digitalWrite(ledPins[onLED2], HIGH);
- digitalWrite(ledPins[offLED1], LOW);
- digitalWrite(ledPins[offLED2], LOW);
- delay(delayTime);
- }
- }
- /*
- Progression 1: In ascending order, turn on an LED and
- then turn on/off for all LEDs following it. Finally,
- turn off the initial LED and move to the next one
- until all are exhausted.
- 0,1,2,3,4,5,6,7
- 1,2,3,4,5,6,7
- 2,3,4,5,6,7
- etc
- */
- void progression1() {
- for (int i = 0; i < NUMPINS; i++) {
- digitalWrite(ledPins[i], HIGH);
- for (int j = i + 1; j < NUMPINS; j++) {
- digitalWrite(ledPins[j], HIGH);
- delay(100);
- digitalWrite(ledPins[j], LOW);
- }
- digitalWrite(ledPins[i], LOW);
- }
- }
- /*
- Progression 2: In ascending order, turn on an LED and
- then swing up and back through the remaining lEDs, turning
- each on/off in order. Finally, turn off the initial LED
- and move on to the next one until all are exhausted:
- 0,1,2,3,4,5,6,7,6,5,4,3,2,1,0
- 1,2,3,4,5,6,7,6,5,4,3,2,1
- 2,3,4,5,6,7,6,5,4,3,2
- etc.
- */
- void progression2() {
- for (int i = 0; i < NUMPINS; i++) {
- digitalWrite(ledPins[i], HIGH);
- for (int j = i + 1; j < NUMPINS; j++) {
- digitalWrite(ledPins[j], HIGH);
- delay(100);
- digitalWrite(ledPins[j], LOW);
- }
- for (int j = NUMPINS - 1; j > i; j--) {
- digitalWrite(ledPins[j], HIGH);
- delay(100);
- digitalWrite(ledPins[j], LOW);
- }
- digitalWrite(ledPins[i], LOW);
- }
- }
- /*
- Progression 3: In ascending order, turn on an LED and
- then turn on/off all remaining LEDs in order including
- the ones that proceed it:
- 0,1,2,3,4,5,6,7
- 1,2,3,4,5,6,7,0
- 2,3,4,5,6,7,0,1
- etc.
- */
- void progression3() {
- for (int i = 0; i < NUMPINS; i++) {
- digitalWrite(ledPins[i], HIGH);
- for (int j = i + 1; j < i + NUMPINS; j++) {
- digitalWrite(ledPins[j% NUMPINS], HIGH);
- delay(100);
- digitalWrite(ledPins[j%NUMPINS], LOW);
- }
- digitalWrite(ledPins[i], LOW);
- }
- }
- /*
- Progression 4: In ascending order, turn on an LED
- and then swing through all the remaining LEDs
- including the ones that precede the starting point:
- 0,1,2,3,4,5,6,7,6,5,4,3,2,1,0
- 1,2,3,4,5,6,7,0,7,6,5,4,3,2,1
- 2,3,4,5,6,7,0,1,0,7,6,5,4,3,2
- etc.
- */
- void progression4() {
- for (int i = 0; i < NUMPINS; i++) {
- digitalWrite(ledPins[i], HIGH);
- for (int j = i + 1; j < i + NUMPINS; j++) {
- digitalWrite(ledPins[j % NUMPINS], HIGH);
- delay(100);
- digitalWrite(ledPins[j % NUMPINS], LOW);
- }
- for (int j = i + NUMPINS - 2; j > i; j--) {
- digitalWrite(ledPins[j % NUMPINS], HIGH);
- delay(100);
- digitalWrite(ledPins[j % NUMPINS], LOW);
- }
- digitalWrite(ledPins[i], LOW);
- }
- }
- /*
- Progression 5: In ascending order, turn on an LED and then each
- following it without turning every one. Once the loop is complete
- turn all LEDs off and move to the next LED in order and repeat
- 0,1,2,3,4,5,6,7, all off
- 1,2,3,4,5,6,7, all off
- 2,3,4,5,6,7, all off
- etc.
- */
- void progression5() {
- for (int i = 0; i < NUMPINS; i++) {
- for (int j = i; j < NUMPINS; j++) {
- digitalWrite(ledPins[j], HIGH);
- delay(100);
- }
- delay(100);
- allOff();
- }
- }
- /*
- Progression 6: Swing back and forth through the LEDs
- moving inwards with each iteration, sort of like a
- swing losing its momentum
- 0,1,2,3,4,5,6,7,6,5,4,3,2,1,0
- 1,2,3,4,5,6,5,4,3,2,1
- 2,3,4,5,4,3,2
- 3,4,3
- */
- void progression6() {
- for (int i = 0; i < NUMPINS/2; i++) {
- for (int j = i; j < NUMPINS-i; j++){
- digitalWrite(ledPins[j], HIGH);
- delay(100 * (i + 1));
- }
- for (int j = NUMPINS - 1 - i; j > i; j--) {
- digitalWrite(ledPins[j], LOW);
- delay(100 * (i + 1));
- }
- allOff();
- }
- }
- /*
- * cylonScan() - Scan left to right to left to right..
- */
- void cylonScan() {
- //the time (in milliseconds) to pause between LEDs
- // make smaller for quicker switching and larger for slower
- int delayTime = 100;
- for(int i = 0; i <= 7; i++) {
- int offLED = i - 1; // Calculate which LED was turned on last time through
- if(i == 0) // for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
- offLED = 7; // turn on LED 2 and off LED 1)
- // however if i = 0 we don't want to turn of led -1 (doesn't exist)
- // instead we turn off LED 7, (looping around)
- digitalWrite(ledPins[i], HIGH); //turn on LED #i
- digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time
- delay(delayTime);
- }
- for(int i = 7; i >= 0; i--) {
- int offLED = i + 1; // Calculate which LED was turned on last time through
- if(i == 7) // for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
- offLED = 0; // turn on LED 2 and off LED 1)
- // however if i = 0 we don't want to turn of led -1 (doesn't exist)
- // instead we turn off LED 7, (looping around)
- digitalWrite(ledPins[i], HIGH); //turn on LED #i
- digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time
- delay(delayTime);
- }
- }
- void randomized(){
- const int maxDelay = 250, totalTime = 5000;
- int delayTime, randomLED;
- for (int i=0; i < totalTime; i+=delayTime) {
- randomLED = random(0,NUMPINS);
- delayTime = random(0, maxDelay);
- digitalWrite(ledPins[randomLED], HIGH);
- delay(delayTime);
- digitalWrite(ledPins[randomLED], LOW);
- }
- }
- /*
- * setup() - this function runs once when you turn your Arduino on
- * We the three control pins to outputs
- */
- void setup() {
- for(int i = 0; i < NUMPINS; i++)
- pinMode(ledPins[i],OUTPUT);
- // pinMode(buttonPin, INPUT);
- Serial.begin(115200);
- }
- /* Declare an array of function pointers and statically
- * assign all the functions to it.
- */
- static void (*functions[])(void) = {
- oneAfterAnotherLoop,
- oneOnAtATime,
- inAndOut,
- cylonScan,
- randomized,
- progression1,
- progression2,
- progression3,
- progression4,
- progression5,
- progression6,
- };
- #define NUMFUNCS (sizeof(functions)/sizeof(functions[0]))
- /*
- * loop() - this function will start after setup finishes and then repeat
- * we call a function called oneAfterAnother(). if you would like a different behaviour
- * uncomment (delete the two slashes) one of the other lines
- */
- void loop() {
- // Reset the LED with a brief all on all off flash
- delay(500);
- allOnOff();
- delay(500);
- // Do the new function
- functions[state]();
- // update the function pointer off set for next time.
- state=(state+1)%NUMFUNCS;
- }
Add Comment
Please, Sign In to add comment