/*
Arduino | coding-help
4 digit 7 segment display problem
Yarrots — 6/30/24 at 9:17 AM

Hello everyone,
I'm trying to display each individual digit with number.
1 digit => 1, 2 digit => 2 etc.

I have two problem doing it : 
1) I have no idea why, but for some reason 1 go on the second digit.
2) On the initialization, the 1 digit displayed does rdm segment.
*/

byte Chiffre[6][8]{
  //1  2  3  4  5  6  7  8

  { 1, 1, 0, 1, 1, 1, 0, 1 },  //    0

  { 0, 0, 1, 1, 0, 0, 0, 1 },  //    1
  
  { 0, 0, 0, 1, 0, 1, 0, 1 },  //    3
  { 1, 0, 0, 1, 1, 1, 0, 0 }  //    4


};

int latch = A4;  //74HC595  pin 12  RCLK   Controls the internal transference of data in SN74HC595 internal registers
int clock = A5;  //74HC595  pin 11 SRCLK Generates the clock signal to control the transference of data
int data = A3;   //74HC595  pin 14 SER Outputs the byte to transfer

const int PinDigit[4] = {3, 4, 5, 6};  // Pin relié a l'afficheur 4 digit display 

int mode = 1; // Version du 4 digit 7 segment. 1 pour anode (Relié au 5V) -- 0 pour cathode (Relié au 0V)           
int data_envoye = 0;



void updateShiftRegister(int Pin) {
  digitalWrite(latch, LOW);
  digitalWrite(PinDigit[Pin], mode);
  shiftOut(data, clock, LSBFIRST, data_envoye);
  delay(500);
  digitalWrite(PinDigit[Pin], !mode);
  digitalWrite(latch, HIGH);
}

void setup() {
  pinMode(latch, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(data, OUTPUT);

  pinMode(PinDigit[0], OUTPUT);
  pinMode(PinDigit[1], OUTPUT);
  pinMode(PinDigit[2], OUTPUT);
  pinMode(PinDigit[3], OUTPUT);

}

void loop() {
  for (int Loop = 0; Loop <= 3; Loop++) {

    for (int Bwrite = 0; Bwrite <= 7; Bwrite++) {

      if (Chiffre[Loop][Bwrite] == 0)  //char (10)
      {
        bitClear(data_envoye, Bwrite);  // regarde si on doit envoyer un 1 ou un 0 en fonction de la place sur char
      } else bitSet(data_envoye, Bwrite);
    }
    switch (Loop) {
      case 0:
        updateShiftRegister(Loop);
        break;
      case 1:
        updateShiftRegister(Loop);
        break;
      case 2:
        updateShiftRegister(Loop);
        break;
      case 3:
        updateShiftRegister(Loop);
        break;
      default:
        break;
    }
  }
}
$abcdeabcde151015202530354045505560fghijfghij
74HC595