/*
  74HC165 Shift register input example

  https://wokwi.com/arduino/projects/306031380875182657

  (C) 2021, Uri Shaked
*/

const int dataPin = 2;   /* Q7 */
const int clockPin = 3;  /* CP */
const int latchPin = 4;  /* PL */

const int numBits = 16;   /* Set to 8 * number of shift registers */

///Matrix
// Include the required Arduino libraries:
#include <LedControl.h>

#define CS_PIN  10
#define CLK_PIN 13
#define DIN_PIN 11
//#define RND_PIN A0
#define MAX_SEG 1


//#include "inputs.cpp"


LedControl mat = LedControl(DIN_PIN, CLK_PIN, CS_PIN, MAX_SEG); // MAX7219

///matrix vége


void setup() {
  Serial.begin(115200);
  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);



  mat.shutdown(0, false);
  // adjust led brightness
  mat.setIntensity(0, 7);
  // Clear
  mat.clearDisplay(0);
//test includes
//test();

}


String getBoard(){
 // Step 1: Sample
  digitalWrite(latchPin, LOW);
  digitalWrite(latchPin, HIGH);
  String board = "";

  // Step 2: Shift
  for (int i = 0; i < numBits; i++) {
    int bit = digitalRead(dataPin);
    if (bit == HIGH) {
      //Serial.print("1");
      board += "1";
    } else {
      //Serial.print("0");
      board += "0";
    }
    digitalWrite(clockPin, HIGH); // Shift out the next bit
    digitalWrite(clockPin, LOW);
  }
return(board);
}

void drawBoard(char board[]) {

uint32_t convertednr = strtoul(board, NULL, 2);

  Serial.println(convertednr);

mat.setRow(0, 0, convertednr);


}


void loop() {

String strBoard = getBoard();
char arrBoard[]="";

Serial.println("string: " + strBoard);
strBoard.toCharArray(arrBoard,numBits+1);

Serial.print("char:   ");
Serial.println(arrBoard);

drawBoard(arrBoard);

  //Serial.println(arrBoard);
  delay(1000);



///matrix


  //matrix vege


}
74HC165
74HC165