#define BOARD_SIZE 8
// Define the switch matrices
bool switchMatrix[BOARD_SIZE][BOARD_SIZE];
bool previousSwitchMatrix[BOARD_SIZE][BOARD_SIZE];
bool piecePositionMatrix[BOARD_SIZE][BOARD_SIZE];
bool startingPositionMatrix[BOARD_SIZE][BOARD_SIZE] = {
// Define the starting position matrix as needed
};
bool initializedFromSwitches = false;
// Function to shift in a byte from a shift register
byte shiftIn(int dataPin, int clockPin, int latchPin) {
byte value = 0;
// Ensure latch pin is low before reading
digitalWrite(latchPin, LOW);
delayMicroseconds(2); // Allow data to stabilize
digitalWrite(latchPin, HIGH);
// Shift in the bits
for (int bit = 7; bit >= 0; bit--) {
digitalWrite(clockPin, LOW);
delayMicroseconds(2); // Allow data to stabilize
value |= (digitalRead(dataPin) << bit);
digitalWrite(clockPin, HIGH);
}
// Reset clock pin
digitalWrite(clockPin, LOW);
return value;
}
void updateSwitchMatrix() {
// Read data from each shift register and store it in switchStates array
byte switchStates[8]; // Eight bytes to read the eight registers
switchStates[0] = shiftIn(11, 12, 13); // DATA_PIN_1, CLOCK_PIN_1, LATCH_PIN_1
switchStates[1] = shiftIn(8, 9, 10); // DATA_PIN_2, CLOCK_PIN_2, LATCH_PIN_2
switchStates[2] = shiftIn(5, 6, 7); // DATA_PIN_3, CLOCK_PIN_3, LATCH_PIN_3
switchStates[3] = shiftIn(2, 3, 4); // DATA_PIN_4, CLOCK_PIN_4, LATCH_PIN_4
switchStates[4] = shiftIn(24, 23, 22); // DATA_PIN_5, CLOCK_PIN_5, LATCH_PIN_5
switchStates[5] = shiftIn(27, 26, 25); // DATA_PIN_6, CLOCK_PIN_6, LATCH_PIN_6
switchStates[6] = shiftIn(30, 29, 28); // DATA_PIN_7, CLOCK_PIN_7, LATCH_PIN_7
switchStates[7] = shiftIn(33, 32, 31); // DATA_PIN_8, CLOCK_PIN_8, LATCH_PIN_8
// Update the previous switch matrix to match the current one
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
previousSwitchMatrix[i][j] = switchMatrix[i][j];
}
}
// Update switch matrix
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (i == 3 && j == 7) // Row 4, column 8
switchMatrix[i][j] = digitalRead(39); // Controlled by pin 39
else if (i == 4 && j == 7) // Row 5, column 8
switchMatrix[i][j] = digitalRead(38); // Controlled by pin 38
else if (i == 5 && j == 7) // Row 6, column 8
switchMatrix[i][j] = digitalRead(37); // Controlled by pin 37
else if (i == 6 && j == 7) // Row 7, column 8
switchMatrix[i][j] = digitalRead(35); // Controlled by pin 35
else if (i == 7 && j == 7) // Row 8, column 8
switchMatrix[i][j] = digitalRead(34); // Controlled by pin 34
else if (i == 7 && j == 0) // Row 8, column 1
switchMatrix[i][j] = digitalRead(36); // Controlled by pin 36
else if (i == 4 && j == 2) // Row 5, column 3 (5, 3 in 1-based)
switchMatrix[i][j] = digitalRead(42); // Controlled by pin 42
else if (i == 1 && j == 6) // Row 2, column 7 (2, 7 in 1-based)
switchMatrix[i][j] = digitalRead(44); // Controlled by pin 44
else if (i == 1 && j == 2) // Row 2, column 3 (2, 3 in 1-based)
switchMatrix[i][j] = digitalRead(43); // Controlled by pin 43
else if (i == 1 && j == 7) // Row 2, column 8
switchMatrix[i][j] = digitalRead(41); // Controlled by pin 41
else if (i == 2 && j == 7) // Row 3, column 8
switchMatrix[i][j] = digitalRead(40); // Controlled by pin 40
else if (i == 1 && j == 3) // Row 2, column 4
switchMatrix[i][j] = digitalRead(45); // Controlled by pin 45
else if (i == 2 && j == 4) // Row 3, column 5
switchMatrix[i][j] = digitalRead(46); // Controlled by pin 46
else if (i == 3 && j == 0) // Row 4, column 1
switchMatrix[i][j] = digitalRead(47); // Controlled by pin 47
else if (i == 3 && j == 4) // Row 4, column 5
switchMatrix[i][j] = digitalRead(48); // Controlled by pin 48
else if (i == 6 && j == 4) // Row 7, column 5
switchMatrix[i][j] = digitalRead(50); // Controlled by pin 50
else if (i == 7) // Row 8
switchMatrix[i][j] = !(switchStates[0] & (1 << j)); // First 8 bits from the first register control row 8
else if (i == 6) // Row 7
switchMatrix[i][j] = !(switchStates[1] & (1 << j)); // First 8 bits from the second register control row 7
else if (i == 5) // Row 6
switchMatrix[i][j] = !(switchStates[2] & (1 << j)); // First 8 bits from the third register control row 6
else if (i == 4) // Row 5
switchMatrix[i][j] = !(switchStates[3] & (1 << j)); // First 8 bits from the fourth register control row 5
else if (i == 3) // Row 4
switchMatrix[i][j] = !(switchStates[4] & (1 << j)); // First 8 bits from the fifth register control row 4
else if (i == 2) // Row 3
switchMatrix[i][j] = !(switchStates[5] & (1 << j)); // First 7 bits from the sixth register control row 3
else if (i == 1) // Row 2
switchMatrix[i][j] = !(switchStates[6] & (1 << j)); // First 7 bits from the seventh register control row 2
else if (i == 0) // Row 1
switchMatrix[i][j] = 0; // First 7 bits from the eighth register control row 1
}
}
// Check if the first two and last two rows are filled with 1s
if (!initializedFromSwitches) {
initializedFromSwitches = true;
// Set the position matrix to the starting position matrix
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
piecePositionMatrix[i][j] = startingPositionMatrix[i][j];
}
}
}
// Print the switch matrix to the Serial Monitor
Serial.println("Switch Matrix:");
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
Serial.print(switchMatrix[i][j]);
Serial.print(" ");
}
Serial.println();
}
Serial.println();
}
bool hasSwitchMatrixChanged() {
bool changed = false;
// Check if there are any changes in the switch matrix
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (switchMatrix[i][j] != previousSwitchMatrix[i][j]) {
// Changes detected, print the location
Serial.print("Switch matrix changed at row ");
Serial.print(i + 1); // Convert to 1-based indexing
Serial.print(", column ");
Serial.println(j + 1); // Convert to 1-based indexing
changed = true;
}
}
}
return changed;
}
void setup() {
// Set up pins and serial communication
Serial.begin(9600);
pinMode(11, INPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT);
pinMode(8, INPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT);
pinMode(5, INPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT);
pinMode(2, INPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT);
pinMode(24, INPUT); pinMode(23, OUTPUT); pinMode(22, OUTPUT);
pinMode(27, INPUT); pinMode(26, OUTPUT); pinMode(25, OUTPUT);
pinMode(30, INPUT); pinMode(29, OUTPUT); pinMode(28, OUTPUT);
pinMode(33, INPUT); pinMode(32, OUTPUT); pinMode(31, OUTPUT);
pinMode(34, INPUT_PULLUP); pinMode(35, INPUT_PULLUP); pinMode(36, INPUT_PULLUP); pinMode(37, INPUT_PULLUP); pinMode(38, INPUT_PULLUP); pinMode(39, INPUT_PULLUP); // Set pins as input with internal pull-up
pinMode(40, INPUT_PULLUP); // Set pin 40 as input with internal pull-up
pinMode(41, INPUT_PULLUP); // Set pin 41 as input with internal pull-up
pinMode(42, INPUT_PULLUP); // Set pin 42 as input with internal pull-up
pinMode(43, INPUT_PULLUP); // Set pin 43 as input with internal pull-up
pinMode(44, INPUT_PULLUP); // Set pin 44 as input with internal pull-up
pinMode(45, INPUT_PULLUP); // Set pin 45 as input with internal pull-up
pinMode(46, INPUT_PULLUP); // Set pin 46 as input with internal pull-up
pinMode(47, INPUT_PULLUP); // Set pin 47 as input with internal pull-up
pinMode(48, INPUT_PULLUP); // Set pin 48 as input with internal pull-up
pinMode(50, INPUT_PULLUP); // Set pin 50 as input with internal pull-up
}
void loop() {
updateSwitchMatrix();
delay(1000); // Wait for a second before updating again
if (hasSwitchMatrixChanged()) {
Serial.println("Switch matrix has changed!");
// Perform actions based on the changes
} else {
Serial.println("Switch matrix has not changed.");
}
delay(1000); // Wait for a second before checking again
}