]> git.piffa.net Git - sketchbook_andrea/blob - shift_register/shift_register_custom_pattern_4/shift_register_custom_pattern_4.ino
c5e1da7815840aedea2446a96c9bdd83522bf7c5
[sketchbook_andrea] / shift_register / shift_register_custom_pattern_4 / shift_register_custom_pattern_4.ino
1
2 /*
3   Shift Register Example
4  Turning on the outputs of a 74HC595 using an array
5  
6  Hardware:
7  * 74HC595 shift register
8  * LEDs attached to each of the outputs of the shift register
9  
10  */
11 //Pin connected to ST_CP of 74HC595
12 int latchPin = 8;
13 //Pin connected to SH_CP of 74HC595
14 int clockPin = 12;
15 ////Pin connected to DS of 74HC595
16 int dataPin = 11;
17
18 //holders for infromation you're going to pass to shifting function
19 byte data;
20 byte dataArray[10];
21
22 void setup() {
23   //set pins to output because they are addressed in the main loop
24   pinMode(latchPin, OUTPUT);
25   Serial.begin(9600);
26
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
39
40     //function that blinks all the LEDs
41   //gets passed the number of blinks and the pause time
42   blinkAll_2Bytes(2,500);
43 }
44
45 void loop() {
46
47   for (int j = 0; j < 10; j++) {
48     //load the light sequence you want from array
49     data = dataArray[j];
50     //ground latchPin and hold low for as long as you are transmitting
51     digitalWrite(latchPin, 0);
52     //move 'em out
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);
57     delay(300);
58   }
59 }
60
61
62
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,
67   //clock idles low
68
69   //internal function setup
70   int i=0;
71   int pinState;
72   pinMode(myClockPin, OUTPUT);
73   pinMode(myDataPin, OUTPUT);
74
75   //clear everything out just in case to
76   //prepare shift register for bit shifting
77   digitalWrite(myDataPin, 0);
78   digitalWrite(myClockPin, 0);
79
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);
86
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) ) {
92       pinState= 1;
93     }
94     else { 
95       pinState= 0;
96     }
97
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);
104   }
105
106   //stop shifting
107   digitalWrite(myClockPin, 0);
108 }
109
110
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);
120   delay(200);
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);
126     delay(d);
127     digitalWrite(latchPin, 0);
128     shiftOut(dataPin, clockPin, 0);
129     shiftOut(dataPin, clockPin, 0);
130     digitalWrite(latchPin, 1);
131     delay(d);
132   }
133 }
134
135
136