/*
#######################################################
|| ||
||Title: Binary Counter with a 74HC164 Shift Register||
|| Name: GC1CEO ||
|| Date: 9/17/2024 ||
|| Attribution: ||
|| Based on shiftOutCode, Hello World ||
|| by Carlyn Maw,Tom Igoe, David A. Mellis ||
|| Date: 10/25/2006 Modified: 3/23/2010 ||
|| ||
|| Description: ||
|| ||
|| This counts from 0 to 255 on a 74HC164 ||
|| Shift Register, outputting a binary code ||
|| on its 8 outputs. ||
|| ||
|| ||
#######################################################
*/
//Pin connected to either DSA (Data, Serial A) or DSB (Data, Serial B) of the 74HC164 (which ever isn't used by dataPin) which enables data input
int latchPin = 8;
//Pin connected to CP (Clock Pulse) of 74HC164
int clockPin = 7;
////Pin connected to either DSA (Data Serial A) or DSB (Data Serial B) of the 74HC164 (which ever isn't used by latchPin)
int dataPin = 9;
////Pin connected to inverted MR (Master Reset) of 74HC164
int resetPin = 6;
// delay between each number (in ms)
int delayTime = 1000;
void setup() {
Serial.begin(115200);
//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);
// Setting reset to HIGH to stop reset and setting latch/input enable to HIGH to always enable input
digitalWrite(resetPin, HIGH);
digitalWrite(latchPin, HIGH);
// Cleaning up serial monitor and announcing program has initalized and going to loop
for(int a = 0; a < 25; a++){
Serial.println();
}
}
void loop() {
// count from 0 to 255 and display the number
// on the LEDs
Serial.print("Counting from 0 to 255!");
Serial.println();
// for loop to count from 0 to 255
for (int numberToDisplay = 0; numberToDisplay < 255; numberToDisplay++) {
// shift out the bits:
shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);
//print the number to serial
Serial.print(String(numberToDisplay) + " ");
// new line every 5 counts (and after the initial 0)
if(numberToDisplay % 5 == 0) { Serial.println("");}
// pause before next value
delay(delayTime);
}
// Announcement that count has finished and is now restarting
Serial.print("Count finished! Restarting!");
Serial.println();
}