/*
##########################################################
|| ||
|| Title: Serial Read with a 74HC595 Shift Register ||
|| Name: GC1CEO ||
|| Date: 09/17/2024 ||
|| Attribution: ||
|| Based on shiftOutCode, Serial Read ||
|| by Carlyn Maw,Tom Igoe, David A. Mellis ||
|| Date: 10/25/2006 Modified: 3/23/2010 ||
|| ||
|| Description: ||
|| This program takes in a serial input of 0 through ||
||7 and turns on that specific LED (0 - 7) while turning||
|| off the rest. ||
|| ||
##########################################################
*/
//Pin connected to ST_CP (Storage Clock / Latch) of 74HC595
int latchPin = 8;
//Pin connected to SH_CP (Shift Clock) of 74HC595
int clockPin = 12;
////Pin connected to DS / SER (Data/Serial Input) of 74HC595
int dataPin = 11;
////Pin connected to inverted MR / SRCLR (Master Reset / Serial Register Clear) of 74HC595
int resetPin = 10;
////Pin connected to inverted OE (Output Enable) of 74HC595
int oePin = 9;
void setup() {
Serial.begin(9600);
//set pins to output so you can control the shift register and setting button and pot pins to INPUT
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(resetPin, OUTPUT);
pinMode(oePin, OUTPUT);
// Setting reset to HIGH to stop reset and setting output enable to LOW to always enable output
digitalWrite(resetPin, HIGH);
digitalWrite(oePin, LOW);
// Cleaning up serial monitor and announcing program has initalized and going to loop
for(int a = 0; a < 25; a++){
Serial.println("");
}
Serial.println("Type a number from 0 to 7.");
}
void loop() {
if (Serial.available() > 0) {
// ASCII '0' through '97' characters are
// represented by the values 48 through 55.
// so if the user types a number from 0 through 7 in ASCII,
// you can subtract 48 to get the actual value:
String bitToGet = Serial.readString();
// write to the shift register with the correct bit set high:
registerWrite(bitToGet.toInt(), 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);
}