]> git.piffa.net Git - sketchbook_andrea/blob - hardware/shift_register/shift_register_serial/shift_register_serial.ino
clean up
[sketchbook_andrea] / hardware / shift_register / shift_register_serial / shift_register_serial.ino
1 /*
2   Shift Register Example
3  for 74HC595 shift register
4
5  This sketch turns reads serial input and uses it to set the pins
6  of a 74HC595 shift register.
7
8  Hardware:
9  * 74HC595 shift register attached to pins 8, 12, and 11 of the Arduino,
10  as detailed below.
11  * LEDs attached to each of the outputs of the shift register
12
13  Created 22 May 2009
14  Created 23 Mar 2010
15  by Tom Igoe
16
17  */
18
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;
25
26 void setup() {
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);
31   Serial.begin(9600);
32   Serial.println("reset");
33 }
34
35 void loop() {
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;
42
43   // write to the shift register with the correct bit set high:
44     registerWrite(bitToSet, HIGH);
45   }
46 }
47
48 // This method sends bits to the shift register:
49
50 void registerWrite(int whichPin, int whichState) {
51 // the bits you want to send
52   byte bitsToSend = 0;
53
54   // turn off the output so the pins don't light up
55   // while you're shifting bits:
56   digitalWrite(latchPin, LOW);
57
58   // turn on the next highest bit in bitsToSend:
59   bitWrite(bitsToSend, whichPin, whichState);
60
61   // shift the bits out:
62   shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
63
64     // turn on the output so the LEDs can light up:
65   digitalWrite(latchPin, HIGH);
66   delay(300);
67
68 }
69