/* Assignment 3 - Rock Paper Scissors by Steven Lehrburger and Armanda Lewis NYU ITP Sociable Objects 11 June 2008 This is the code for the scorekeeper. There is separate code for the two player boards */ // #include // for Software Serial/debugging // #define rxPin 3 // #define txPin 4 int playerOnePins[] = { 5, 6, 7, 8 }; // arrays of pin numbers that will be used to count the score int playerTwoPins[] = { 9, 10, 11, 12 }; int num_pins = 4; // the number of pins (i.e. the length of the array) - note this means the max score is 2^4 = 16 boolean playerOneUpdated = false; // booleans to indicate if the scores were both recently updated boolean playerTwoUpdated = false; // SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);// for debugging void displayPlayerOneScore(int number); // function prototypes void displayPlayerTwoScore(int number); int exponent(int base, int power); void setup() { Serial.begin(9600); // pinMode(rxPin, INPUT); // set up the software serial inputs and outputs // pinMode(txPin, OUTPUT); // mySerial.begin(9600); // mySerial.println("Starting Software Serial"); int i; for (i = 0; i < num_pins; i++) { // declare each scoring pin as output pinMode(playerOnePins[i], OUTPUT); pinMode(playerTwoPins[i], OUTPUT); } Serial.print("+++"); // put the radio in command mode: char thisByte = 0; while (thisByte != '\r') { // wait for the radio to respond with "OK\r" if (Serial.available() > 0) { thisByte = Serial.read(); } } Serial.print("ATMY0202\r"); Serial.print("ATID1492\r"); Serial.print("ATCN\r"); } void loop() { if (playerOneUpdated && playerTwoUpdated) { // if both have updated, flush the buffer and wait for them to update again - none of the old score values are important playerOneUpdated = false; playerTwoUpdated = false; Serial.flush(); } if (Serial.available() >= 4) { char firstByte = Serial.read(); // read the input (this only gets called when the buffer has enough characters if ((firstByte == '1') || (firstByte == '2')) { // the first byte should indicate the player number char secondByte = Serial.read(); if (secondByte == '\n') { // the second byte should be a linefeed char score = Serial.read(); // the third byte wil be the score char fourthByte = Serial.read(); // and the last byte will be a carriage return if (fourthByte == '\r') { // so if the last byte *was* a carriage return if (firstByte == '1') { // mySerial.print("player2 score = "); // mySerial.println(score); displayPlayerOneScore(score); // display the score for the appropriate player } else if (firstByte == '2') { // mySerial.print("player1 score = "); // mySerial.println(score); displayPlayerTwoScore(score); } } } } delay(500); // mySerial.println(" again "); } } void displayPlayerOneScore(int number) // illuminates the LEDs for the passed number { int remaining = number; for (int i = num_pins; i >= 0; i--) { // starting with the last pin (that has the largest value) and going down if ((remaining - exponent(2, i)) >= 0) { // if that pin is larger than the remaining value digitalWrite(playerOnePins[i], HIGH); // illuminate that pin remaining -= exponent(2, i); // and subtract the value from the number and keep going } else { digitalWrite(playerOnePins[i], LOW); // otherwise, the pin is off } } playerOneUpdated = true; } void displayPlayerTwoScore(int number) // same as above, but for the other player's pins { // (this could be decomposed into one function, but then you'd have to pass the pin array by reference... not worth it) int remaining = number; for (int i = num_pins; i >= 0; i--) { if ((remaining - exponent(2, i)) >= 0) { digitalWrite(playerTwoPins[i], HIGH); remaining -= exponent(2, i); } else { digitalWrite(playerTwoPins[i], LOW); } } playerTwoUpdated = true; } int exponent(int base, int power) // simply calculates an exponent - perhaps there is a library with this { // but it was faster to rewrite it int result = 1; for (int i = 0; i < power; i++) { result *= base; } return(result); }