Object That Tweets: Ticklish Object
This is a rock that tweets its laughter when it is tickled or poked. Tickling it harder will make it laugh more.
This is more of a fun little tech demo than anything.
Apologies for the ugly code…
Processing code:
import processing.serial.*;
import com.temboo.core.*;
import com.temboo.Library.Twitter.Tweets.*;
Serial myPort;
int valueA; // Sensor Value A
int valueB; // Sensor Value B
int oldValueB;
int finalPressure = 0;
boolean foundPressure = false;
int resetTimer = 0;
//------------------------------------
void setup() {
size(300, 200);
// List my available serial ports
int nPorts = Serial.list().length;
for (int i=0; i < nPorts; i++) {
println("Port " + i + ": " + Serial.list()[i]);
}
String portName = Serial.list()[9];
myPort = new Serial(this, portName, 9600);
serialChars = new ArrayList();
}
float round(float number, float decimal) {
return (float)(round((number*pow(10, decimal))))/pow(10, decimal);
}
//------------------------------------
void draw() {
// Process the serial data. This acquires freshest values.
processSerial();
background (150);
if(!foundPressure && valueB < oldValueB) {
finalPressure = oldValueB;
foundPressure = true;
runStatusesUpdateChoreo();
}
if(foundPressure) {
resetTimer++;
if(resetTimer > 1000) {
resetTimer = 0;
foundPressure = false;
finalPressure = 0;
oldValueB = 0;
valueB = 0;
}
}
text ("old pressure: "+oldValueB, 80, 35);
text ("pressure: "+valueB, 80, 55);
text ("final pressure: "+finalPressure, 80, 75);
}
ArrayList serialChars; // Temporary storage for received serial data
int whichValueToAccum = 0; // Which piece of data am I currently collecting?
boolean bJustBuilt = false; // Did I just finish collecting a datum?
void processSerial() {
while (myPort.available () > 0) {
char aChar = (char) myPort.read();
// You'll need to add a block like one of these
// if you want to add a 3rd sensor:
if (aChar == 'A') {
bJustBuilt = false;
whichValueToAccum = 0;
} else if (aChar == 'B') {
bJustBuilt = false;
whichValueToAccum = 1;
} else if (((aChar == 13) || (aChar == 10)) && (!bJustBuilt)) {
// If we just received a return or newline character, build the number:
int accum = 0;
int nChars = serialChars.size();
for (int i=0; i < nChars; i++) {
int n = (nChars - i) - 1;
int aDigit = ((Integer)(serialChars.get(i))).intValue();
accum += aDigit * (int)(pow(10, n));
}
// Set the global variable to the number we captured.
// You'll need to add another block like one of these
// if you want to add a 3rd sensor:
if (whichValueToAccum == 0) {
valueA = accum;
// println ("A = " + valueA);
} else if (whichValueToAccum == 1) {
oldValueB = valueB;
valueB = accum;
// println ("B = " + valueB);
}
// Now clear the accumulator
serialChars.clear();
bJustBuilt = true;
} else if ((aChar >= 48) && (aChar < = 57)) {
// If the char is between '0' and '9', save it.
int aDigit = (int)(aChar - '0');
serialChars.add(aDigit);
}
}
}
String getLaughText(int p) {
String result = "";
p = p/75;
for(int i = 0; i < p; i++) {
result += "ha";
}
result += "!";
return result;
}
// Create a session using your Temboo account application details
TembooSession session = new TembooSession("ticklishobject", "myFirstApp", "d6a74398d87c4963b7ce4951f8d291c9");
void runStatusesUpdateChoreo() {
// Create the Choreo object using your Temboo session
StatusesUpdate statusesUpdateChoreo = new StatusesUpdate(session);
// Set inputs
statusesUpdateChoreo.setAccessToken("2881358115-wqORtFIwM25CwVfrw7xB6ZBElOZ9wTiVDotqiwV");
statusesUpdateChoreo.setAccessTokenSecret("lB7M7Yr08lM6wda6W2h9oOaIdXuiFiO7i3Y67owkdKHJK");
statusesUpdateChoreo.setConsumerSecret("aDUmwJyDtIS0WuIdyq8U322FB5gxBL7GDfUfsVXP01ca8XcGTg");
statusesUpdateChoreo.setStatusUpdate(getLaughText(finalPressure));
statusesUpdateChoreo.setConsumerKey("eGeUwIZoaLGkhC8FlwpfaFwBZ");
// Run the Choreo and store the results
StatusesUpdateResultSet statusesUpdateResults = statusesUpdateChoreo.run();
// Print results
println(statusesUpdateResults.getResponse());
}
Arduino code:
// This Arduino program reads two analog signals,
// such as from two potentiometers, and transmits
// the digitized values over serial communication.
int sensorValue0 = 0; // variable to store the value coming from the sensor
int sensorValue1 = 0; // variable to store the value coming from the other sensor
void setup() {
Serial.begin(9600); // initialize serial communications
}
void loop() {
// Read the value from the sensor(s):
sensorValue0 = analogRead (A0); // reads value from Analog input 0
sensorValue1 = analogRead (A2); // reads value from Analog input 1
Serial.print ("A");
Serial.println (sensorValue0);
Serial.print ("B");
Serial.println (sensorValue1);
delay (50); // wait a fraction of a second, to be polite
}