/*
#######################################################
|| ||
||Title: Binary Counter with a 74HC595 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 74HC595 ||
|| Shift Register, outputting a binary code ||
|| on its 8 outputs. ||
|| ||
|| ||
#######################################################
*/
//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;
// delay between each number (in ms)
int delayTime = 500;
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);
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();
}
}
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++) {
// set the latch pin to low so LEDs dont change during shift
digitalWrite(latchPin, LOW);
// shift out the bits:
shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);
//take the latch pin high so the LEDs will light up:
digitalWrite(latchPin, HIGH);
//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();
}