//**************************************************************//
// Name : Register74HC595 //
// Author : Andy.193 //
// Date : 04.07.2023 //
// Version : 1.0 //
// Notes : Code for using a 74HC595 Shift Register //
// : to show the number from the Poti. //
//****************************************************************
//Pin connected to ST_CP of 74HC595
int latchPin = 4;
//Pin connected to SH_CP of 74HC595
int clockPin = 2;
////Pin connected to DS of 74HC595
int dataPin = 15;
void Register74HC595(int dataPin, int clockPin, int latchPin, String ShowOutput, int resetPin = 13, bool Present = true, bool WithDot = false){
/*
//If COM+ then shiftOut() '+ DotDigit'
const byte digitPatterns[] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110 // 9
};*/
//If COM- then shiftOut() '- DotDigit'
const byte digitPatterns[] = {
B00000011, // 0
B10011111, // 1
B00100101, // 2
B00001101, // 3
B10011001, // 4
B01001001, // 5
B01000001, // 6
B00011111, // 7
B00000001, // 8
B00001001 // 9
};
for(int i = 0; i < ShowOutput.length(); i++){
shiftOut(dataPin, clockPin, LSBFIRST, B00000000);
}
digitalWrite(resetPin, LOW);
delay(10);
if(Present) digitalWrite(resetPin, HIGH);
digitalWrite(latchPin, LOW);
for(int i = ShowOutput.length()-1; i >= 0; i--){
int DotDigit = 0;
if(i == ShowOutput.length()-1 && WithDot) DotDigit = 1;
shiftOut(dataPin, clockPin, LSBFIRST, digitPatterns[String(ShowOutput[i]).toInt()] - DotDigit);
}
digitalWrite(latchPin, HIGH);
};
#define Poti 14
int MaxTime = 20;
int MinTime = 1;
void setup() {
Serial.begin(115200);
Serial.println("");
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(13, OUTPUT);
pinMode(Poti, INPUT);
}
void loop(){
String buffer = "";
int PotiNumberInt = round(((MaxTime-MinTime)/4095.0)*analogRead(Poti) + MinTime);
if(PotiNumberInt < 10) buffer = "0" + String(PotiNumberInt);
else buffer = String(PotiNumberInt);
Register74HC595(15, 2, 4, buffer, 13, true, true);
delay(20);
}