4 Turning on the outputs of a 74HC595 using an array
7 * 74HC595 shift register
8 * LEDs attached to each of the outputs of the shift register
11 //Pin connected to ST_CP of 74HC595
13 //Pin connected to SH_CP of 74HC595
15 ////Pin connected to DS of 74HC595
18 //holders for infromation you're going to pass to shifting function
23 //set pins to output because they are addressed in the main loop
24 pinMode(latchPin, OUTPUT);
27 //Arduino doesn't seem to have a way to write binary straight into the code
28 //so these values are in HEX. Decimal would have been fine, too.
29 dataArray[0] = 0xFF; //11111111
30 dataArray[1] = 0xFE; //11111110
31 dataArray[2] = 0xFC; //11111100
32 dataArray[3] = 0xF8; //11111000
33 dataArray[4] = 0xF0; //11110000
34 dataArray[5] = 0xE0; //11100000
35 dataArray[6] = 0xC0; //11000000
36 dataArray[7] = 0x80; //10000000
37 dataArray[8] = 0x00; //00000000
38 dataArray[9] = 0x38; //11100000
40 //function that blinks all the LEDs
41 //gets passed the number of blinks and the pause time
42 blinkAll_2Bytes(2,500);
47 for (int j = 0; j < 10; j++) {
48 //load the light sequence you want from array
50 //ground latchPin and hold low for as long as you are transmitting
51 digitalWrite(latchPin, 0);
53 shiftOut(dataPin, clockPin, data);
54 //return the latch pin high to signal chip that it
55 //no longer needs to listen for information
56 digitalWrite(latchPin, 1);
63 // the heart of the program
64 void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
65 // This shifts 8 bits out MSB first,
66 //on the rising edge of the clock,
69 //internal function setup
72 pinMode(myClockPin, OUTPUT);
73 pinMode(myDataPin, OUTPUT);
75 //clear everything out just in case to
76 //prepare shift register for bit shifting
77 digitalWrite(myDataPin, 0);
78 digitalWrite(myClockPin, 0);
80 //for each bit in the byte myDataOut�
81 //NOTICE THAT WE ARE COUNTING DOWN in our for loop
82 //This means that %00000001 or "1" will go through such
83 //that it will be pin Q0 that lights.
84 for (i=7; i>=0; i--) {
85 digitalWrite(myClockPin, 0);
87 //if the value passed to myDataOut and a bitmask result
88 // true then... so if we are at i=6 and our value is
89 // %11010100 it would the code compares it to %01000000
90 // and proceeds to set pinState to 1.
91 if ( myDataOut & (1<<i) ) {
98 //Sets the pin to HIGH or LOW depending on pinState
99 digitalWrite(myDataPin, pinState);
100 //register shifts bits on upstroke of clock pin
101 digitalWrite(myClockPin, 1);
102 //zero the data pin after shift to prevent bleed through
103 digitalWrite(myDataPin, 0);
107 digitalWrite(myClockPin, 0);
111 //blinks the whole register based on the number of times you want to
112 //blink "n" and the pause between them "d"
113 //starts with a moment of darkness to make sure the first blink
114 //has its full visual effect.
115 void blinkAll_2Bytes(int n, int d) {
116 digitalWrite(latchPin, 0);
117 shiftOut(dataPin, clockPin, 0);
118 shiftOut(dataPin, clockPin, 0);
119 digitalWrite(latchPin, 1);
121 for (int x = 0; x < n; x++) {
122 digitalWrite(latchPin, 0);
123 shiftOut(dataPin, clockPin, 255);
124 shiftOut(dataPin, clockPin, 255);
125 digitalWrite(latchPin, 1);
127 digitalWrite(latchPin, 0);
128 shiftOut(dataPin, clockPin, 0);
129 shiftOut(dataPin, clockPin, 0);
130 digitalWrite(latchPin, 1);