2008년 7월 2일 수요일

Rock, Paper, Scissors GAME


circuit with XBee


Eduardo Lytton and I made Rock, Scissor, and Paper game. The game we made will be triggered by start switch. Once, push the start switch, each side generates a random number for rock, scissor, and paper. It will be sent to the other side as a data and the data will be compared with their own data. Depending on the real rock scissor and paper game, the one who wins the game get the light with 'you win' LED.






'ZTerm' and Arduino code represent procedure when game has started.

-code-

/*
Rock Paper Scissors Game using XBee, Arduino, and usb to serial adaptor
*/

#include

#define txLed 2
#define rxLed 3
#define rocPin_0 6
#define sciPin_1 7
#define papPin_2 8
#define youWin 4
#define switchPin 5
#define blinkPin 13
#define resetPin 9
#define txPin 12
#define rxPin 11

int myNumber = 0;

int inByte = -1;
int inValue[2];
int inValuePos;
int inMyValue;
byte getPartNum;

int stringPos = 0;

int switchState = 0;
int resetSwitchState = 0;

boolean gameStart = false;
boolean firstDataGet = false;
boolean dataIn = false;

SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);


//___________________________________________________________________setup
void setup(){
Serial.begin(9600);
pinMode(txLed, OUTPUT);
pinMode(rxLed, OUTPUT);
pinMode(rocPin_0, OUTPUT);
pinMode(sciPin_1, OUTPUT);
pinMode(papPin_2, OUTPUT);
pinMode(switchPin, INPUT);
pinMode(resetPin, INPUT);
pinMode(youWin, OUTPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);


mySerial.begin(9600);
mySerial.println("start...");
setDestination();
blink(3);
Serial.flush(); // clear the serial buffer so it's empty
}// setup



void setDestination(){ //set PanID, myID, destination ID
Serial.print("+++");
char thisByte = 0;
while(thisByte != '\r'){
if( Serial.available() > 0){
thisByte = Serial.read();
}
}
Serial.print("ATRE\r");
Serial.print("ATDH0, DL1234\r");
Serial.print("ATMY5678\r");
//Serial.print("ATID1111\r");
Serial.print("ATIDE00E\r");
Serial.print("ATCN\r");
}

void blink(int howManyTimes){ // function to make sure that Arduino is functioning properly
for(int i = 0; i 0){
dataIn = true;
// Serial.print("######");
mySerial.println("true");
digitalWrite(rxLed, HIGH); // light LED to indicate reciving information on Xbee
handleSerial(); // function for what to do with this information received
compareWhoWin(getPartNum, myNumber);
}
else{
digitalWrite(rxLed, LOW); // if no information received, LED is off
}



//send the ranNumvalue to the partner
if(myNumber > 0){ // if have a number generated from random
digitalWrite(txLed, HIGH); // light the LED on Xbee to indicate sending information

mySerial.println(myNumber);
Serial.print(myNumber, DEC); // send this value serial as DEC
Serial.print("\r"); // print a carriage return in ASCII
digitalWrite(txLed, LOW); // turn off LED on Xbee to indicate not sending information
}

}//loop
//----------------------------------------------------------------------------------------------------loop


//-------------------------------------------------------------------function for handleing serial
//function for serial communication between the xbees
void handleSerial(){

inByte = Serial.read(); // a variable for each byte taken from serial read function
mySerial.print("inByte: ");
mySerial.println(inByte, BYTE);

if(inByte != '\r'){ // the first byte is not carrige return, put the first byte into inMyValue
inMyValue = inByte;
}
if(inByte == '\r'){ // if you get a carriage return
// int getPartNum = atoi(inString); // change the ASCII value in the string to integer to make getPartNum - partner's number
getPartNum = inMyValue -48 ;
// char get"Part = inByte;
mySerial.print("Partner Number: ");
mySerial.println(getPartNum);

inMyValue = 0; // make inMyValue zero
}
}

//------------------------------------------------------------------ function for getting random Number
int getRandomNum(){ // function to get random number
randomSeed(millis()*analogRead(0)); // for real random combine millis and analogRead
int ranNumber;
ranNumber = random(1,4); // get a random value from 1 to 3


return ranNumber;
}//getRandomNum


//------------------------------------------------------------------ function for comparing who win
void compareWhoWin(int partnerNum, int myNumberAgain){ // function to compare partner and your numbers to see who wins: rock, paper, or scissors
mySerial.print("myData : ");
mySerial.println(myNumberAgain);
mySerial.print("partnerData: ");
mySerial.println(partnerNum);

if(partnerNum == myNumberAgain){ // if a tie ( numbers the same)
digitalWrite(youWin, HIGH); // light both youWin LEDS
mySerial.println("&&&&&&&&&&&&&&&&&&");
mySerial.println(" TIE ");
delay(50);
} if(partnerNum == 1 && myNumberAgain == 2 ){ // if pN rock vs mN scissors
digitalWrite(youWin,LOW); // mN youWin LED is not lit (LOSE)
mySerial.println("&&&&&&&");
mySerial.println(" You Lose");
delay(50);
} if(partnerNum ==1 && myNumberAgain == 3 ){ // if pN rock vs mN paper
digitalWrite(youWin,HIGH); // mN youWin LED is lit (WIN)
mySerial.println("&&&&&&&&&");
mySerial.println(" you Win");
delay(50);
} if(partnerNum == 2 && myNumberAgain == 1 ){ // if pN scissors vs mN rock
digitalWrite(youWin,HIGH); // mN youWin LED is lit (WIN)
mySerial.println("&&&&&&&&&");
mySerial.println(" you Win");
delay(50);
} if(partnerNum == 2 && myNumberAgain == 3 ){ // if pN scissors vs mN paper
digitalWrite(youWin,LOW); // mN youWin LED is not lit (LOSE)
mySerial.println("&&&&&&&");
mySerial.println(" You Lose");
delay(50);
} if(partnerNum == 3 && myNumberAgain == 1 ){ // if pN paper vs mN rock
digitalWrite(youWin,LOW); // mN youWin LED is not lit (LOSE)
mySerial.println("&&&&&&&");
mySerial.println(" You Lose");
delay(50);
} if(partnerNum == 3 && myNumberAgain == 2 ){ // if pN paper vs mN scissors
digitalWrite(youWin,HIGH); // mN youWin LED is lit (WIN)
mySerial.println("&&&&&&&&&");
mySerial.println(" you Win");
delay(50);
}

}//compareWhoWin



//-------------------------------------------------------------------------------function for reset a game
void resetGame() { // function to reset LEDs after the counter counts time

gameStart = false;
firstDataGet = false;
dataIn = false;

digitalWrite(youWin,LOW);
digitalWrite(rocPin_0,LOW);
digitalWrite(sciPin_1,LOW);
digitalWrite(papPin_2,LOW);
digitalWrite(switchPin, LOW);

myNumber = 0;
resetSwitchState = 0;
Serial.flush();
}


댓글 없음: