Advertisement
Guest User

Untitled

a guest
Nov 5th, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. int latchPin = 5;  // Latch pin of 74HC595 is connected to Digital pin 5
  2. int clockPin = 6; // Clock pin of 74HC595 is connected to Digital pin 6
  3. int dataPin = 4;  // Data pin of 74HC595 is connected to Digital pin 4
  4.  
  5. byte leds = 0;    // Variable to hold the pattern of which LEDs are currently turned on or off
  6.  
  7. /*
  8.  * setup() - this function runs once when you turn your Arduino on
  9.  * We initialize the serial connection with the computer
  10.  */
  11. void setup()
  12. {
  13.   // Set all the pins of 74HC595 as OUTPUT
  14.   pinMode(latchPin, OUTPUT);
  15.   pinMode(dataPin, OUTPUT);  
  16.   pinMode(clockPin, OUTPUT);
  17. }
  18.  
  19. /*
  20.  * loop() - this function runs over and over again
  21.  */
  22. void loop()
  23. {
  24.   leds = 0; // Initially turns all the LEDs off, by giving the variable 'leds' the value 0
  25.   updateShiftRegister();
  26.   delay(500);
  27.   for (int i = 0; i < 8; i++) // Turn all the LEDs ON one by one.
  28.   {
  29.     bitSet(leds, i);    // Set the bit that controls that LED in the variable 'leds'
  30.     updateShiftRegister();
  31.     delay(500);
  32.   }
  33. }
  34.  
  35. /*
  36.  * updateShiftRegister() - This function sets the latchPin to low, then calls the Arduino function 'shiftOut' to shift out contents of variable 'leds' in the shift register before putting the 'latchPin' high again.
  37.  */
  38. void updateShiftRegister()
  39. {
  40.    digitalWrite(latchPin, LOW);
  41.    shiftOut(dataPin, clockPin, LSBFIRST, leds);
  42.    digitalWrite(latchPin, HIGH);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement