Professional Documents
Culture Documents
Tictactoe
Tictactoe
int yLight0;
int yLight1;
int yLight2;
int yLight3;
int yLight4;
int yLight5;
int yLight6;
int yLight7;
int yLight8;
int bLight0;
int bLight1;
int bLight2;
int bLight3;
int bLight4;
int bLight5;
int bLight6;
int bLight7;
int bLight8;
int indexY;
int indexB;
int turn = 1;
int buttonYState;
int buttonBState;
byte data = 0;
void setup()
{
pinMode(datapinB, OUTPUT);
pinMode(clockpinB, OUTPUT);
pinMode(latchpinB, OUTPUT);
void loop()
{
buttonYState = digitalRead(buttonY);
buttonBState = digitalRead(buttonB);
if (indexY == 0){ // If a certain LED is selected once the button is pressed, it stays on
yLight0 = 1;
}
if (indexY == 1){
yLight1 = 1;
}
if (indexY == 2){
yLight2 = 1;
}
if (indexY == 3){
yLight3 = 1;
}
if (indexY == 4){
yLight4 = 1;
}
if (indexY == 5){
yLight5 = 1;
}
if (indexY == 6){
yLight6 = 1;
}
if (indexY == 7){
yLight7 = 1;
}
if (indexY == 8){
yLight8 = 1;
}
turn = 0; // Switchs turns to make it blues turn
if (indexB == 0){
bLight0 = 1;
}
if (indexB == 1){
bLight1 = 1;
}
if (indexB == 2){
bLight2 = 1;
}
if (indexB == 3){
bLight3 = 1;
}
if (indexB == 4){
bLight4 = 1;
}
if (indexB == 5){
bLight5 = 1;
}
if (indexB == 6){
bLight6 = 1;
}
if (indexB == 7){
bLight7 = 1;
}
if (indexB == 8){
bLight8 = 1;
}
}
}
void shiftWriteY(int desiredPin, boolean desiredState) // Creates function that turns desired pin
into the desired state
{
bitWrite(data,desiredPin,desiredState); // Change global variable data, setting it to either 0 or
1
shiftOut(datapinY, clockpinY, MSBFIRST, data); // Sends data to yellows shift register
digitalWrite(latchpinY, LOW);
void shiftWriteB(int desiredPin, boolean desiredState) // Same as shiftWriteY, only for blue
player
{
bitWrite(data,desiredPin,desiredState);
digitalWrite(latchpinB, HIGH);
digitalWrite(latchpinB, LOW);
}
void ySelect(){ // Creating function to select yellows LED
buttonYState = digitalRead(buttonY);
if (sensorValue <= 113 || yLight0 == 1){ // If sensorValue is less than 113, or variable yLight0 is
1, run this
shiftWriteY(0, HIGH); // Turn LED 0 on
indexY = 0; // Change variable indexY to 0
if (buttonYState == 0 && sensorValue <= 113) // Checks if button is pressed and sensorValue
is less than 113
{
yLight0 = 1;// Change variable yLight0 to 1
}
}
else { // Otherwise
shiftWriteY(0, LOW); // Turn LED 0 off
if (sensorValue >= 114 && sensorValue <= 226 || yLight1 == 1){ // Same as above, only with
different parameters of sensorValue
shiftWriteY(1, HIGH);
indexY = 1;
yLight1 = 1;
}
}
else{
shiftWriteY(1, LOW);
}
if (sensorValue >= 227 && sensorValue <= 339 || yLight3 == 1){
shiftWriteY(3, HIGH);
indexY = 3;
if (buttonYState == 0){
yLight3 = 1;
}
}
else{
shiftWriteY(3, LOW);
}
if (sensorValue >= 340 && sensorValue <= 452 || yLight2 == 1){
shiftWriteY(2, HIGH);
indexY = 2;
yLight2 = 1;
}
}
else{
shiftWriteY(2, LOW);
}
if (sensorValue >= 453 && sensorValue <= 565 || yLight4 == 1){
shiftWriteY(4, HIGH);
indexY = 4;
yLight4 = 1;
}
}
else{
shiftWriteY(4, LOW);
}
if (sensorValue >= 566 && sensorValue <= 678 || yLight5 == 1){
shiftWriteY(5, HIGH);
indexY = 5;
yLight5 = 1;
}
}
else{
shiftWriteY(5, LOW);
}
if (sensorValue >= 679 && sensorValue <= 791 || yLight6 == 1){
shiftWriteY(6, HIGH);
indexY = 6;
yLight6 = 1;
}
}
else{
shiftWriteY(6, LOW);
}
if (sensorValue >= 792 && sensorValue <= 904 || yLight7 == 1){
shiftWriteY(7, HIGH);
indexY = 7;
yLight7 = 1;
}
}
else{
shiftWriteY(7, LOW);
}
if (sensorValue >= 905 && sensorValue <= 1023 || yLight8 == 1){
digitalWrite(testPin, HIGH);
indexY = 8;
yLight8 =1;
}
}
else{
digitalWrite(testPin, LOW);
}
}
void bSelect(){ // Same as ySelect, only for blue player
buttonBState = digitalRead(buttonB);
int sensorValueB;
sensorValueB = analogRead(sensorPin);
shiftWriteB(0, HIGH);
indexB = 0;
else {
shiftWriteB(0, LOW);
}
shiftWriteB(1, HIGH);
indexB = 1;
bLight1 = 1;
}
}
else{
shiftWriteB(1, LOW);
}
if (sensorValueB >= 227 && sensorValueB <= 339 || bLight3 == 1){
shiftWriteB(3, HIGH);
indexB = 3;
bLight3 = 1;
}
}
else{
shiftWriteB(3, LOW);
}
if (sensorValueB >= 340 && sensorValueB <= 452 || bLight2 == 1){
shiftWriteB(2, HIGH);
indexB = 2;
bLight2 = 1;
}
}
else{
shiftWriteB(2, LOW);
}
if (sensorValueB >= 453 && sensorValueB <= 565 || bLight4 == 1){
shiftWriteB(4, HIGH);
indexB = 4;
bLight4 = 1;
}
}
else{
shiftWriteB(4, LOW);
}
if (sensorValueB >= 566 && sensorValueB <= 678 || bLight5 == 1){
shiftWriteB(5, HIGH);
indexB = 5;
bLight5 = 1;
}
}
else{
shiftWriteB(5, LOW);
}
if (sensorValueB >= 679 && sensorValueB <= 791 || bLight6 == 1){
shiftWriteB(6, HIGH);
indexB = 6;
bLight6 = 1;
}
}
else{
shiftWriteB(6, LOW);
}
if (sensorValueB >= 792 && sensorValueB <= 904 || bLight7 == 1){
shiftWriteB(7, HIGH);
indexB = 7;
bLight7 = 1;
}
}
else{
shiftWriteB(7, LOW);
}
if (sensorValueB >= 905 && sensorValueB <= 1023 || bLight8 == 1){
digitalWrite(testPin2, HIGH);
indexB = 8;
bLight8 =1;
}
}
else{
digitalWrite(testPin2, LOW);
}
}