X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=hardware%2Fshift_register%2Fshift_register_serial%2Fshift_register_serial.ino;fp=hardware%2Fshift_register%2Fshift_register_serial%2Fshift_register_serial.ino;h=38b5d7cbe90b90863161c20bb3e68c27e4713955;hb=2e29cbe13965809a5d3866ccdc12b1d665b54115;hp=0000000000000000000000000000000000000000;hpb=11eb80579bf2e63a8244897614542c917f641a47;p=sketchbook_andrea diff --git a/hardware/shift_register/shift_register_serial/shift_register_serial.ino b/hardware/shift_register/shift_register_serial/shift_register_serial.ino new file mode 100644 index 0000000..38b5d7c --- /dev/null +++ b/hardware/shift_register/shift_register_serial/shift_register_serial.ino @@ -0,0 +1,69 @@ +/* + Shift Register Example + for 74HC595 shift register + + This sketch turns reads serial input and uses it to set the pins + of a 74HC595 shift register. + + Hardware: + * 74HC595 shift register attached to pins 8, 12, and 11 of the Arduino, + as detailed below. + * LEDs attached to each of the outputs of the shift register + + Created 22 May 2009 + Created 23 Mar 2010 + by Tom Igoe + + */ + +//Pin connected to latch pin (ST_CP) of 74HC595 +const int latchPin = 8; +//Pin connected to clock pin (SH_CP) of 74HC595 +const int clockPin = 12; +////Pin connected to Data in (DS) of 74HC595 +const int dataPin = 11; + +void setup() { + //set pins to output because they are addressed in the main loop + pinMode(latchPin, OUTPUT); + pinMode(dataPin, OUTPUT); + pinMode(clockPin, OUTPUT); + Serial.begin(9600); + Serial.println("reset"); +} + +void loop() { + if (Serial.available() > 0) { + // ASCII '0' through '9' characters are + // represented by the values 48 through 57. + // so if the user types a number from 0 through 9 in ASCII, + // you can subtract 48 to get the actual value: + int bitToSet = Serial.read() - 48; + + // write to the shift register with the correct bit set high: + registerWrite(bitToSet, HIGH); + } +} + +// This method sends bits to the shift register: + +void registerWrite(int whichPin, int whichState) { +// the bits you want to send + byte bitsToSend = 0; + + // turn off the output so the pins don't light up + // while you're shifting bits: + digitalWrite(latchPin, LOW); + + // turn on the next highest bit in bitsToSend: + bitWrite(bitsToSend, whichPin, whichState); + + // shift the bits out: + shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend); + + // turn on the output so the LEDs can light up: + digitalWrite(latchPin, HIGH); + delay(300); + +} +