Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. /******************************************************************
  2. * SparkFun Inventor's Kit
  3. * Example sketch 04 -- MULTIPLE LEDs
  4. *
  5. * Make eight LEDs dance. Dance LEDs, dance!
  6. *
  7. * This sketch was written by SparkFun Electronics,
  8. * with lots of help from the Arduino community.
  9. * This code is completely free for any use.
  10. * Visit http://learn.sparkfun.com/products/2 for SIK information.
  11. * Visit http://www.arduino.cc to learn about the Arduino.
  12. *
  13. * Version 2.0 6/2012 MDG
  14. * Version 2.1 9/2014 BCH
  15. /*****************************************************************/
  16.  
  17. int ledPins[] = {2,3,4,5,6,7,8,9}; // Defines an array to store the pin numbers of the 8 LEDs.
  18. // An array is like a list variable that can store multiple numbers.
  19. // Arrays are referenced or "indexed" with a number in the brackets [ ]. See the examples in
  20. // the pinMode() functions below.
  21.  
  22. void setup()
  23. {
  24. // setup all 8 pins as OUTPUT - notice that the list is "indexed" with a base of 0.
  25. pinMode(ledPins[0],OUTPUT); // ledPins[0] = 2
  26. pinMode(ledPins[1],OUTPUT); // ledPins[1] = 3
  27. pinMode(ledPins[2],OUTPUT); // ledPins[2] = 4
  28. pinMode(ledPins[3],OUTPUT); // ledPins[3] = 5
  29. pinMode(ledPins[4],OUTPUT); // ledPins[4] = 6
  30. pinMode(ledPins[5],OUTPUT); // ledPins[5] = 7
  31. pinMode(ledPins[6],OUTPUT); // ledPins[6] = 8
  32. pinMode(ledPins[7],OUTPUT); // ledPins[7] = 9
  33. }
  34.  
  35. void loop()
  36. {
  37.  
  38. // This loop() calls functions that we've written further below.
  39. // We've disabled some of these by commenting them out (putting
  40. // "//" in front of them). To try different LED displays, remove
  41. // the "//" in front of the ones you'd like to run, and add "//"
  42. // in front of those you don't to comment out (and disable) those
  43. // lines.
  44.  
  45. oneAfterAnother(); // Light up all the LEDs in turn
  46.  
  47. //oneOnAtATime(); // Turn on one LED at a time
  48.  
  49. //pingPong(); // Same as oneOnAtATime() but change direction once LED reaches edge
  50.  
  51. //marquee(); // Chase lights like you see on theater signs
  52.  
  53. //randomLED(); // Blink LEDs randomly
  54. }
  55.  
  56.  
  57.  
  58. /******************************************************************
  59. * oneAfterAnother()
  60. *
  61. * This function turns all the LEDs on, pauses, and then turns all
  62. * the LEDS off. The function takes advantage of for() loops and
  63. * the array to do this with minimal typing.
  64. /*****************************************************************/
  65. void oneAfterAnother()
  66. {
  67. int index;
  68. int delayTime = 100; // milliseconds to pause between LEDs
  69. // make this smaller for faster switching
  70.  
  71. // Turn all the LEDs on:
  72. for(index = 0; index <= 7; index = ++index) // step through index from 0 to 7
  73. {
  74. digitalWrite(ledPins[index], HIGH);
  75. delay(delayTime);
  76. }
  77.  
  78. // Turn all the LEDs off:
  79. for(index = 7; index >= 0; index = --index) // step through index from 7 to 0
  80. {
  81. digitalWrite(ledPins[index], LOW);
  82. delay(delayTime);
  83. }
  84. }
  85.  
  86. /*****************************************************************
  87. * oneOnAtATime()
  88. *
  89. * This function will step through the LEDs, lighting only one at
  90. * a time. It turns each LED ON and then OFF before going to the
  91. * next LED.
  92. /****************************************************************/
  93.  
  94. void oneOnAtATime()
  95. {
  96. int index;
  97. int delayTime = 100; // milliseconds to pause between LEDs
  98. // make this smaller for faster switching
  99.  
  100. for(index = 0; index <= 7; index = ++index) // step through the LEDs, from 0 to 7
  101. {
  102. digitalWrite(ledPins[index], HIGH); // turn LED on
  103. delay(delayTime); // pause to slow down
  104. digitalWrite(ledPins[index], LOW); // turn LED off
  105. }
  106. }
  107.  
  108. /*****************************************************************
  109. * pingPong()
  110. *
  111. * This function will step through the LEDs, lighting one at at
  112. * time in both directions. There is no delay between the LED off
  113. * and turning on the next LED. This creates a smooth pattern for
  114. * the LED pattern.
  115. /****************************************************************/
  116. void pingPong()
  117. {
  118. int index;
  119. int delayTime = 100; // milliseconds to pause between LEDs
  120.  
  121. for(index = 0; index <= 7; index = ++index) // step through the LEDs, from 0 to 7
  122. {
  123. digitalWrite(ledPins[index], HIGH); // turn LED on
  124. delay(delayTime); // pause to slow down
  125. digitalWrite(ledPins[index], LOW); // turn LED off
  126. }
  127.  
  128. for(index = 7; index >= 0; index = --index) // step through the LEDs, from 7 to 0
  129. {
  130. digitalWrite(ledPins[index], HIGH); // turn LED on
  131. delay(delayTime); // pause to slow down
  132. digitalWrite(ledPins[index], LOW); // turn LED off
  133. }
  134. }
  135.  
  136. /*****************************************************************
  137. * marquee()
  138. *
  139. * This function will mimic "chase lights" like those around
  140. * theater signs.
  141. /****************************************************************/
  142. void marquee()
  143. {
  144. int index;
  145. int delayTime = 200; // milliseconds to pause between LEDs
  146.  
  147. // Step through the first four LEDs
  148. // (We'll light up one in the lower 4 and one in the upper 4)
  149.  
  150. for(index = 0; index <= 3; index++) // Step from 0 to 3
  151. {
  152. digitalWrite(ledPins[index], HIGH); // Turn a LED on
  153. digitalWrite(ledPins[index+4], HIGH); // Skip four, and turn that LED on
  154. delay(delayTime); // Pause to slow down the sequence
  155. digitalWrite(ledPins[index], LOW); // Turn the LED off
  156. digitalWrite(ledPins[index+4], LOW); // Skip four, and turn that LED off
  157. }
  158. }
  159.  
  160. /*****************************************************************
  161. * randomLED()
  162. *
  163. * This function will turn on random LEDs. Can you modify it so it
  164. * also lights them for random times?
  165. /****************************************************************/
  166. void randomLED()
  167. {
  168. int index;
  169. int delayTime;
  170.  
  171. index = random(8); // pick a random number between 0 and 7
  172. delayTime = 100;
  173.  
  174. digitalWrite(ledPins[index], HIGH); // turn LED on
  175. delay(delayTime); // pause to slow down
  176. digitalWrite(ledPins[index], LOW); // turn LED off
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement