//Arduino UNO XBee //LM: Autonomous communications demo int outpin [] = {8, 9, 10, 11, 12, 13}; byte rbyte; // Puzzle byte, chosen from - byte falseByte = 'n'; // false byte - proxy for boolean false byte trueByte = 'y'; // true byte byte x; // scratch // Player who sets first game. One XBee has GOFIRST = true, the other false - const boolean GOFIRST = true; // = false for second XBee boolean myTurn = GOFIRST; // Turn will alternate const int loopDelay = 10; // Configurable constants const int blinkDelay = 50; // const int betweenGuesses = 250; // const int betweenTurns = 500; // const int betweenGames = 1000; // const int a0 = 0; // Analog pin A0 - sense push-button const int a1 = 1; // Floating (not connected) void setup() { randomSeed(analogRead(a1)); for (int i = 0; i < 6; i++) { pinMode(outpin[i], OUTPUT); digitalWrite(outpin[i], LOW); } Serial.begin(9600); while (! Serial); // Wait until ready if (GOFIRST) { // Initialize com with other Arduino newPuzzle(); } } void loop() { if (Serial.available() > 0) { x = Serial.read(); // always read a byte if (myTurn) { // x is other XBee's guess replyToGuess(x); } // End 'my turn' section else { // x indicates quality of my guess if (x == trueByte) { // Correct guess newPuzzle(); } else { delay(betweenGuesses); submitGuess(); } } delay(loopDelay); // why? no reason } } // In demo prototype, number display is split by middle blue LED void display(byte b) { boolean d[] = {false, false, false, false}; if (b > 7) d[0] = true; if ((b > 3 && b < 8) || b > 11) d[1] = true; if (b % 4 == 2 || b % 4 == 3) d[2] = true; if (b %2 == 1) d[3] = true; // LEDs clearDisplay(); if (d[0]) digitalWrite(outpin[0], HIGH); if (d[1]) digitalWrite(outpin[1], HIGH); if (d[2]) digitalWrite(outpin[3], HIGH); if (d[3]) digitalWrite(outpin[4], HIGH); } void clearDisplay() { for (int i = 0; i < 5; i++) digitalWrite(outpin[i], LOW); } void newPuzzle() { delay(betweenGames); myTurn = true; rbyte = random(1, 15); // Random byte display(rbyte); // Display the puzzle byte Serial.write(rbyte); multBlink(outpin[2], blinkDelay, 2); // Double-flash blue LED // Demo interpretation is whimsical } void submitGuess() { byte b = random(1, 15); // Random byte (Stupid guess!) display(b); Serial.write(b); blink(outpin[2], blinkDelay); // One-flash blue LED } void replyToGuess(byte x) { // Feedback if (x == rbyte) { // correct guess myTurn = false; Serial.write(trueByte); clearDisplay(); } else { Serial.write(falseByte); } } void getFeedback() { // After submitting guess if (Serial.read() == trueByte) // Good guess (rare) newPuzzle(); // Otherwise no-op - Continue guessing } // Debug utilities void blink(int led, int delayTime) { digitalWrite(led, HIGH); delay(delayTime); digitalWrite(led, LOW); delay(delayTime); } void multBlink(int led, int delayTime, int n) { // Flash LED n times for (int i = 0; i < n; i++) { blink(led, delayTime); } } boolean buttonPress() { // A0 is pulled high except when button is pressed if (analogRead(a0) > 256) return false; delay(200); // Allow time for button release return true; }