Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Use the Google Voice API to send commands to your RoboBrrd. The Arduino script requires the Streaming lib: http://arduiniana.org/libraries/streaming/. The Processing script requires the STT Library: http://stt.getflourish.com/.
/**
RoboBrrd Voice Commands
This script uses the corresponding Processing script to send voice commands to the RoboBrrd
*/
#include <Streaming.h>
const int pinStatus = 13; // Status Pin
const int pinR = 3; // R Pin
const int pinG = 5; // G Pin
const int pinB = 6; // B Pin
const int leds[] = {pinR, pinG, pinB};
const int pinSpkr = 7; // Speaker Pin
String inputString = ""; // Voice Commands
boolean stringComplete = false; // Voice Command Complete
void setup()
{
Serial.begin(9600); // Start the Serial Connection
Serial << "Hello from RoboBrrd! SQUAWK! CHIRP! Beginning initialization... beep beep" << endl;
inputString.reserve(200); // Store some memory for the Voice Commands
// Set the pins to output
pinMode(pinStatus, OUTPUT);
for(int i = 0; i < 3; i++)
{
pinMode(leds[i], OUTPUT);
}
pinMode(pinSpkr, OUTPUT);
playTone(260, 70); // Tweet to let us know it's ready
playTone(280, 70);
playTone(300, 70);
}
void loop()
{
if (stringComplete) // When Voice Command is received, do something with it
{
Serial << inputString << endl;
blinkLed(pinStatus, 2, 100); // Blink status led twice to let us know we've received the command
doAction(inputString); // Do the action
inputString = ""; // Clear out the action and wait for the next command
stringComplete = false;
}
}
void serialEvent() // Serial Event Handler
{
while (Serial.available())
{
char inChar = (char)Serial.read();
if (inChar == '\n') { // If newline character received, complete the command
stringComplete = true;
} else {
inputString += inChar;
}
}
}
void resetLeds() // Reset the RGB Leds to off
{
for(int i = 0; i < 3; i++)
{
analogWrite(leds[i], 0);
}
}
void doAction(String action)
{
// Actions for the RoboBrrd to take
// Add to this list if you want additional actions
if (action == "red" ||
action == "orange" ||
action == "yellow" ||
action == "green" ||
action == "blue" ||
action == "purple" ||
action == "pink") // Handle color requests
{
resetLeds();
// Isolate the specific actions so we can modify the LEDs specifically
if (action == "red")
{
analogWrite(leds[0], 255);
}
else if (action == "pink")
{
analogWrite(leds[0], 255);
analogWrite(leds[1], 20);
analogWrite(leds[2], 147);
}
else if (action == "orange")
{
analogWrite(leds[0], 255);
analogWrite(leds[1], 165);
}
else if (action == "yellow")
{
analogWrite(leds[0], 255);
analogWrite(leds[1], 255);
}
else if (action == "green")
{
analogWrite(leds[1], 255);
}
else if (action == "blue")
{
analogWrite(leds[2], 255);
}
else if (action == "purple")
{
analogWrite(leds[0], 128);
analogWrite(leds[2], 128);
}
}
else if (action == "tweet") // handle tweet
{
playTone(260, 70);
playTone(280, 70);
playTone(300, 70);
}
}
void blinkLed(int pin, int rep, int del)
{
for(int i = 0; i < rep; i++)
{
digitalWrite(pin, HIGH);
delay(del);
digitalWrite(pin, LOW);
delay(del);
}
}
void playTone(int tone, int duration)
{
for (long i = 0; i < duration * 1000L; i += tone * 2)
{
digitalWrite(pinSpkr, HIGH);
delayMicroseconds(tone);
digitalWrite(pinSpkr, LOW);
delayMicroseconds(tone);
}
}
/**
RoboBrrd Voice Commands
This script uses the STT Library.
you can download it from here: http://stt.getflourish.com/
*/
import processing.serial.*;
import com.getflourish.stt.*;
Serial serialPort;
STT stt;
String result;
int deviceId = 6; // Serial Device Id
String lang = "en"; // Set Language
void setup()
{
size(500, 250);
println(Serial.list()); // Print out all available Serial devices
String portName = Serial.list()[deviceId];
serialPort = new Serial(this, portName, 9600); // Connect to Serial Device
stt = new STT(this); // Start up the STT library
stt.enableDebug();
stt.setLanguage(lang);
textFont(createFont("Arial", 12));
result = "Say something...";
}
void draw()
{
background(0);
fill(100);
text("Press a key to record.\nRelease the key to stop recording.\nAvailable commands:\nRed,\nOrange,\nYellow,\nGreen,\nBlue,\nPurple,\nPink,\nTweet", 20, 20);
text("Response:", 250, 20);
fill(255);
text(result, 250, 40);
}
void transcribe(String utterance, float confidence)
{
println(utterance);
result = utterance;
// Limit voice commands to only those recognized.
// Add recognized commands to this list to send them to the RoboBrrd
if (result.equals("red") ||
result.equals("orange") ||
result.equals("yellow") ||
result.equals("green") ||
result.equals("blue") ||
result.equals("purple") ||
result.equals("pink") ||
result.equals("tweet"))
serialPort.write(result + '\n');
}
public void keyPressed ()
{
stt.begin(); // Press any key to start recording
}
public void keyReleased ()
{
stt.end(); // Release key to stop recording
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment