Amanda Bernsohn: @ITP | Images | Statement | CV | Projects

 

Rock Paper Scissors

For this project we used two Arduinos, two XBees, two FTDI USB to Serial adapters for software serial, and 8 switches.

The idea was for each player to have a reset switch and one switch each for rock, paper and scissors. It took a lot longer for us to get this project running than we had anticipated, primarily because we didn't initially use software serial and couldn't decipher exactly how, or even if the XBees were communicating.







Code:
//Project: Paper Rock Scissor
//Stella Kim and Amanda Bernsohn
//Class: Sociable Obects

#define winLedPin 5 // pin that indicated winning
#define resetLedPin 6 // LED that indicates a reset
#define buttonReset 9 // Button to reset the game
#define buttonR 10 // Rock Button
#define buttonP 11 // Paper Button
#define buttonS 12 // Scissors Button
#define txLed 2 // LED to indicate outgoing data
#define rxLed 3 // LED to indicate incoming data

char inByte=0;
boolean outcome=false;
boolean reset = false;

void setup(){
// set pinModes for everything
pinMode(buttonR, INPUT);
pinMode(buttonP, INPUT);
pinMode(buttonS, INPUT);
pinMode(buttonReset, INPUT);
pinMode(winLedPin, OUTPUT);
pinMode(resetLedPin, OUTPUT);
pinMode(txLed, OUTPUT);
pinMode(rxLed, OUTPUT);
Serial.begin(9600);
setDestination();
blink(10);
}
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();
}
}
//code from Glow the LED lab
// 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, DL1234\r");
// set my address using 16-bit addressing:
Serial.print("ATMY5678\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("ATID1111\r");
// put the radio in data mode:
Serial.print("ATCN\r");
}
void loop(){
//check button state
if (buttonReset==HIGH){
reset=true;
}
else{
reset=false;
}
player();
}
void player(){
char myChoice, herChoice;
if(buttonR==HIGH){
myChoice='R';
}
if(buttonP==HIGH){
myChoice='P';
}
if(buttonS==HIGH){
myChoice='S';
}

//send answer to serial buffer for opponents choice
Serial.print(myChoice);
handleSerial();

//keep lisetning if opponenets value not yet received
while(inByte != 'R' && inByte !='P' && inByte != 'S')
handleSerial();

//store opponents value
herChoice=inByte;

if((herChoice=='P') && (myChoice=='S')){
outcome=true;
digitalWrite(winLedPin,HIGH);
}

if((herChoice=='P') && (myChoice=='R')){
outcome=false;
digitalWrite(winLedPin,LOW);
}
else if((herChoice=='R') && (myChoice=='S')){
outcome=false;
digitalWrite(winLedPin,LOW);
}
else if((herChoice=='R') && (myChoice=='P')){
outcome=true;
digitalWrite(winLedPin,HIGH);
}
else if((herChoice=='S') && (myChoice=='P')){
outcome=false;
digitalWrite(winLedPin,LOW);
}
else if((herChoice=='S') && (myChoice=='R')){
outcome=true;
digitalWrite(winLedPin,HIGH);
}

else if(herChoice==myChoice){
outcome=false;
blink(5);

//reset game
if(reset==true){
digitalWrite(resetLedPin,HIGH);
delay(20);
digitalWrite(resetLedPin,LOW);
delay(20);
digitalWrite(resetLedPin,HIGH);
delay(20);
digitalWrite(resetLedPin,LOW);
delay(20);
}
}
}

void handleSerial(){
if (Serial.available() > 0 ) {
// read a byte
inByte = Serial.read();
}
}

// Blink the tx LED:
void blink(int howManyTimes) {
for (int i=0; i< howManyTimes; i++) {
digitalWrite(winLedPin, HIGH);
delay(250);
digitalWrite(winLedPin, LOW);
delay(250);
}
}

posted by Amanda @ 3:53 AM,

0 Comments:

Post a Comment

<< Home