3 for 74HC595 shift register
5 This sketch turns reads serial input and uses it to set the pins
6 of a 74HC595 shift register.
9 * 74HC595 shift register attached to pins 8, 12, and 11 of the Arduino,
11 * LEDs attached to each of the outputs of the shift register
19 //Pin connected to latch pin (ST_CP) of 74HC595
20 const int latchPin = 8;
21 //Pin connected to clock pin (SH_CP) of 74HC595
22 const int clockPin = 12;
23 ////Pin connected to Data in (DS) of 74HC595
24 const int dataPin = 11;
27 //set pins to output because they are addressed in the main loop
28 pinMode(latchPin, OUTPUT);
29 pinMode(dataPin, OUTPUT);
30 pinMode(clockPin, OUTPUT);
32 Serial.println("reset");
36 if (Serial.available() > 0) {
37 // ASCII '0' through '9' characters are
38 // represented by the values 48 through 57.
39 // so if the user types a number from 0 through 9 in ASCII,
40 // you can subtract 48 to get the actual value:
41 int bitToSet = Serial.read() - 48;
43 // write to the shift register with the correct bit set high:
44 registerWrite(bitToSet, HIGH);
48 // This method sends bits to the shift register:
50 void registerWrite(int whichPin, int whichState) {
51 // the bits you want to send
54 // turn off the output so the pins don't light up
55 // while you're shifting bits:
56 digitalWrite(latchPin, LOW);
58 // turn on the next highest bit in bitsToSend:
59 bitWrite(bitsToSend, whichPin, whichState);
61 // shift the bits out:
62 shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
64 // turn on the output so the LEDs can light up:
65 digitalWrite(latchPin, HIGH);