Advertisement
adamundefined

Circuit 4

Mar 8th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.52 KB | None | 0 0
  1. void setup()
  2. {
  3.   int index;
  4.  
  5.   for(index = 0; index <= 7; index++)
  6.   {
  7.     pinMode(ledPins[index],OUTPUT);
  8.     // ledPins[index] is replaced by the value in the array.
  9.     // For example, ledPins[0] is 2
  10.   }
  11. }
  12.  
  13.  
  14. void loop()
  15. {
  16.  
  17.  
  18.   oneAfterAnotherNoLoop();  // Light up all the LEDs in turn
  19.  
  20.   //oneAfterAnotherLoop();  // Same as oneAfterAnotherNoLoop,
  21.                             // but with much less typing
  22.  
  23.   //oneOnAtATime();         // Turn on one LED at a time,
  24.                             // scrolling down the line
  25.  
  26.   //pingPong();             // Light the LEDs middle to the edges
  27.  
  28.   //marquee();              // Chase lights like you see on signs
  29.  
  30.   //randomLED();            // Blink LEDs randomly
  31. }
  32.  
  33.  
  34. /*
  35. oneAfterAnotherNoLoop()
  36.  
  37. This function will light one LED, delay for delayTime, then light
  38. the next LED, and repeat until all the LEDs are on. It will then
  39. turn them off in the reverse order.
  40.  
  41. This function does NOT use a for() loop. We've done it the hard way
  42. to show you how much easier life can be when you use for() loops.
  43. Take a look at oneAfterAnotherLoop() further down, which does
  44. exactly the same thing with much less typing.
  45. */
  46.  
  47. void oneAfterAnotherNoLoop()
  48. {
  49.   int delayTime = 100; // time (milliseconds) to pause between LEDs
  50.                        // make this smaller for faster switching
  51.  
  52.   // turn all the LEDs on:
  53.  
  54.   digitalWrite(ledPins[0], HIGH);  //Turns on LED #0 (pin 2)
  55.   delay(delayTime);                //wait delayTime milliseconds
  56.   digitalWrite(ledPins[1], HIGH);  //Turns on LED #1 (pin 3)
  57.   delay(delayTime);                //wait delayTime milliseconds
  58.   digitalWrite(ledPins[2], HIGH);  //Turns on LED #2 (pin 4)
  59.   delay(delayTime);                //wait delayTime milliseconds
  60.   digitalWrite(ledPins[3], HIGH);  //Turns on LED #3 (pin 5)
  61.   delay(delayTime);                //wait delayTime milliseconds
  62.   digitalWrite(ledPins[4], HIGH);  //Turns on LED #4 (pin 6)
  63.   delay(delayTime);                //wait delayTime milliseconds
  64.   digitalWrite(ledPins[5], HIGH);  //Turns on LED #5 (pin 7)
  65.   delay(delayTime);                //wait delayTime milliseconds
  66.   digitalWrite(ledPins[6], HIGH);  //Turns on LED #6 (pin 8)
  67.   delay(delayTime);                //wait delayTime milliseconds
  68.   digitalWrite(ledPins[7], HIGH);  //Turns on LED #7 (pin 9)
  69.   delay(delayTime);                //wait delayTime milliseconds  
  70.  
  71.   // turn all the LEDs off:
  72.  
  73.   digitalWrite(ledPins[7], LOW);   //Turn off LED #7 (pin 9)
  74.   delay(delayTime);                //wait delayTime milliseconds
  75.   digitalWrite(ledPins[6], LOW);   //Turn off LED #6 (pin 8)
  76.   delay(delayTime);                //wait delayTime milliseconds
  77.   digitalWrite(ledPins[5], LOW);   //Turn off LED #5 (pin 7)
  78.   delay(delayTime);                //wait delayTime milliseconds
  79.   digitalWrite(ledPins[4], LOW);   //Turn off LED #4 (pin 6)
  80.   delay(delayTime);                //wait delayTime milliseconds
  81.   digitalWrite(ledPins[3], LOW);   //Turn off LED #3 (pin 5)
  82.   delay(delayTime);                //wait delayTime milliseconds
  83.   digitalWrite(ledPins[2], LOW);   //Turn off LED #2 (pin 4)
  84.   delay(delayTime);                //wait delayTime milliseconds
  85.   digitalWrite(ledPins[1], LOW);   //Turn off LED #1 (pin 3)
  86.   delay(delayTime);                //wait delayTime milliseconds
  87.   digitalWrite(ledPins[0], LOW);   //Turn off LED #0 (pin 2)
  88.   delay(delayTime);                //wait delayTime milliseconds  
  89. }
  90.  
  91.  
  92. /*
  93. oneAfterAnotherLoop()
  94.  
  95. This function does exactly the same thing as oneAfterAnotherNoLoop(),
  96. but it takes advantage of for() loops and the array to do it with
  97. much less typing.
  98. */
  99.  
  100. void oneAfterAnotherLoop()
  101. {
  102.   int index;
  103.   int delayTime = 100; // milliseconds to pause between LEDs
  104.                        // make this smaller for faster switching
  105.  
  106.   // Turn all the LEDs on:
  107.  
  108.   // This for() loop will step index from 0 to 7
  109.   // (putting "++" after a variable means add one to it)
  110.   // and will then use digitalWrite() to turn that LED on.
  111.  
  112.   for(index = 0; index <= 7; index++)
  113.   {
  114.     digitalWrite(ledPins[index], HIGH);
  115.     delay(delayTime);                
  116.   }                                  
  117.  
  118.   // Turn all the LEDs off:
  119.  
  120.   // This for() loop will step index from 7 to 0
  121.   // (putting "--" after a variable means subtract one from it)
  122.   // and will then use digitalWrite() to turn that LED off.
  123.  
  124.   for(index = 7; index >= 0; index--)
  125.   {
  126.     digitalWrite(ledPins[index], LOW);
  127.     delay(delayTime);
  128.   }              
  129. }
  130.  
  131.  
  132. /*
  133. oneOnAtATime()
  134.  
  135. This function will step through the LEDs,
  136. lighting only one at at time.
  137. */
  138.  
  139. void oneOnAtATime()
  140. {
  141.   int index;
  142.   int delayTime = 100; // milliseconds to pause between LEDs
  143.                        // make this smaller for faster switching
  144.  
  145.   // step through the LEDs, from 0 to 7
  146.  
  147.   for(index = 0; index <= 7; index++)
  148.   {
  149.     digitalWrite(ledPins[index], HIGH);  // turn LED on
  150.     delay(delayTime);                    // pause to slow down
  151.     digitalWrite(ledPins[index], LOW);   // turn LED off
  152.   }
  153. }
  154.  
  155.  
  156. /*
  157. pingPong()
  158.  
  159. This function will step through the LEDs,
  160. lighting one at at time in both directions.
  161. */
  162.  
  163. void pingPong()
  164. {
  165.   int index;
  166.   int delayTime = 100; // milliseconds to pause between LEDs
  167.                        // make this smaller for faster switching
  168.  
  169.   // step through the LEDs, from 0 to 7
  170.  
  171.   for(index = 0; index <= 7; index++)
  172.   {
  173.     digitalWrite(ledPins[index], HIGH);  // turn LED on
  174.     delay(delayTime);                    // pause to slow down
  175.     digitalWrite(ledPins[index], LOW);   // turn LED off
  176.   }
  177.  
  178.   // step through the LEDs, from 7 to 0
  179.  
  180.   for(index = 7; index >= 0; index--)
  181.   {
  182.     digitalWrite(ledPins[index], HIGH);  // turn LED on
  183.     delay(delayTime);                    // pause to slow down
  184.     digitalWrite(ledPins[index], LOW);   // turn LED off
  185.   }
  186. }
  187.  
  188.  
  189. /*
  190. marquee()
  191.  
  192. This function will mimic "chase lights" like those around signs.
  193. */
  194.  
  195. void marquee()
  196. {
  197.   int index;
  198.   int delayTime = 200; // milliseconds to pause between LEDs
  199.                        // Make this smaller for faster switching
  200.  
  201.   // Step through the first four LEDs
  202.   // (We'll light up one in the lower 4 and one in the upper 4)
  203.  
  204.   for(index = 0; index <= 3; index++) // Step from 0 to 3
  205.   {
  206.     digitalWrite(ledPins[index], HIGH);    // Turn a LED on
  207.     digitalWrite(ledPins[index+4], HIGH);  // Skip four, and turn that LED on
  208.     delay(delayTime);                      // Pause to slow down the sequence
  209.     digitalWrite(ledPins[index], LOW);     // Turn the LED off
  210.     digitalWrite(ledPins[index+4], LOW);   // Skip four, and turn that LED off
  211.   }
  212. }
  213.  
  214.  
  215. /*
  216. randomLED()
  217.  
  218. This function will turn on random LEDs. Can you modify it so it
  219. also lights them for random times?
  220. */
  221.  
  222. void randomLED()
  223. {
  224.   int index;
  225.   int delayTime;
  226.  
  227.   // The random() function will return a semi-random number each
  228.   // time it is called. See http://arduino.cc/en/Reference/Random
  229.   // for tips on how to make random() even more random.
  230.  
  231.   index = random(8);    // pick a random number between 0 and 7
  232.   delayTime = 100;
  233.    
  234.   digitalWrite(ledPins[index], HIGH);  // turn LED on
  235.   delay(delayTime);                    // pause to slow down
  236.   digitalWrite(ledPins[index], LOW);   // turn LED off
  237. }
  238.  
  239. void binaryCount()
  240. {
  241.   for (int i=0; i<256; i++) {
  242.     showBinNumber(i);
  243.     delay(256-i);
  244.   }
  245. }
  246.  
  247. void showBinNumber(int num) {
  248.   for (int i=0; i<8; i++) {
  249.     if (num%2)
  250.       digitalWrite(ledPins[i], HIGH);
  251.     else
  252.       digitalWrite(ledPins[i], LOW);
  253.     num/=2;
  254.   }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement