« TOM-TOM TOMBINO PROTOTYPE | Main | F... YOU, FUKUSAWA! »

ROCK, PAPER, SCISSORS

IT HAS BEEN A LONG HARD ROAD...

RPS.jpg

Still trying to get the code to work... But i am considering changing route.

//-----------abs values for rock paper scissors
#define rock 2
#define paper 0
#define scissors 1

int inByte= -1; // incoming byte from serial RX

int switchPin1 = 12; // digital input pin for a switch
int switchPin2 = 11;
int switchPin3 = 10;

int yellowLed = 3; // digital output pin for an LED
int redLed = 2;
int greenLed = 9;

int switchState1 = 0; // the state of the switch
int switchState2 = 0; // the state of the switch
int switchState3 = 0; // the state of the switch

int Loc = 0;
int Rem = 0;

int winner = 0;


//________________________________________

void setup() {
//-----------configure serial communications:
Serial.begin (9600);
pinMode(switchPin1, INPUT); // set the switch pin to be an input
pinMode(switchPin2, INPUT); //
pinMode(switchPin3, INPUT); //
pinMode(yellowLed, OUTPUT); // set the yellow LED pin to be an output
pinMode(redLed, OUTPUT); //
pinMode(greenLed, OUTPUT); //

//-----------set XBee's destination address:
setDestination();
//-----------blink the TX LED indicating that the main program's
//about to start:
}

//________________________________________
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, DL1000\r");
// set my address using 16-bit addressing:
Serial.print("ATMY2000\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("ATID0911\r");
// put the radio in data mode:
Serial.print("ATCN\r");
}

//________________________________________
void loop() {

//if (Serial.available() > 0) {
readSwitch();
handleEval();
winnerLights();
}

void readSwitch() {

switchState1 = digitalRead(switchPin1);
switchState2 = digitalRead(switchPin2);
switchState3 = digitalRead(switchPin3);

if (switchState1 == 1) {
// if the switch is closed:
digitalWrite(greenLed, HIGH); // turn on the green LED
Serial.print(rock, DEC);
Serial.print("\r");

}
else {
// if the switch is open:
digitalWrite(greenLed, LOW); // turn off the green LED

}
if (switchState2 == 1) {
// if the switch is closed:
digitalWrite(redLed, HIGH); // turn on the red LED
Serial.print (paper, DEC);
Serial.print("\r");

}
else {
// if the switch is open:
digitalWrite(redLed, LOW); // turn off the red LED

}
if (switchState3 == 1) {
// if the switch is closed:
digitalWrite(yellowLed, HIGH); // turn on the yellow LED
Serial.print(scissors, DEC);
Serial.print("\r");

}
else {
// if the switch is open:
digitalWrite(yellowLed, LOW); // turn off the yellow LED

}

}

void handleEval () {

Rem = Serial.read();
//Serial.println(Rem, BYTE);

if (Loc == Rem) {
winner = 0;
}
else if (Loc == rock && Rem == paper) {
winner = 2;
}
else if (Loc == rock && Rem == scissors) {
winner = 1;
}
else if (Loc == paper && Rem == rock) {
winner = 1;
}
else if (Loc == paper && Rem == scissors) {
winner = 2;
}
else if (Loc == scissors && Rem == rock) {
winner = 2;
}
else if (Loc == scissors && Rem == paper) {
winner = 1;
}

}
void winnerLights () {

if (winner == 1) {

digitalWrite(yellowLed, HIGH);
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, HIGH);

delay (200);

digitalWrite(yellowLed, LOW);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
}
}

TrackBack

TrackBack URL for this entry:
http://itp.nyu.edu/~bp432/cgi-bin/mt/mt-tb.cgi/114