RPS_Arduino Code

Here is Steven’s Arduino code for RPS 

#define txLED 3             // LED to indicate outgoing data

#define rxLED 4             // LED to indicate incoming data

#define rockRxLED 5

#define paperRxLED 6

#define scissorsRxLED 7

#define rockInPin 8

#define paperInPin 9

#define scissorsInPin 10

#define winlossLEDPin 13

#define restartPin 12
#define inputReceived 2

#define txrxDelay 100

 

int scoreCount = 0; // keep track of the score, nothing happens in case of tie

boolean opponentReady;      

// true if input has been received from the opponent, otherwise false

boolean selfReady;  // true if input has been received from the switches, //otherwise false

int opponentChoice;        // input received from opponent

int selfChoice;            // input received from switches                           

// we need the booleans AND the chars for this (as opposed to just checking to //make sure the chars are valid values             

// in place of the booleans) because we only want to store the first valid input

 

 

void setup() {

  // configure serial communications:

Serial.begin(9600); 

// configure output pins:

pinMode(txLED, OUTPUT);

pinMode(rxLED, OUTPUT);

pinMode(rockRxLED, OUTPUT);

pinMode(paperRxLED, OUTPUT);

pinMode(scissorsRxLED, OUTPUT);

pinMode(rockInPin, INPUT);

pinMode(paperInPin, INPUT);

pinMode(scissorsInPin, INPUT);

 pinMode(winlossLEDPin, OUTPUT);

pinMode(restartPin, INPUT);

pinMode(inputReceived, OUTPUT);

setDestination();                           // set XBee’s destination address:

 

blink(winlossLEDPin, 3);                                   // blink the winloss LED

newGame();                                  // initialize variables for a new game

}

 

void setDestination() {

 // put the radio in command mode:

Serial.print(”+++”);

// wait for the radio to respond with “OK\r”

char thisByte = 0;

while (thisByte != ‘\r’) {

 

       if (Serial.available() > 0) {

                  thisByte = Serial.read();

         }

     }// set the destination address, using 16-bit addressing.

 // if you’re using two radios, one radio’s destination

 // should be the other radio’s MY address, and vice versa:

 

Serial.print(”ATDH0, DL5678\r”);

// set my address using 16-bit addressing:

Serial.print(”ATMY1234\r”);

 

// set the PAN ID. If you’re working in a place where many people

 

// are using XBees, you should set your own PAN ID distinct

// from other projects.

Serial.print(”ATID1492\r”);// put the radio in data mode:

Serial.print(”ATCN\r”);

}

// Blink the specified LED:

 

 

 

void blink(int pinNum, int howManyTimes) {

 

         for (int i=0; i< howManyTimes; i++) {

                  digitalWrite(pinNum, HIGH);

                  delay(200);

                digitalWrite(pinNum, LOW);

                  delay(200);

            }

                   }

 

 

void loop() {

// zeroth, check for a restart

 

if (digitalRead(restartPin) == HIGH) {  

newGame();

} // first, check for input from the opponent

if ((!opponentReady) && (Serial.available() >= 2)) { 

// listen for incoming serial data

 

digitalWrite(rxLED, HIGH);

// turn on the rx LED whenever you’re reading data:

handleSerial();

digitalWrite(rxLED, LOW);

delay(200);

}

else {  digitalWrite(rxLED, LOW);

// turn off the rx LED when there’s no incoming data:}

// second, check for input from the switches

selfChoice = getChoice();

 

if (selfReady) { 

// if the user is ready, send the value - note this is only ever true when //selfChoice has a valid value

 

digitalWrite(txLED, HIGH); // light the tx LED to say you’re sending:

 

 Serial.print(selfChoice, DEC);                        // send the choice

 

  delay(20);

 

Serial.print(’\r’, DEC);    // send a carriage return to meet protocol   digitalWrite(txLED, LOW);   // turn off the tx LED

delay(txrxDelay);

}

// if input has been received from both, finish the game

if (opponentReady && selfReady) {

 

scoreCount += determineWinner();

while (digitalRead(restartPin) == LOW) {

 

 // wait until the new game button has been pressed

         }

 

    }

}

 

int getChoice() {

if (selfReady) {  // if we already are ready, we cannot change our choice

 

    return selfChoice;

  }

    selfReady = true;

 

  if (digitalRead(rockInPin) == HIGH) {     

// if the rock switch is pressed, return R
    return ‘0′;

       } else if (digitalRead(paperInPin) == HIGH) {    

// if the paper switch is pressed, return P
    return ‘1′;

 

 } else if (digitalRead(scissorsInPin) == HIGH) { 

 // if the scissors switch is pressed, return S
    return ‘2′; 

  }

 

  selfReady = false;

 return ‘4′;

}

 

 

void handleSerial() {

 

 int firstByte = Serial.read();                                        

  // read the input (this only gets called when the buffer is not empty

int secondByte = Serial.read();

 

  if (((firstByte == ‘0′) || (firstByte == ‘1′) || (firstByte == ‘2′)) && (secondByte == ‘\r’)) {       

// if it is a valid input byte character

   opponentChoice = firstByte;                                        

// store it and return

opponentReady = true;                                           

// the opponent is ready and we have his value  

digitalWrite(inputReceived, HIGH);

 }

 

    }

     int determineWinner() {                                     // returns 0 for tie or a loss, 1 for win (from perspective of self)       

 

 

 

         if (opponentChoice == ‘0′) {

// first, indicate what the opponent chose

 

digitalWrite(rockRxLED, HIGH);

          }

          else if (opponentChoice == ‘1′) {

               digitalWrite(paperRxLED, HIGH);

            }

          else if (opponentChoice == ‘2′) {

                    digitalWrite(scissorsRxLED, HIGH);                   

             }// then figure out who won:

              if (opponentChoice == selfChoice) {  // if there was a tie              

        digitalWrite(winlossLEDPin, HIGH);

return(0);

}

 if (((opponentChoice == ‘0′) && (selfChoice == ‘1′)) ||   // if you won

   ((opponentChoice == ‘1′) && (selfChoice == ‘2′)) ||

((opponentChoice == ‘2′) && (selfChoice == ‘0′))) {

 

         digitalWrite(winlossLEDPin, HIGH);

return(1;

} else {                                                 // otherwise, you lost

     digitalWrite(winlossLEDPin, LOW);return(0);

}

}// ALSO IMPLEMENT NEW GAME ON OTHER ARDUINO SO THAT //ONE DOESNT RESET WITHOUT THE OTHER!

 

void newGame() {

Serial.flush();

opponentReady = false;

selfReady = false;

opponentChoice = ‘4′;

selfChoice = ‘4′;

digitalWrite(winlossLEDPin, LOW);

digitalWrite(inputReceived, LOW);

blink(txLED, 1);

blink(rxLED, 1);

blink(rockRxLED, 1);

blink(paperRxLED, 1);

blink(scissorsRxLED, 1);

}

One Response to “RPS_Arduino Code”

  1. […] RPS_Arduino Code […]

Leave a Reply