The Uncertain 7-Cube

Pages
Contributors: Nick Poole
Favorited Favorite 3

The Code

Since the Wake-on-Shake board handles all of the hibernation and motion detecting functions, all that the Arduino has to do is pick a phrase, send it to the voice synth, and then tell the Wake-on-Shake to turn everything off again. The code is based heavily on the Emic2 example code provided by Parallax.

language:C
#include <SoftwareSerial.h>
#include <TrueRandom.h>

#define rxPin 2    // Serial input (connects to Emic 2 SOUT)
#define txPin 3    // Serial output (connects to Emic 2 SIN)
#define ledPin 13  // Most Arduino boards have an on-board LED on this pin
#define wakePin 9 // Wake on Shake "Keep Awake" Pin

// set up a new serial port
SoftwareSerial emicSerial =  SoftwareSerial(rxPin, txPin);

void setup()  // Set up code called once on start-up
{
 // define pin modes
 pinMode(ledPin, OUTPUT);
 pinMode(rxPin, INPUT);
 pinMode(txPin, OUTPUT);
 pinMode(wakePin, OUTPUT);

 // set the data rate for the SoftwareSerial port
 emicSerial.begin(9600);

 /*
    When the Emic 2 powers on, it takes about 3 seconds for it to successfully
    intialize. It then sends a ":" character to indicate it's ready to accept
    commands. If the Emic 2 is already initialized, a CR will also cause it
    to send a ":"
 */

 emicSerial.print('\n');             // Send a CR in case the system is already up

 while (emicSerial.read() != ':');   // When the Emic 2 has initialized and is ready, it will 
 //send a single ':' character, so wait here until we receive it
 delay(10);                          // Short delay
 emicSerial.flush();                 // Flush the receive buffer
 digitalWrite(wakePin, HIGH); // Tell the Wake-on-Shake that we're still awake
}

void loop()
{
int freeWill = TrueRandom.random(16); // Choose a response. This is the magic part.
// int summonVoice = TrueRandom.random(9); // Summon a voice from Beyond
// summonVoice = char(summonVoice); // Convert the will of the universe 
//to a character so the emic module will accept it
// emicSerial.print('N'); // Select voice
// emicSerial.print(summonVoice); // Our voice from Beyond
// emicSerial.print('\n'); // Terminate the voice command
emicSerial.print('S'); // Speak some text command
switch (freeWill) {

 case 0:
  emicSerial.print("I mean, anything is possible. Right?");
  break;   
 case 1:
  emicSerial.print("I don't feel comfortable saying either way.");   
  break;
 case 2:
  emicSerial.print("Yes. . Oor No. . I won't speculate.");   
  break;   
 case 3:
  emicSerial.print("I say: go with what you know.");   
  break;
 case 4:
  emicSerial.print("How important is it to know that right now?");   
  break;   
 case 5:
  emicSerial.print("If it happens, it happens.");   
  break;
 case 6:
  emicSerial.print("Hoo could possibly know that?");   
  break;   
 case 7:
  emicSerial.print("I won't pretend to be an expert on the subject.");   
  break;
 case 8:
  emicSerial.print("It's not obviously a yes. It's not a particularly strong no either.");   
  break;   
 case 9:
  emicSerial.print("Bro... You need to live in the now. The future will be here soon enough.");   
  break;
 case 10:
  emicSerial.print("Ummmm. . . Sure? I mean, I don't know. but maybe?");   
  break;
 case 11:
  emicSerial.print("You're not giving me a lot to work with here.");   
  break;
 case 12:
  emicSerial.print("I'll need to form a subcommittee and get back to you.");   
  break;
 case 13:
  emicSerial.print("Market research in that segment shows a trend torward attitudes that strongly favor neither answer in particular.");   
  break;
 case 14:
  emicSerial.print("I suggest you shop the idea with a focus group.");   
  break;
 case 15:
  emicSerial.print("You should leverage web 2 point oh social leadership to crowdsource the answer.");   
  break;

default:
  emicSerial.print("The default case occurred. This shouldn't have happened.");
  }

 emicSerial.print('\n'); // Terminate the speech command
 while (emicSerial.read() != ':');   // Wait here until the Emic 2 responds with a ":" indicating it's done talking
 digitalWrite(wakePin, LOW); // Let the Wake-On-Shake module know it's okay to turn off
 while(1){}; // Hang out here until the WOS module shuts us down
}

So there it is, I simply used a random number generator library called “TrueRandom” to pick a number. Then I used that number to select a phrase from a list. That phrase was sent to the serial port, and then the sleep pin was allowed to fall to 0V, signalling the Wake-on-Shake to shut down. Pick up the cube and the whole process starts over again!