Advertisement
adamundefined

Circuit 14

Mar 8th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.59 KB | None | 0 0
  1. void loop()
  2. {
  3.   // We're going to use the same functions we played with back
  4.   // in circuit 04, "Multiple LEDs", we've just replaced
  5.   // digitalWrite() with a new function called shiftWrite()
  6.   // (see below). We also have a new function that demonstrates
  7.   // binary counting.
  8.  
  9.   // To try the different functions below, uncomment the one
  10.   // you want to run, and comment out the remaining ones to
  11.   // disable them from running.
  12.  
  13.   oneAfterAnother();      // All on, all off
  14.  
  15.   //oneOnAtATime();       // Scroll down the line
  16.  
  17.   //pingPong();           // Like above, but back and forth
  18.  
  19.   //randomLED();          // Blink random LEDs
  20.  
  21.   //marquee();
  22.  
  23.   //binaryCount();        // Bit patterns from 0 to 255
  24. }
  25.  
  26.  
  27. void shiftWrite(int desiredPin, boolean desiredState)
  28.  
  29. // This function lets you make the shift register outputs
  30. // HIGH or LOW in exactly the same way that you use digitalWrite().
  31.  
  32. // Like digitalWrite(), this function takes two parameters:
  33.  
  34. //    "desiredPin" is the shift register output pin
  35. //    you want to affect (0-7)
  36.  
  37. //    "desiredState" is whether you want that output
  38. //    to be HIGH or LOW
  39.  
  40. // Inside the Arduino, numbers are stored as arrays of "bits",
  41. // each of which is a single 1 or 0 value. Because a "byte" type
  42. // is also eight bits, we'll use a byte (which we named "data"
  43. // at the top of this sketch) to send data to the shift register.
  44. // If a bit in the byte is "1", the output will be HIGH. If the bit
  45. // is "0", the output will be LOW.
  46.  
  47. // To turn the individual bits in "data" on and off, we'll use
  48. // a new Arduino commands called bitWrite(), which can make
  49. // individual bits in a number 1 or 0.
  50. {
  51.   // First we'll alter the global variable "data", changing the
  52.   // desired bit to 1 or 0:
  53.  
  54.   bitWrite(data,desiredPin,desiredState);
  55.  
  56.   // Now we'll actually send that data to the shift register.
  57.   // The shiftOut() function does all the hard work of
  58.   // manipulating the data and clock pins to move the data
  59.   // into the shift register:
  60.  
  61.   shiftOut(datapin, clockpin, MSBFIRST, data);
  62.  
  63.   // Once the data is in the shift register, we still need to
  64.   // make it appear at the outputs. We'll toggle the state of
  65.   // the latchPin, which will signal the shift register to "latch"
  66.   // the data to the outputs. (Latch activates on the high-to
  67.   // -low transition).
  68.  
  69.   digitalWrite(latchpin, HIGH);
  70.   digitalWrite(latchpin, LOW);
  71. }
  72.  
  73.  
  74. /*
  75. oneAfterAnother()
  76.  
  77. This function will light one LED, delay for delayTime, then light
  78. the next LED, and repeat until all the LEDs are on. It will then
  79. turn them off in the reverse order.
  80. */
  81.  
  82. void oneAfterAnother()
  83. {
  84.   int index;
  85.   int delayTime = 100; // Time (milliseconds) to pause between LEDs
  86.                        // Make this smaller for faster switching
  87.  
  88.   // Turn all the LEDs on:
  89.  
  90.   // This for() loop will step index from 0 to 7
  91.   // (putting "++" after a variable means add one to it)
  92.   // and will then use digitalWrite() to turn that LED on.
  93.  
  94.   for(index = 0; index <= 7; index++)
  95.   {
  96.     shiftWrite(index, HIGH);
  97.     delay(delayTime);                
  98.   }
  99.  
  100.   // Turn all the LEDs off:
  101.  
  102.   // This for() loop will step index from 7 to 0
  103.   // (putting "--" after a variable means subtract one from it)
  104.   // and will then use digitalWrite() to turn that LED off.
  105.  
  106.   for(index = 7; index >= 0; index--)
  107.   {
  108.     shiftWrite(index, LOW);
  109.     delay(delayTime);
  110.   }
  111. }
  112.  
  113.  
  114. /*
  115. oneOnAtATime()
  116.  
  117. This function will step through the LEDs, lighting one at at time.
  118. */
  119.  
  120. void oneOnAtATime()
  121. {
  122.   int index;
  123.   int delayTime = 100; // Time (milliseconds) to pause between LEDs
  124.                        // Make this smaller for faster switching
  125.  
  126.   // step through the LEDs, from 0 to 7
  127.  
  128.   for(index = 0; index <= 7; index++)
  129.   {
  130.     shiftWrite(index, HIGH);    // turn LED on
  131.     delay(delayTime);       // pause to slow down the sequence
  132.     shiftWrite(index, LOW); // turn LED off
  133.   }
  134. }
  135.  
  136.  
  137. /*
  138. pingPong()
  139.  
  140. This function will step through the LEDs, lighting one at at time,
  141. in both directions.
  142. */
  143.  
  144. void pingPong()
  145. {
  146.   int index;
  147.   int delayTime = 100; // time (milliseconds) to pause between LEDs
  148.                        // make this smaller for faster switching
  149.  
  150.   // step through the LEDs, from 0 to 7
  151.  
  152.   for(index = 0; index <= 7; index++)
  153.   {
  154.     shiftWrite(index, HIGH);    // turn LED on
  155.     delay(delayTime);       // pause to slow down the sequence
  156.     shiftWrite(index, LOW); // turn LED off
  157.   }
  158.  
  159.   // step through the LEDs, from 7 to 0
  160.  
  161.   for(index = 7; index >= 0; index--)
  162.   {
  163.     shiftWrite(index, HIGH);    // turn LED on
  164.     delay(delayTime);       // pause to slow down the sequence
  165.     shiftWrite(index, LOW); // turn LED off
  166.   }
  167. }
  168.  
  169.  
  170. /*
  171. randomLED()
  172.  
  173. This function will turn on random LEDs. Can you modify it so it
  174. also lights them for random times?
  175. */
  176.  
  177. void randomLED()
  178. {
  179.   int index;
  180.   int delayTime = 100; // time (milliseconds) to pause between LEDs
  181.                        // make this smaller for faster switching
  182.  
  183.   // The random() function will return a semi-random number each
  184.   // time it is called. See http://arduino.cc/en/Reference/Random
  185.   // for tips on how to make random() more random.
  186.  
  187.   index = random(8);    // pick a random number between 0 and 7
  188.  
  189.   shiftWrite(index, HIGH);  // turn LED on
  190.   delay(delayTime);     // pause to slow down the sequence
  191.   shiftWrite(index, LOW);   // turn LED off
  192. }
  193.  
  194.  
  195. /*
  196. marquee()
  197.  
  198. This function will mimic "chase lights" like those around signs.
  199. */
  200.  
  201. void marquee()
  202. {
  203.   int index;
  204.   int delayTime = 200; // Time (milliseconds) to pause between LEDs
  205.                        // Make this smaller for faster switching
  206.  
  207.   // Step through the first four LEDs
  208.   // (We'll light up one in the lower 4 and one in the upper 4)
  209.  
  210.   for(index = 0; index <= 3; index++)
  211.   {
  212.     shiftWrite(index, HIGH);    // Turn a LED on
  213.     shiftWrite(index+4, HIGH);  // Skip four, and turn that LED on
  214.     delay(delayTime);       // Pause to slow down the sequence
  215.     shiftWrite(index, LOW); // Turn both LEDs off
  216.     shiftWrite(index+4, LOW);
  217.   }
  218. }
  219.  
  220.  
  221. /*
  222. binaryCount()
  223.  
  224. Numbers are stored internally in the Arduino as arrays of "bits",
  225. each of which is a 1 or 0. Just like the base-10 numbers we use
  226. every day, The position of the bit affects the magnitude of its
  227. contribution to the total number:
  228.  
  229. Bit position   Contribution
  230. 0              1
  231. 1              2
  232. 2              4
  233. 3              8
  234. 4              16
  235. 5              32
  236. 6              64
  237. 7              128
  238.  
  239. To build any number from 0 to 255 from the above 8 bits, just
  240. select the contributions you need to make. The bits will then be
  241. 1 if you use that contribution, and 0 if you don't.
  242.  
  243. This function will increment the "data" variable from 0 to 255
  244. and repeat. When we send this value to the shift register and LEDs,
  245. you can see the on-off pattern of the eight bits that make up the
  246. byte. See http://www.arduino.cc/playground/Code/BitMath for more
  247. information on binary numbers.
  248. */
  249.  
  250. void binaryCount()
  251. {
  252.   int delayTime = 1000; // time (milliseconds) to pause between LEDs
  253.                         // make this smaller for faster switching
  254.  
  255.   // Send the data byte to the shift register:
  256.  
  257.   shiftOut(datapin, clockpin, MSBFIRST, data);
  258.  
  259.   // Toggle the latch pin to make the data appear at the outputs:
  260.  
  261.   digitalWrite(latchpin, HIGH);
  262.   digitalWrite(latchpin, LOW);
  263.  
  264.   // Add one to data, and repeat!
  265.   // (Because a byte type can only store numbers from 0 to 255,
  266.   // if we add more than that, it will "roll around" back to 0
  267.   // and start over).
  268.  
  269.   data++;
  270.  
  271.   // Delay so you can see what's going on:
  272.  
  273.   delay(delayTime);
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement