const int In1 = 7; const int In2 = 6; const int In3 = 5; const int In4 = 4; const int In5 = 3; const int In6 = 2; const int In7 = A2; const int In8 = A3; const int Ot1 = 8; const int Ot2 = 9; const int Ot3 = 10; const int Ot4 = 11; const int Ot5 = 12; const int Ot6 = 13; const int Ot7 = A1; const int Ot8 = A0; const int player1Pin = A4; const int player2Pin = A5; const int Inputs[]={In1,In2,In3,In4,In5,In6,In7,In8}; const int Outputs[]={Ot1,Ot2,Ot3,Ot4,Ot5,Ot6,Ot7,Ot8}; const unsigned long debounceTime = 10; //milliseconds unsigned long switchPressTime[8][8]; //when switches last changed state byte currentSwitchState[8][8]; byte oldSwitchState[8][8]={0}; byte actualState[8][8]; byte currentPlayer = 1; // player 1: currentPlayer = 1 player 2: currentPlayer = 0 void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(In1,OUTPUT); pinMode(In2,OUTPUT); pinMode(In3,OUTPUT); pinMode(In4,OUTPUT); pinMode(In5,OUTPUT); pinMode(In6,OUTPUT); pinMode(In7,OUTPUT); pinMode(In8,OUTPUT); pinMode(Ot1,INPUT); pinMode(Ot2,INPUT); pinMode(Ot3,INPUT); pinMode(Ot4,INPUT); pinMode(Ot5,INPUT); pinMode(Ot6,INPUT); pinMode(Ot7,INPUT); pinMode(Ot8,INPUT); pinMode(player1Pin,OUTPUT); // Pl1 and Pl2 are actually output pins, but the way the code is configurd on the LCD code requires a pinMode(player2Pin,OUTPUT); // pullup to high. The pins will be converted to outputs in the loop, brought low and then returned to an // input state. high-z. digitalWrite(player1Pin, HIGH); digitalWrite(player2Pin, HIGH); } void loop() { // put your main code here, to run repeatedly: for(int i = 0; i < 8; i++) { digitalWrite(Inputs[i],HIGH); for(int j = 0; j < 8; j++) { currentSwitchState[i][j]= digitalRead(Outputs[j]); if( currentSwitchState[i][j] != oldSwitchState[i][j] ) { if( millis() - switchPressTime[i][j] >= debounceTime) { switchPressTime[i][j] = millis(); oldSwitchState[i][j] = currentSwitchState[i][j]; if (oldSwitchState[i][j] == HIGH) { if( currentPlayer == 1) { digitalWrite(player2Pin,HIGH); digitalWrite(player1Pin, LOW); currentPlayer = 0; } else { digitalWrite(player1Pin,HIGH); digitalWrite(player2Pin,LOW); currentPlayer = 1; } } } } } digitalWrite(Inputs[i],LOW); } for(int i=0; i < 8; i++){ for(int j=0; j < 8; j++){ Serial.write(oldSwitchState[i][j]); } }; Serial.print("STOP"); }