#include <Wire.h>
#include <SPI.h>
#include <Adafruit_NeoPixel.h>
// I2C Addresses
#define PCF8574_ADDRESS_4 0x20
#define PCF8574_ADDRESS_5 0x21
#define PCF8574_ADDRESS_6 0x22
#define PCF8574_ADDRESS_7 0x23
#define PCF8574_ADDRESS_8 0x24
#define PCF8574_ADDRESS_9 0x25
// SPI Constants
const int CS_10 = 10;
const int CS_9 = 9;
const byte PORT_A = 0x12;
const byte PORT_B = 0x13;
// NeoPixel Constants
const int TOTAL_LEDS = 64;
int myArray[64];
//Array to determine positions for the rows and columns calculated for Rook movement
int Rows[8][8] = {
{0, 1, 2, 3, 4, 5, 6, 7},
{8, 9, 10, 11, 12, 13, 14, 15},
{16, 17, 18, 19, 20, 21, 22, 23},
{24, 25, 26, 27, 28, 29, 30, 31},
{32, 33, 34, 35, 36, 37, 38, 39},
{40, 41, 42, 43, 44, 45, 46, 47},
{48, 49, 50, 51, 52, 53, 54, 55},
{56, 57, 58, 59, 60, 61, 62, 63}
};
// Starting positions for Rooks and Turn establishment
int B1W = 56;
int B1B = 36;
int turn=1;
// Initialize the single NeoPixel strip object with 64 LEDs
Adafruit_NeoPixel strip = Adafruit_NeoPixel(TOTAL_LEDS, 2, NEO_GRB + NEO_KHZ800);
void setup() {
// Initialize the I2C and SPI interfaces
Wire.begin();
SPI.begin();
pinMode(CS_10, OUTPUT);
pinMode(CS_9, OUTPUT);
// Initialize the NeoPixel strip
strip.begin();
strip.setBrightness(127);
strip.show(); // Initialize all pixels to 'off'
Serial.begin(9600); // in
}
void dataCollection() {
byte data1;
byte data2;
byte data3;
byte data4;
byte data5;
byte data6;
byte data7;
byte data8;
//Data Collection
data1 = readSPIRegister(CS_9, PORT_B); //ROW 1
data2 = readSPIRegister(CS_9, PORT_A); //ROW2
data3 = readSPIRegister(CS_10, PORT_B); //ROW 1
data4 = readSPIRegister(CS_10, PORT_A); //ROW2
Wire.requestFrom(PCF8574_ADDRESS_4, 1);//ROW5
data5 = Wire.read();
Wire.requestFrom(PCF8574_ADDRESS_5, 1);//ROW6
data6 = Wire.read();
Wire.requestFrom(PCF8574_ADDRESS_6, 1);//ROW7
data7 = Wire.read();
Wire.requestFrom(PCF8574_ADDRESS_7, 1);//ROW8
data8 = Wire.read();
//Data Collection
myArray[7] = (data1) & 1;
myArray[6] = (data1 >> 1) & 1;
myArray[5] = (data1 >> 2) & 1;
myArray[4] = (data1 >> 3) & 1;
myArray[3] = (data1 >> 4) & 1;
myArray[2] = (data1 >> 5) & 1;
myArray[1] = (data1 >> 6) & 1;
myArray[0] = (data1 >> 7) & 1;
myArray[15] = (data2) & 1;
myArray[14] = (data2 >> 1) & 1;
myArray[13] = (data2 >> 2) & 1;
myArray[12] = (data2 >> 3) & 1;
myArray[11] = (data2 >> 4) & 1;
myArray[10] = (data2 >> 5) & 1;
myArray[9] = (data2 >> 6) & 1;
myArray[8] = (data2 >> 7) & 1;
myArray[23] = (data3) & 1;
myArray[22] = (data3 >> 1) & 1;
myArray[21] = (data3 >> 2) & 1;
myArray[20] = (data3 >> 3) & 1;
myArray[19] = (data3 >> 4) & 1;
myArray[18] = (data3 >> 5) & 1;
myArray[17] = (data3 >> 6) & 1;
myArray[16] = (data3 >> 7) & 1;
myArray[31] = (data4) & 1;
myArray[30] = (data4 >> 1) & 1;
myArray[29] = (data4 >> 2) & 1;
myArray[28] = (data4 >> 3) & 1;
myArray[27] = (data4 >> 4) & 1;
myArray[26] = (data4 >> 5) & 1;
myArray[25] = (data4 >> 6) & 1;
myArray[24] = (data4 >> 7) & 1;
myArray[39] = (data5) & 1;
myArray[38] = (data5 >> 1) & 1;
myArray[37] = (data5 >> 2) & 1;
myArray[36] = (data5 >> 3) & 1;
myArray[35] = (data5 >> 4) & 1;
myArray[34] = (data5 >> 5) & 1;
myArray[33] = (data5 >> 6) & 1;
myArray[32] = (data5 >> 7) & 1;
myArray[47] = (data6) & 1;
myArray[46] = (data6 >> 1) & 1;
myArray[45] = (data6 >> 2) & 1;
myArray[44] = (data6 >> 3) & 1;
myArray[43] = (data6 >> 4) & 1;
myArray[42] = (data6 >> 5) & 1;
myArray[41] = (data6 >> 6) & 1;
myArray[40] = (data6 >> 7) & 1;
myArray[55] = (data7) & 1;
myArray[54] = (data7 >> 1) & 1;
myArray[53] = (data7 >> 2) & 1;
myArray[52] = (data7 >> 3) & 1;
myArray[51] = (data7 >> 4) & 1;
myArray[50] = (data7 >> 5) & 1;
myArray[49] = (data7 >> 6) & 1;
myArray[48] = (data7 >> 7) & 1;
myArray[63] = (data8) & 1;
myArray[62] = (data8 >> 1) & 1;
myArray[61] = (data8 >> 2) & 1;
myArray[60] = (data8 >> 3) & 1;
myArray[59] = (data8 >> 4) & 1;
myArray[58] = (data8 >> 5) & 1;
myArray[57] = (data8 >> 6) & 1;
myArray[56] = (data8 >> 7) & 1;
//Data Assigment
Serial.print("\nbegin\n");
for(int i=0;i<=7;i++){
Serial.print("\n");
Serial.print(myArray[7+(8*i)]);
Serial.print(myArray[6+(8*i)]);
Serial.print(myArray[5+(8*i)]);
Serial.print(myArray[4+(8*i)]);
Serial.print(myArray[3+(8*i)]);
Serial.print(myArray[2+(8*i)]);
Serial.print(myArray[1+(8*i)]);
Serial.print(myArray[0+(8*i)]);
Serial.print("\n");
Serial.print(i);
Serial.print("\n");
}
Serial.print("\nend\n");
}
//Rook Movement Code
int BishopFunct(int BP){
dataCollection(); // Collecting data, function not defined in the snippet
int Bishop = myArray[BP]; // Getting the value at index 'd' from 'myArray'
// Calculating indices or positions
int bishop_row = BP / 8; //Determines which Row Rook is in
int bishop_col = BP % 8; // Determines which Column Rook is in
int bishop_mov[32]; //An array which will hold all the positions in the same row and column which the Rook is in.
int w=0; //w,y, and z are used in an iteration which determines the index of RK in the rook_mov, since naturally it will contain the position which the rook was placed.
int a=0;
int b=0;
int c=0;
int d=0;
//These two next for loops then uses the rook's row and column positions found earlier to place all the individual positions for the positions which share the same row and column as RK.
for (int i=0;i<=7;i++){
bishop_mov[i] = Rows[bishop_row+i][bishop_col+i];
if (bishop_row+i > 7 || bishop_col+i > 7){
bishop_mov[i] = 65;
}
}
for (int i=0;i<=7;i++){
bishop_mov[i+8] = Rows[bishop_row+i][bishop_col-i];
if (bishop_row+i > 7){
bishop_mov[i+8] = 65;
}
if (bishop_col-i < 0){
bishop_mov[i+8] = 65;
}
}
for (int i=0;i<=7;i++){
bishop_mov[i+16] = Rows[bishop_row-i][bishop_col+i];
if (bishop_col+i > 7){
bishop_mov[i+16] = 65;
}
if (bishop_row-i < 0){
bishop_mov[i+16] = 65;
}
}
for (int i=0;i<=7;i++){
bishop_mov[i+24] = Rows[bishop_row-i][bishop_col-i];
if (bishop_row-i < 0 || bishop_col-i < 0){
bishop_mov[i+24] = 65;
}
}
int BishopLight[32] = {127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127}; //This array will be used to light up the NeoPixel LED's, 127 being the max brightnes.
//We then use the index found in the previous for loop to set the lighting value to 0 for the indexes in RookLight which matches the indexes of RK in rook_mov, thus turning off the light where the Rook was placed.
//For Daryl: You could take advantage of RookLight for the locking mechanism, basically indexing the position of already placed pieces in rook_mov, and then masking that same index in RookLight. Just a suggestion.
BishopLight[0] = 0;
BishopLight[8] = 0;
BishopLight[16] = 0;
BishopLight[24] = 0;
// While loop starts if Rook = 0, or if the piece is picked up.
while (Bishop== 0) {
dataCollection(); // Again, collecting data
delay(1000);
for (int i=0;i<=31;i++){
Serial.print(bishop_mov[i]);
Serial.print(",");
}
Serial.print("\n");
Serial.print(bishop_row);
Serial.print(bishop_col);
Serial.print("\n");
for (int i=0;i<=31;i++){
Serial.print(BishopLight[i]);
Serial.print(",");
}
//This for loop then lights each position found in rook_mov, with the corresponding brightness for green found in RookLight.
for (int i=0;i<=31;i++){
strip.setPixelColor(bishop_mov[i],strip.Color(0, BishopLight[i], 0));
strip.show();
}
//This for loop is the actual position tracking for loop, basically if a piece is placed in any of the columns or rows of RK (other then RK itself) it will shut off the lights and update the value of RK to this value where the piece was placed.
for (int i=0;i<=31;i++){
if (myArray[bishop_mov[i]]==1 && bishop_mov[i] != BP){
lightsoff();
BP = bishop_mov[i];
turn=turn+1; //Update turns
break;
}
}
Bishop = myArray[BP];
}
return BP;
}
void loop(){
lightsoff();
if (turn%2!=0){
B1W = BishopFunct(B1W);
}
if (turn%2==0){
B1B = BishopFunct(B1B);
}
}
void lightsoff(){
for(int i=0;i<=63;i++){
strip.setPixelColor(i,strip.Color(0, 0, 0));
strip.show();
}
}
byte readSPIRegister(int csPin, byte reg) {
byte data;
digitalWrite(csPin, LOW);
SPI.transfer(0x41); // Opcode for MCP23S17
SPI.transfer(reg);
data = SPI.transfer(0xFF);
digitalWrite(csPin, HIGH);
return data;
}