//**************************************************************//
//  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);
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
74HC595
sr1:Q1
sr1:Q2
sr1:Q3
sr1:Q4
sr1:Q5
sr1:Q6
sr1:Q7
sr1:GND
sr1:Q7S
sr1:MR
sr1:SHCP
sr1:STCP
sr1:OE
sr1:DS
sr1:Q0
sr1:VCC
sevseg1:COM.1
sevseg1:COM.2
sevseg1:A
sevseg1:B
sevseg1:C
sevseg1:D
sevseg1:E
sevseg1:F
sevseg1:G
sevseg1:DP
pot1:GND
pot1:SIG
pot1:VCC
74HC595
sr2:Q1
sr2:Q2
sr2:Q3
sr2:Q4
sr2:Q5
sr2:Q6
sr2:Q7
sr2:GND
sr2:Q7S
sr2:MR
sr2:SHCP
sr2:STCP
sr2:OE
sr2:DS
sr2:Q0
sr2:VCC
sevseg2:COM.1
sevseg2:COM.2
sevseg2:A
sevseg2:B
sevseg2:C
sevseg2:D
sevseg2:E
sevseg2:F
sevseg2:G
sevseg2:DP