Sie sind auf Seite 1von 12

const int datapinY = 2; // Defining all variables

const int clockpinY = 3;


const int latchpinY = 4;

const int datapinB = 5;


const int clockpinB = 6;
const int latchpinB = 7;

const int sensorPin = 0;

const int testPin2 = 12;


const int testPin = 13;

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;

const int buttonY = 8;


const int buttonB = 9;

int buttonYState;
int buttonBState;
byte data = 0;

void setup()
{

buttonYState = digitalRead(buttonY); // buttonYState is equal to the value of buttonY


buttonBState = digitalRead(buttonB);

pinMode(datapinY, OUTPUT); // Setting each pin in shiftregistor to give it an output


pinMode(clockpinY, OUTPUT);
pinMode(latchpinY, OUTPUT);

pinMode(testPin, OUTPUT); // Setting each lone LED as an output


pinMode(testPin2, OUTPUT);

pinMode(datapinB, OUTPUT);
pinMode(clockpinB, OUTPUT);
pinMode(latchpinB, OUTPUT);

pinMode(buttonY, INPUT); // Setting the yellow button to an input device


pinMode(buttonB, INPUT);
}

void loop()
{

buttonYState = digitalRead(buttonY);
buttonBState = digitalRead(buttonB);

if (turn == 1) // If its yellows turn run this code


{
if (buttonYState == 0) // If button is pressed, run this code
{
shiftWriteY(indexY, HIGH); // Turns on selected yellow LED

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

else{ // Otherwise run this


ySelect(); // How to select the yellow LED
}
}

else if (buttonBState == 0) // Otherwise check if blue button is pressed


{
shiftWriteB(indexB, HIGH); // Turns on selected blue LED

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;
}

turn = 1; // Switchs turns to make it yellows turn


}

else // Otherwise run this


{
bSelect(); // How to select the blue LED

}
}

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, HIGH); // Makes data appear at the outputs

digitalWrite(latchpinY, LOW);

void shiftWriteB(int desiredPin, boolean desiredState) // Same as shiftWriteY, only for blue
player
{
bitWrite(data,desiredPin,desiredState);

shiftOut(datapinB, clockpinB, MSBFIRST, data);

digitalWrite(latchpinB, HIGH);

digitalWrite(latchpinB, LOW);

}
void ySelect(){ // Creating function to select yellows LED

buttonYState = digitalRead(buttonY);

int sensorValue; // Creating variable sensorValue

sensorValue = analogRead(sensorPin); // Defining sensorValue as the reading of sensorPin

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;

if (buttonYState == 0 && sensorValue >= 114 && sensorValue <= 226){

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;

if (buttonYState == 0 && sensorValue >= 340 && sensorValue <= 452){

yLight2 = 1;
}
}

else{
shiftWriteY(2, LOW);
}
if (sensorValue >= 453 && sensorValue <= 565 || yLight4 == 1){

shiftWriteY(4, HIGH);
indexY = 4;

if (buttonYState == 0 && sensorValue >= 453 && sensorValue <= 565){

yLight4 = 1;
}
}

else{
shiftWriteY(4, LOW);
}
if (sensorValue >= 566 && sensorValue <= 678 || yLight5 == 1){

shiftWriteY(5, HIGH);
indexY = 5;

if (buttonYState == 0 && sensorValue >= 566 && sensorValue <= 678){

yLight5 = 1;
}
}

else{
shiftWriteY(5, LOW);
}
if (sensorValue >= 679 && sensorValue <= 791 || yLight6 == 1){

shiftWriteY(6, HIGH);
indexY = 6;

if (buttonYState == 0 && sensorValue >= 679 && sensorValue <= 791){

yLight6 = 1;
}
}

else{
shiftWriteY(6, LOW);
}
if (sensorValue >= 792 && sensorValue <= 904 || yLight7 == 1){

shiftWriteY(7, HIGH);
indexY = 7;

if (buttonYState == 0 && sensorValue >= 792 && sensorValue <= 904){

yLight7 = 1;
}
}

else{
shiftWriteY(7, LOW);
}
if (sensorValue >= 905 && sensorValue <= 1023 || yLight8 == 1){

digitalWrite(testPin, HIGH);
indexY = 8;

if (buttonYState == 0 && sensorValue >= 905 && sensorValue <= 1023){

yLight8 =1;
}
}

else{
digitalWrite(testPin, LOW);
}
}
void bSelect(){ // Same as ySelect, only for blue player

buttonBState = digitalRead(buttonB);

int sensorValueB;

sensorValueB = analogRead(sensorPin);

if (sensorValueB <= 113 || bLight0 == 1){

shiftWriteB(0, HIGH);
indexB = 0;

if (buttonBState == 0 && sensorValueB <= 113)


{
bLight0 = 1;
}

else {
shiftWriteB(0, LOW);
}

if (sensorValueB >= 114 && sensorValueB <= 226 || bLight1 == 1){

shiftWriteB(1, HIGH);
indexB = 1;

if (buttonBState == 0 && sensorValueB >= 114 && sensorValueB <= 226){

bLight1 = 1;
}
}

else{
shiftWriteB(1, LOW);
}
if (sensorValueB >= 227 && sensorValueB <= 339 || bLight3 == 1){

shiftWriteB(3, HIGH);
indexB = 3;

if (buttonBState == 0 && sensorValueB >= 227 && sensorValueB <= 339){

bLight3 = 1;
}
}

else{
shiftWriteB(3, LOW);
}
if (sensorValueB >= 340 && sensorValueB <= 452 || bLight2 == 1){

shiftWriteB(2, HIGH);
indexB = 2;

if (buttonBState == 0 && sensorValueB >= 340 && sensorValueB <= 452){

bLight2 = 1;
}
}

else{
shiftWriteB(2, LOW);
}
if (sensorValueB >= 453 && sensorValueB <= 565 || bLight4 == 1){

shiftWriteB(4, HIGH);
indexB = 4;

if (buttonBState == 0 && sensorValueB >= 453 && sensorValueB <= 565){

bLight4 = 1;
}
}

else{
shiftWriteB(4, LOW);
}
if (sensorValueB >= 566 && sensorValueB <= 678 || bLight5 == 1){

shiftWriteB(5, HIGH);
indexB = 5;

if (buttonBState == 0 && sensorValueB >= 566 && sensorValueB <= 678){

bLight5 = 1;
}
}

else{
shiftWriteB(5, LOW);
}
if (sensorValueB >= 679 && sensorValueB <= 791 || bLight6 == 1){

shiftWriteB(6, HIGH);
indexB = 6;

if (buttonBState == 0 && sensorValueB >= 679 && sensorValueB <= 791){

bLight6 = 1;
}
}

else{
shiftWriteB(6, LOW);
}
if (sensorValueB >= 792 && sensorValueB <= 904 || bLight7 == 1){

shiftWriteB(7, HIGH);
indexB = 7;

if (buttonBState == 0 && sensorValueB >= 792 && sensorValueB <= 904){

bLight7 = 1;
}
}

else{
shiftWriteB(7, LOW);
}
if (sensorValueB >= 905 && sensorValueB <= 1023 || bLight8 == 1){
digitalWrite(testPin2, HIGH);
indexB = 8;

if (buttonBState == 0 && sensorValueB >= 905 && sensorValueB <= 1023){

bLight8 =1;
}
}

else{
digitalWrite(testPin2, LOW);
}
}

Das könnte Ihnen auch gefallen