Advertisement
mcdadec20

Combined shift register code

Jun 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1.  
  2. // Name : Collin McDade and Britney Beatey
  3. // Date : 6/22/17
  4. // Version : 1.0
  5. //Pin connected to ST_CP of 74HC595
  6. int latchPin = 9;
  7. //Pin connected to SH_CP of 74HC595
  8. int clockPin = 10;
  9. ////Pin connected to DS of 74HC595
  10. int dataPin = 8;
  11.  
  12. //holders for infromation you're going to pass to shifting function
  13. byte dataRED;
  14. byte dataGREEN;
  15. byte dataArrayRED[10];
  16. byte dataArrayGREEN[10];
  17.  
  18. void setup() {
  19. //set pins to output because they are addressed in the main loop
  20. pinMode(latchPin, OUTPUT);
  21. Serial.begin(9600);
  22.  
  23. //Arduino doesn't seem to have a way to write binary straight into the code
  24. //so these values are in HEX. Decimal would have been fine, too.
  25. dataArrayRED[0] = 0xFF; //11111111
  26. dataArrayRED[1] = 0xFE; //11111110
  27. dataArrayRED[2] = 0xFC; //11111100
  28. dataArrayRED[3] = 0xF8; //11111000
  29. dataArrayRED[4] = 0xF0; //11110000
  30. dataArrayRED[5] = 0xE0; //11100000
  31. dataArrayRED[6] = 0xC0; //11000000
  32. dataArrayRED[7] = 0x80; //10000000
  33. dataArrayRED[8] = 0x00; //00000000
  34. dataArrayRED[9] = 0xE0; //11100000
  35.  
  36. //Arduino doesn't seem to have a way to write binary straight into the code
  37. //so these values are in HEX. Decimal would have been fine, too.
  38. dataArrayGREEN[0] = 0xFF; //11111111
  39. dataArrayGREEN[1] = 0x7F; //01111111
  40. dataArrayGREEN[2] = 0x3F; //00111111
  41. dataArrayGREEN[3] = 0x1F; //00011111
  42. dataArrayGREEN[4] = 0x0F; //00001111
  43. dataArrayGREEN[5] = 0x07; //00000111
  44. dataArrayGREEN[6] = 0x03; //00000011
  45. dataArrayGREEN[7] = 0x01; //00000001
  46. dataArrayGREEN[8] = 0x00; //00000000
  47. dataArrayGREEN[9] = 0x07; //00000111
  48.  
  49. //function that blinks all the LEDs
  50. //gets passed the number of blinks and the pause time
  51. blinkAll_2Bytes(2,500);
  52. }
  53.  
  54. void loop() {
  55.  
  56.  
  57. for (int j = 0; j < 10; j++) {
  58. //load the light sequence you want from array
  59. dataRED = dataArrayRED[j];
  60. dataGREEN = dataArrayGREEN[j];
  61. //ground latchPin and hold low for as long as you are transmitting
  62. digitalWrite(latchPin, 0);
  63. //move 'em out
  64. shiftOut(dataPin, clockPin, dataGREEN);
  65. shiftOut(dataPin, clockPin, dataRED);
  66. //return the latch pin high to signal chip that it
  67. //no longer needs to listen for information
  68. digitalWrite(latchPin, 1);
  69. delay(300);
  70. }
  71. }
  72.  
  73.  
  74.  
  75. // the heart of the program
  76. void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  77. // This shifts 8 bits out MSB first,
  78. //on the rising edge of the clock,
  79. //clock idles low
  80.  
  81. //internal function setup
  82. int i=0;
  83. int pinState;
  84. pinMode(myClockPin, OUTPUT);
  85. pinMode(myDataPin, OUTPUT);
  86.  
  87. //clear everything out just in case to
  88. //prepare shift register for bit shifting
  89. digitalWrite(myDataPin, 0);
  90. digitalWrite(myClockPin, 0);
  91.  
  92. //for each bit in the byte myDataOut�
  93. //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  94. //This means that %00000001 or "1" will go through such
  95. //that it will be pin Q0 that lights.
  96. for (i=7; i>=0; i--) {
  97. digitalWrite(myClockPin, 0);
  98.  
  99. //if the value passed to myDataOut and a bitmask result
  100. // true then... so if we are at i=6 and our value is
  101. // %11010100 it would the code compares it to %01000000
  102. // and proceeds to set pinState to 1.
  103. if ( myDataOut & (1<<i) ) {
  104. pinState= 1;
  105. }
  106. else {
  107. pinState= 0;
  108. }
  109.  
  110. //Sets the pin to HIGH or LOW depending on pinState
  111. digitalWrite(myDataPin, pinState);
  112. //register shifts bits on upstroke of clock pin
  113. digitalWrite(myClockPin, 1);
  114. //zero the data pin after shift to prevent bleed through
  115. digitalWrite(myDataPin, 0);
  116. }
  117.  
  118. //stop shifting
  119. digitalWrite(myClockPin, 0);
  120. }
  121.  
  122.  
  123. //blinks the whole register based on the number of times you want to
  124. //blink "n" and the pause between them "d"
  125. //starts with a moment of darkness to make sure the first blink
  126. //has its full visual effect.
  127. void blinkAll_2Bytes(int n, int d) {
  128. digitalWrite(latchPin, 0);
  129. shiftOut(dataPin, clockPin, 0);
  130. shiftOut(dataPin, clockPin, 0);
  131. digitalWrite(latchPin, 1);
  132. delay(200);
  133. for (int x = 0; x < n; x++) {
  134. digitalWrite(latchPin, 0);
  135. shiftOut(dataPin, clockPin, 255);
  136. shiftOut(dataPin, clockPin, 255);
  137. digitalWrite(latchPin, 1);
  138. delay(d);
  139. digitalWrite(latchPin, 0);
  140. shiftOut(dataPin, clockPin, 0);
  141. shiftOut(dataPin, clockPin, 0);
  142. digitalWrite(latchPin, 1);
  143. delay(d);
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement